fix: dynamic component switching error

This commit is contained in:
chenos 2023-02-21 22:08:26 +08:00
parent 139ca9a635
commit 8113748f26
2 changed files with 17 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import { useField } from '@formily/react'; import { useField } from '@formily/react';
import { merge } from '@formily/shared'; import { merge } from '@formily/shared';
import flat from 'flat'; import flat from 'flat';
import cloneDeep from 'lodash/cloneDeep';
import get from 'lodash/get'; import get from 'lodash/get';
import { useContext, useEffect } from 'react'; import { useContext, useEffect } from 'react';
import { FilterContext, FilterLogicContext } from './context'; import { FilterContext, FilterLogicContext } from './context';
@ -35,7 +36,7 @@ export const useValues = () => {
if (!path) { if (!path) {
return; return;
} }
const [fieldPath, otherPath] = path.split('.$'); const [fieldPath = '', otherPath = ''] = path.split('.$');
const [operatorValue] = otherPath.split('.', 2); const [operatorValue] = otherPath.split('.', 2);
const dataIndex = fieldPath.split('.'); const dataIndex = fieldPath.split('.');
const option = findOption(dataIndex, options); const option = findOption(dataIndex, options);
@ -57,7 +58,9 @@ export const useValues = () => {
field.data = field.data || {}; field.data = field.data || {};
field.data.operators = option?.operators; field.data.operators = option?.operators;
field.data.operator = operator; 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.dataIndex = dataIndex;
field.data.value = null; field.data.value = null;
data2value(); data2value();
@ -65,7 +68,10 @@ export const useValues = () => {
setOperator(operatorValue) { setOperator(operatorValue) {
const operator = field.data?.operators?.find?.((item) => item.value === operatorValue); const operator = field.data?.operators?.find?.((item) => item.value === operatorValue);
field.data.operator = operator; 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; field.data.value = operator.noValue ? operator.default || true : null;
data2value(); data2value();
}, },

View File

@ -69,9 +69,14 @@ const filterOption = (input, option) => (option?.label ?? '').toLowerCase().incl
const InternalSelect = connect( const InternalSelect = connect(
(props: Props) => { (props: Props) => {
const { objectValue, ...others } = props; const { objectValue, value, ...others } = props;
const mode = props.mode || props.multiple ? 'multiple' : undefined; const mode = props.mode || props.multiple ? 'multiple' : undefined;
const toValue = (v) => {
if (['multiple', 'tags'].includes(mode)) {
return v || [];
}
return v;
};
if (objectValue) { if (objectValue) {
return <ObjectSelect {...others} mode={mode} />; return <ObjectSelect {...others} mode={mode} />;
} }
@ -80,6 +85,7 @@ const InternalSelect = connect(
showSearch showSearch
filterOption={filterOption} filterOption={filterOption}
allowClear allowClear
value={toValue(value)}
{...others} {...others}
onChange={(changed) => { onChange={(changed) => {
props.onChange?.(changed === undefined ? null : changed); props.onChange?.(changed === undefined ? null : changed);