From 97a4ef742249eec40bf29b53b9a72586aea086a6 Mon Sep 17 00:00:00 2001 From: chenos Date: Tue, 13 Jun 2023 09:46:01 +0800 Subject: [PATCH] fix(association-field): default values for the fields of a association field (#2037) --- .../antd/association-field/AssociationFieldProvider.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 cd2f632080..d511354da8 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 @@ -39,10 +39,12 @@ export const AssociationFieldProvider = observer( if (field.value?.[collectionField.targetKey]) { delete field.value[collectionField.targetKey]; } + field.value = { ...field.initialValue, ...field.value }; } else if (['belongsToMany', 'hasMany'].includes(collectionField.type)) { if (Array.isArray(field.value)) { - field.value.forEach((v) => { + field.value = field.value.map((v) => { delete v[collectionField.targetKey]; + return { ...field.initialValue?.[0], ...v }; }); } } @@ -65,7 +67,7 @@ export const AssociationFieldProvider = observer( } } setLoading(false); - }, [currentMode, collectionField, field.value]); + }, [currentMode, collectionField, JSON.stringify(field.value)]); if (loading) { return null;