refactor(duplicate action):duplicate fields error when change duplicate mode & support unselect all (#2768)

* refactor: depulication action

* refactor: depulication action
This commit is contained in:
katherinehhh 2023-10-09 14:29:26 +08:00 committed by GitHub
parent caa75877ab
commit c923dfafed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 4 deletions

View File

@ -871,6 +871,7 @@ export default {
'Sync successfully': '同步成功',
'Sync from form fields': '同步表单字段',
'Select all': '全选',
'UnSelect all':'取消全选',
'Determine whether a record exists by the following fields': '通过以下字段判断记录是否存在',
'Cascade Select': '级联选择',
Execute: '执行',

View File

@ -1,5 +1,5 @@
import { ArrayTable } from '@formily/antd-v5';
import { onFieldValueChange } from '@formily/core';
import { onFieldValueChange, onFieldInputValueChange } from '@formily/core';
import { connect, ISchema, mapProps, useField, useFieldSchema, useForm, useFormEffects } from '@formily/react';
import { isValid, uid } from '@formily/shared';
import { Alert, Tree as AntdTree } from 'antd';
@ -21,16 +21,36 @@ import { requestSettingsSchema } from './utils';
const Tree = connect(
AntdTree,
mapProps((props, field: any) => {
useEffect(() => {
field.value = props.defaultCheckedKeys || [];
}, []);
const [checkedKeys, setCheckedKeys] = useState(props.defaultCheckedKeys || []);
const onCheck = (checkedKeys) => {
setCheckedKeys(checkedKeys);
field.value = checkedKeys;
};
field.onCheck = onCheck;
const form = useForm();
return {
...props,
checkedKeys,
onCheck,
treeData: props?.treeData.map((v: any) => {
if (form.values.duplicateMode === 'quickDulicate') {
const children = v?.children?.map((k) => {
return {
...k,
disabled: false,
};
});
return {
...v,
disabled: false,
children,
};
}
return v;
}),
};
}),
);
@ -265,6 +285,17 @@ function DuplicationMode() {
},
};
};
const useUnSelectAllFields = (form) => {
return {
async run() {
form.query('duplicateFields').take((f) => {
f.componentProps.defaultCheckedKeys = [];
f.setInitialValue([]);
f?.onCheck([]);
});
},
};
};
return (
<SchemaSettings.ModalItem
title={t('Duplicate mode')}
@ -276,6 +307,8 @@ function DuplicationMode() {
getOnLoadData,
getOnCheck,
treeData: fieldSchema['x-component-props']?.treeData,
duplicateValues,
onFieldInputValueChange,
}}
schema={
{
@ -366,6 +399,29 @@ function DuplicationMode() {
},
},
},
unselectAll: {
type: 'void',
title: '{{ t("UnSelect all") }}',
'x-component': 'Action.Link',
'x-reactions': [
{
dependencies: ['.duplicateMode', '.duplicateFields'],
fulfill: {
state: {
visible: `{{ $deps[0]==="quickDulicate"&&$form.getValuesIn('duplicateFields').length>0 }}`,
},
},
},
],
'x-component-props': {
type: 'primary',
style: { float: 'right', position: 'relative', zIndex: 1200, marginRight: '10px' },
useAction: () => {
const from = useForm();
return useUnSelectAllFields(from);
},
},
},
duplicateFields: {
type: 'array',
title: '{{ t("Data fields") }}',
@ -392,7 +448,7 @@ function DuplicationMode() {
},
'x-reactions': [
{
dependencies: ['.collection'],
dependencies: ['.collection', '.duplicateMode'],
fulfill: {
state: {
disabled: '{{ !$deps[0] }}',
@ -415,7 +471,7 @@ function DuplicationMode() {
fieldSchema['x-component-props'].duplicateMode = duplicateMode;
fieldSchema['x-component-props'].duplicateFields = fields;
fieldSchema['x-component-props'].duplicateCollection = collection;
fieldSchema['x-component-props'].treeData = treeData;
fieldSchema['x-component-props'].treeData = treeData || field.componentProps?.treeData;
dn.emit('patch', {
schema: {
['x-uid']: fieldSchema['x-uid'],