From 63f7112e0aa5f8de66ef0485a6019151794cabc1 Mon Sep 17 00:00:00 2001 From: katherinehhh Date: Wed, 17 May 2023 14:26:11 +0800 Subject: [PATCH] feat(association-field): default one data for to-many association (#1873) * refactor: default one data for multiple association * refactor: default one data for multiple association * refactor: default one data for multiple association * fix: initial value * fix: add new --------- Co-authored-by: chenos --- .../antd/association-field/Nester.tsx | 21 ++++++++++++------- .../antd/form-item/FormItem.tsx | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) 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 29a932ba8..d9a788953 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 @@ -1,11 +1,11 @@ +import { CloseCircleOutlined } from '@ant-design/icons'; import { ArrayField } from '@formily/core'; import { RecursionField, observer, useFieldSchema } from '@formily/react'; import { Button, Card, Divider } from 'antd'; -import { CloseCircleOutlined } from '@ant-design/icons'; import React, { useContext } from 'react'; +import { useTranslation } from 'react-i18next'; import { AssociationFieldContext } from './context'; import { useAssociationFieldContext } from './hooks'; -import { useTranslation } from 'react-i18next'; // import { useRemoveActionProps } from '../../../block-provider/hooks'; export const Nester = (props) => { @@ -23,12 +23,12 @@ const ToOneNester = (props) => { return {props.children}; }; -const toArr = (value) => { +const toArr = (value, isReadpretty) => { if (!value) { - return []; + return isReadpretty ? [] : [{}]; } if (Array.isArray(value)) { - return value; + return value.length > 0 ? value : isReadpretty ? [] : [{}]; } return [value]; }; @@ -36,7 +36,7 @@ const toArr = (value) => { const ToManyNester = observer((props) => { const fieldSchema = useFieldSchema(); const { field } = useAssociationFieldContext(); - const values = toArr(field.value); + const values = toArr(field.value, field.readPretty); const { t } = useTranslation(); // const { onClick } = useRemoveActionProps(`${collectionField.collectionName}.${collectionField.target}`); return ( @@ -64,11 +64,16 @@ const ToManyNester = observer((props) => { type={'dashed'} block onClick={() => { - field.value = field.value || []; + const v = field.value || []; + if (values.length > v.length) { + field.value = values; + } else { + field.value = v; + } field.value.push({}); }} > - {t('Add new')} + {t('Add new')} )} diff --git a/packages/core/client/src/schema-component/antd/form-item/FormItem.tsx b/packages/core/client/src/schema-component/antd/form-item/FormItem.tsx index 855a34acc..586a550fc 100644 --- a/packages/core/client/src/schema-component/antd/form-item/FormItem.tsx +++ b/packages/core/client/src/schema-component/antd/form-item/FormItem.tsx @@ -572,7 +572,7 @@ FormItem.Designer = function Designer() { field.componentProps = field.componentProps || {}; field.componentProps.mode = mode; if (mode === 'Nester') { - const initValue = ['o2m', 'm2m'].includes(collectionField?.interface) ? [] : {}; + const initValue = ['hasMany', 'belongsToMany'].includes(collectionField?.type) ? [] : {}; field.value = field.value || initValue; } dn.emit('patch', {