From 8113748f261397d7639d9aae37764c168d6b4754 Mon Sep 17 00:00:00 2001 From: chenos Date: Tue, 21 Feb 2023 22:08:26 +0800 Subject: [PATCH] fix: dynamic component switching error --- .../src/schema-component/antd/filter/useValues.ts | 12 +++++++++--- .../src/schema-component/antd/select/Select.tsx | 10 ++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/filter/useValues.ts b/packages/core/client/src/schema-component/antd/filter/useValues.ts index 4e9b2d5ab..e7b16e5ff 100644 --- a/packages/core/client/src/schema-component/antd/filter/useValues.ts +++ b/packages/core/client/src/schema-component/antd/filter/useValues.ts @@ -1,6 +1,7 @@ import { useField } from '@formily/react'; import { merge } from '@formily/shared'; import flat from 'flat'; +import cloneDeep from 'lodash/cloneDeep'; import get from 'lodash/get'; import { useContext, useEffect } from 'react'; import { FilterContext, FilterLogicContext } from './context'; @@ -35,7 +36,7 @@ export const useValues = () => { if (!path) { return; } - const [fieldPath, otherPath] = path.split('.$'); + const [fieldPath = '', otherPath = ''] = path.split('.$'); const [operatorValue] = otherPath.split('.', 2); const dataIndex = fieldPath.split('.'); const option = findOption(dataIndex, options); @@ -57,7 +58,9 @@ export const useValues = () => { field.data = field.data || {}; field.data.operators = option?.operators; field.data.operator = operator; - field.data.schema = merge(option?.schema, operator?.schema); + const s1 = cloneDeep(option?.schema); + const s2 = cloneDeep(operator?.schema); + field.data.schema = merge(s1, s2); field.data.dataIndex = dataIndex; field.data.value = null; data2value(); @@ -65,7 +68,10 @@ export const useValues = () => { setOperator(operatorValue) { const operator = field.data?.operators?.find?.((item) => item.value === operatorValue); field.data.operator = operator; - field.data.schema = merge(field.data.schema, operator.schema); + const option = findOption(field.data.dataIndex, options); + const s1 = cloneDeep(option?.schema); + const s2 = cloneDeep(operator?.schema); + field.data.schema = merge(s1, s2); field.data.value = operator.noValue ? operator.default || true : null; data2value(); }, diff --git a/packages/core/client/src/schema-component/antd/select/Select.tsx b/packages/core/client/src/schema-component/antd/select/Select.tsx index 090f9a471..9cfdf6ef8 100644 --- a/packages/core/client/src/schema-component/antd/select/Select.tsx +++ b/packages/core/client/src/schema-component/antd/select/Select.tsx @@ -69,9 +69,14 @@ const filterOption = (input, option) => (option?.label ?? '').toLowerCase().incl const InternalSelect = connect( (props: Props) => { - const { objectValue, ...others } = props; + const { objectValue, value, ...others } = props; const mode = props.mode || props.multiple ? 'multiple' : undefined; - + const toValue = (v) => { + if (['multiple', 'tags'].includes(mode)) { + return v || []; + } + return v; + }; if (objectValue) { return ; } @@ -80,6 +85,7 @@ const InternalSelect = connect( showSearch filterOption={filterOption} allowClear + value={toValue(value)} {...others} onChange={(changed) => { props.onChange?.(changed === undefined ? null : changed);