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:
parent
0264d89d14
commit
63f7112e0a
@ -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 <Card bordered={true}>{props.children}</Card>;
|
||||
};
|
||||
|
||||
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<ArrayField>();
|
||||
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')}
|
||||
</Button>
|
||||
)}
|
||||
</Card>
|
||||
|
@ -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', {
|
||||
|
Loading…
Reference in New Issue
Block a user