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 <chenlinxh@gmail.com>
This commit is contained in:
katherinehhh 2023-05-17 14:26:11 +08:00 committed by GitHub
parent 0264d89d14
commit 63f7112e0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 9 deletions

View File

@ -1,11 +1,11 @@
import { CloseCircleOutlined } from '@ant-design/icons';
import { ArrayField } from '@formily/core'; import { ArrayField } from '@formily/core';
import { RecursionField, observer, useFieldSchema } from '@formily/react'; import { RecursionField, observer, useFieldSchema } from '@formily/react';
import { Button, Card, Divider } from 'antd'; import { Button, Card, Divider } from 'antd';
import { CloseCircleOutlined } from '@ant-design/icons';
import React, { useContext } from 'react'; import React, { useContext } from 'react';
import { useTranslation } from 'react-i18next';
import { AssociationFieldContext } from './context'; import { AssociationFieldContext } from './context';
import { useAssociationFieldContext } from './hooks'; import { useAssociationFieldContext } from './hooks';
import { useTranslation } from 'react-i18next';
// import { useRemoveActionProps } from '../../../block-provider/hooks'; // import { useRemoveActionProps } from '../../../block-provider/hooks';
export const Nester = (props) => { export const Nester = (props) => {
@ -23,12 +23,12 @@ const ToOneNester = (props) => {
return <Card bordered={true}>{props.children}</Card>; return <Card bordered={true}>{props.children}</Card>;
}; };
const toArr = (value) => { const toArr = (value, isReadpretty) => {
if (!value) { if (!value) {
return []; return isReadpretty ? [] : [{}];
} }
if (Array.isArray(value)) { if (Array.isArray(value)) {
return value; return value.length > 0 ? value : isReadpretty ? [] : [{}];
} }
return [value]; return [value];
}; };
@ -36,7 +36,7 @@ const toArr = (value) => {
const ToManyNester = observer((props) => { const ToManyNester = observer((props) => {
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const { field } = useAssociationFieldContext<ArrayField>(); const { field } = useAssociationFieldContext<ArrayField>();
const values = toArr(field.value); const values = toArr(field.value, field.readPretty);
const { t } = useTranslation(); const { t } = useTranslation();
// const { onClick } = useRemoveActionProps(`${collectionField.collectionName}.${collectionField.target}`); // const { onClick } = useRemoveActionProps(`${collectionField.collectionName}.${collectionField.target}`);
return ( return (
@ -64,11 +64,16 @@ const ToManyNester = observer((props) => {
type={'dashed'} type={'dashed'}
block block
onClick={() => { onClick={() => {
field.value = field.value || []; const v = field.value || [];
if (values.length > v.length) {
field.value = values;
} else {
field.value = v;
}
field.value.push({}); field.value.push({});
}} }}
> >
{t('Add new')} {t('Add new')}
</Button> </Button>
)} )}
</Card> </Card>

View File

@ -572,7 +572,7 @@ FormItem.Designer = function Designer() {
field.componentProps = field.componentProps || {}; field.componentProps = field.componentProps || {};
field.componentProps.mode = mode; field.componentProps.mode = mode;
if (mode === 'Nester') { if (mode === 'Nester') {
const initValue = ['o2m', 'm2m'].includes(collectionField?.interface) ? [] : {}; const initValue = ['hasMany', 'belongsToMany'].includes(collectionField?.type) ? [] : {};
field.value = field.value || initValue; field.value = field.value || initValue;
} }
dn.emit('patch', { dn.emit('patch', {