From 976efd93a10b3031c8c33ed5496f411b8e0c3509 Mon Sep 17 00:00:00 2001 From: Joel Date: Fri, 30 Aug 2024 14:14:33 +0800 Subject: [PATCH] feat: support filter variable var data sync --- .../components/filter-condition.tsx | 101 +++++++++++++++--- .../components/sub-variable-picker.tsx | 27 +++-- .../workflow/nodes/list-filter/panel.tsx | 16 ++- .../workflow/nodes/list-filter/types.ts | 5 +- .../workflow/nodes/list-filter/use-config.ts | 68 +++++++++--- 5 files changed, 180 insertions(+), 37 deletions(-) diff --git a/web/app/components/workflow/nodes/list-filter/components/filter-condition.tsx b/web/app/components/workflow/nodes/list-filter/components/filter-condition.tsx index e44157301d..10cbb26010 100644 --- a/web/app/components/workflow/nodes/list-filter/components/filter-condition.tsx +++ b/web/app/components/workflow/nodes/list-filter/components/filter-condition.tsx @@ -1,31 +1,106 @@ 'use client' import type { FC } from 'react' -import React from 'react' +import React, { useCallback, useMemo } from 'react' +import { useTranslation } from 'react-i18next' +import ConditionOperator from '../../if-else/components/condition-list/condition-operator' +import { VarType } from '../../../types' +import type { Condition } from '../types' +import { ComparisonOperator } from '../../if-else/types' +import { comparisonOperatorNotRequireValue, getOperators } from '../../if-else/utils' import SubVariablePicker from './sub-variable-picker' -import { SimpleSelect as Select } from '@/app/components/base/select' import Input from '@/app/components/base/input' +import { FILE_TYPE_OPTIONS, TRANSFER_METHOD } from '@/app/components/workflow/nodes/if-else/default' +import { SimpleSelect as Select } from '@/app/components/base/select' +const optionNameI18NPrefix = 'workflow.nodes.ifElse.optionName' type Props = { + condition: Condition + onChange: (condition: Condition) => void + varType: VarType hasSubVariable: boolean } const FilterCondition: FC = ({ + condition, + varType, + onChange, hasSubVariable, }) => { - const [input, setInput] = React.useState('') + const { t } = useTranslation() + const isSelect = [ComparisonOperator.in, ComparisonOperator.notIn, ComparisonOperator.allOf].includes(condition.comparison_operator) + const selectOptions = useMemo(() => { + if (isSelect) { + if (condition.key === 'type' || condition.comparison_operator === ComparisonOperator.allOf) { + return FILE_TYPE_OPTIONS.map(item => ({ + name: t(`${optionNameI18NPrefix}.${item.i18nKey}`), + value: item.value, + })) + } + if (condition.key === 'transfer_method') { + return TRANSFER_METHOD.map(item => ({ + name: t(`${optionNameI18NPrefix}.${item.i18nKey}`), + value: item.value, + })) + } + return [] + } + return [] + }, [condition.comparison_operator, condition.key, isSelect, t]) + const handleChange = useCallback((key: string) => { + return (value: any) => { + onChange({ + ...condition, + [key]: value, + }) + } + }, [condition, onChange]) + + const handleSubVariableChange = useCallback((value: string) => { + onChange({ + key: value, + comparison_operator: getOperators(varType, { key: value })[0], + value: '', + }) + }, [onChange, varType]) + return (
- {hasSubVariable && } -
- setInput(e.target.value)} /> + )} +
+ + {!comparisonOperatorNotRequireValue(condition.comparison_operator) && ( + <> + {isSelect && ( + handleChange('value')(e.target.value)} + /> + )} + + )}
) diff --git a/web/app/components/workflow/nodes/list-filter/components/sub-variable-picker.tsx b/web/app/components/workflow/nodes/list-filter/components/sub-variable-picker.tsx index 4714ec73e5..5481992455 100644 --- a/web/app/components/workflow/nodes/list-filter/components/sub-variable-picker.tsx +++ b/web/app/components/workflow/nodes/list-filter/components/sub-variable-picker.tsx @@ -1,17 +1,28 @@ 'use client' import type { FC } from 'react' -import React from 'react' +import React, { useCallback } from 'react' +import { SUB_VARIABLES } from '../../if-else/default' +import type { Item } from '@/app/components/base/select' import { SimpleSelect as Select } from '@/app/components/base/select' import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development' import cn from '@/utils/classnames' type Props = { + value: string + onChange: (value: string) => void className?: string } const SubVariablePicker: FC = ({ + value, + onChange, className, }) => { + const subVarOptions = SUB_VARIABLES.map(item => ({ + value: item, + name: item, + })) + const renderOption = ({ item }: { item: Record }) => { return (
@@ -23,15 +34,17 @@ const SubVariablePicker: FC = ({
) } + + const handleChange = useCallback(({ value }: Item) => { + onChange(value as string) + }, [onChange]) + return (