From 07584909137318a9ef5ef0fdbfee7df5d280ab90 Mon Sep 17 00:00:00 2001 From: Katherine Date: Thu, 24 Apr 2025 09:54:22 +0800 Subject: [PATCH] fix: unexpected data creation when displaying association field under sub-form/sub-table in create form action (#6727) * fix: unexpected data creation when displaying relational field subtable in create form * fix: bug * fix: bug * fix: bug --- .../client/src/block-provider/hooks/index.ts | 26 ++++++++++++++++++- .../association-field/AssociationSelect.tsx | 11 +++++--- ...eLazyLoadDisplayAssociationFieldsOfForm.ts | 4 +++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/core/client/src/block-provider/hooks/index.ts b/packages/core/client/src/block-provider/hooks/index.ts index 1b82601e71..9f47f4e6c2 100644 --- a/packages/core/client/src/block-provider/hooks/index.ts +++ b/packages/core/client/src/block-provider/hooks/index.ts @@ -97,6 +97,30 @@ const filterValue = (value) => { return obj; }; +function getFilteredFormValues(form) { + const values = _.cloneDeep(form.values); + const allFields = []; + form.query('*').forEach((field) => { + if (field) { + allFields.push(field); + } + }); + const readonlyPaths = allFields + .filter((field) => field?.componentProps?.readOnlySubmit) + .map((field) => { + const segments = field.path?.segments || []; + if (segments.length <= 1) { + return segments.join('.'); + } + return segments.slice(0, -1).join('.'); + }); + for (const path of readonlyPaths) { + _.unset(values, path); + } + + return values; +} + export function getFormValues({ filterByTk, field, @@ -124,7 +148,7 @@ export function getFormValues({ } } - return form.values; + return getFilteredFormValues(form); } export function useCollectValuesToSubmit() { diff --git a/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx b/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx index ed9969a523..03c67938c8 100644 --- a/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx @@ -34,7 +34,7 @@ import useServiceOptions, { useAssociationFieldContext } from './hooks'; const removeIfKeyEmpty = (obj, filterTargetKey) => { if (!obj || typeof obj !== 'object' || !filterTargetKey || Array.isArray(obj)) return obj; - return !obj[filterTargetKey] ? null : obj; + return !obj[filterTargetKey] ? undefined : obj; }; export const AssociationFieldAddNewer = (props) => { @@ -106,8 +106,13 @@ const InternalAssociationSelect = observer( useEffect(() => { const initValue = isVariable(field.value) ? undefined : field.value; const value = Array.isArray(initValue) ? initValue.filter(Boolean) : initValue; - setInnerValue(value); - }, [field.value]); + const result = removeIfKeyEmpty(value, filterTargetKey); + setInnerValue(result); + if (!isEqual(field.value, result)) { + field.value = result; + } + }, [field.value, filterTargetKey]); + useEffect(() => { const id = uid(); form.addEffects(id, () => { diff --git a/packages/core/client/src/schema-component/antd/form-item/hooks/useLazyLoadDisplayAssociationFieldsOfForm.ts b/packages/core/client/src/schema-component/antd/form-item/hooks/useLazyLoadDisplayAssociationFieldsOfForm.ts index bd2f7e4357..4c9bba47bd 100644 --- a/packages/core/client/src/schema-component/antd/form-item/hooks/useLazyLoadDisplayAssociationFieldsOfForm.ts +++ b/packages/core/client/src/schema-component/antd/form-item/hooks/useLazyLoadDisplayAssociationFieldsOfForm.ts @@ -101,6 +101,10 @@ const useLazyLoadDisplayAssociationFieldsOfForm = () => { field.value = null; } else { field.value = result; + field.componentProps = { + ...field.componentProps, + readOnlySubmit: true, + }; // 让它不参与提交 } }); })