From 8d4123d8d6d4e99f8f79784916cae01dfc45d918 Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 9 Jun 2023 23:45:04 +0800 Subject: [PATCH] feat: filter out IDs in sub-forms (#2025) --- .../association-field/AssociationFieldProvider.tsx | 14 ++++++++++++++ .../schema-component/antd/form-v2/Templates.tsx | 2 ++ 2 files changed, 16 insertions(+) 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 1278e36d0..cd2f63208 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 @@ -33,6 +33,20 @@ export const AssociationFieldProvider = observer( setLoading(false); return; } + // 如果是表单模板数据,使用子表单和子表格组件时,过滤掉关系 ID + if (field.value && field.form['__template'] && ['Nester', 'SubTable'].includes(currentMode)) { + if (['belongsTo', 'hasOne'].includes(collectionField.type)) { + if (field.value?.[collectionField.targetKey]) { + delete field.value[collectionField.targetKey]; + } + } else if (['belongsToMany', 'hasMany'].includes(collectionField.type)) { + if (Array.isArray(field.value)) { + field.value.forEach((v) => { + delete v[collectionField.targetKey]; + }); + } + } + } if (field.value !== null && field.value !== undefined) { // Nester 子表单时,如果没数据初始化一个 [null] 的占位 if (currentMode === 'Nester' && Array.isArray(field.value)) { diff --git a/packages/core/client/src/schema-component/antd/form-v2/Templates.tsx b/packages/core/client/src/schema-component/antd/form-v2/Templates.tsx index 4e218e63b..ad739bb82 100644 --- a/packages/core/client/src/schema-component/antd/form-v2/Templates.tsx +++ b/packages/core/client/src/schema-component/antd/form-v2/Templates.tsx @@ -81,6 +81,7 @@ export const Templates = ({ style = {}, form }) => { useEffect(() => { if (enabled && defaultTemplate) { + form.__template = true; fetchTemplateData(api, defaultTemplate, t) .then((data) => { if (form && data) { @@ -106,6 +107,7 @@ export const Templates = ({ style = {}, form }) => { if (form && data) { // 切换之前先把之前的数据清空 form.reset(); + form.__template = true; forEach(data, (value, key) => { if (value) {