From 8b64a559e47fdb5d5249e2d3d689e950b520ace8 Mon Sep 17 00:00:00 2001 From: jack zhang <1098626505@qq.com> Date: Wed, 14 Jun 2023 19:29:29 +0800 Subject: [PATCH] fix: form performance (#2047) * fix: form performance * fix: refresh when the targetKeyValue change --------- Co-authored-by: chenos --- .../schema-component/antd/action/Action.tsx | 2 +- .../AssociationFieldProvider.tsx | 19 ++++++++++++++++++- .../antd/association-field/Nester.tsx | 4 ++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/action/Action.tsx b/packages/core/client/src/schema-component/antd/action/Action.tsx index 8555c356d..227b867cc 100644 --- a/packages/core/client/src/schema-component/antd/action/Action.tsx +++ b/packages/core/client/src/schema-component/antd/action/Action.tsx @@ -162,7 +162,7 @@ export const Action: ComposedAction = observer( > {popover && } {!popover && renderButton()} - {!popover && props.children} + {!popover &&
e.stopPropagation()}>{props.children}
} ); }, diff --git a/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx b/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx index d511354da..be31cc28e 100644 --- a/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx @@ -14,17 +14,33 @@ export const AssociationFieldProvider = observer( const collectionField = useMemo( () => getCollectionJoinField(fieldSchema['x-collection-field']), + // eslint-disable-next-line react-hooks/exhaustive-deps [fieldSchema['x-collection-field'], fieldSchema.name], ); const isFileCollection = useMemo( () => getCollection(collectionField?.target)?.template === 'file', + // eslint-disable-next-line react-hooks/exhaustive-deps [fieldSchema['x-collection-field']], ); const currentMode = useMemo( () => fieldSchema['x-component-props']?.mode || (isFileCollection ? 'FileManager' : 'Select'), + // eslint-disable-next-line react-hooks/exhaustive-deps [fieldSchema['x-component-props']?.mode], ); + const targetKeyValue = useMemo(() => { + if (!field.value) return ''; + if (['belongsTo', 'hasOne'].includes(collectionField.type)) { + return field.value[collectionField.targetKey] ?? ''; + } + if (['belongsToMany', 'hasMany'].includes(collectionField.type)) { + if (Array.isArray(field.value)) { + return field.value.map((v) => v[collectionField.targetKey] ?? '').join(','); + } + } + return ''; + }, [collectionField, field.value]); + const [loading, setLoading] = useState(true); useEffect(() => { @@ -67,7 +83,8 @@ export const AssociationFieldProvider = observer( } } setLoading(false); - }, [currentMode, collectionField, JSON.stringify(field.value)]); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [currentMode, collectionField, targetKeyValue]); if (loading) { return null; diff --git a/packages/core/client/src/schema-component/antd/association-field/Nester.tsx b/packages/core/client/src/schema-component/antd/association-field/Nester.tsx index 46e376efb..6e912d219 100644 --- a/packages/core/client/src/schema-component/antd/association-field/Nester.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/Nester.tsx @@ -46,7 +46,7 @@ const ToManyNester = observer( allowed = !value?.[options.targetKey]; } return ( - <> +
{field.editable && allowMultiple && ( @@ -88,7 +88,7 @@ const ToManyNester = observer(
- +
); })}