feat: filter out IDs in sub-forms (#2025)

This commit is contained in:
chenos 2023-06-09 23:45:04 +08:00 committed by GitHub
parent 84e52448f5
commit 8d4123d8d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -33,6 +33,20 @@ export const AssociationFieldProvider = observer(
setLoading(false); setLoading(false);
return; 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) { if (field.value !== null && field.value !== undefined) {
// Nester 子表单时,如果没数据初始化一个 [null] 的占位 // Nester 子表单时,如果没数据初始化一个 [null] 的占位
if (currentMode === 'Nester' && Array.isArray(field.value)) { if (currentMode === 'Nester' && Array.isArray(field.value)) {

View File

@ -81,6 +81,7 @@ export const Templates = ({ style = {}, form }) => {
useEffect(() => { useEffect(() => {
if (enabled && defaultTemplate) { if (enabled && defaultTemplate) {
form.__template = true;
fetchTemplateData(api, defaultTemplate, t) fetchTemplateData(api, defaultTemplate, t)
.then((data) => { .then((data) => {
if (form && data) { if (form && data) {
@ -106,6 +107,7 @@ export const Templates = ({ style = {}, form }) => {
if (form && data) { if (form && data) {
// 切换之前先把之前的数据清空 // 切换之前先把之前的数据清空
form.reset(); form.reset();
form.__template = true;
forEach(data, (value, key) => { forEach(data, (value, key) => {
if (value) { if (value) {