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

View File

@ -1,5 +1,5 @@
import { ArrayTable } from '@formily/antd-v5'; 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 { connect, ISchema, mapProps, useField, useFieldSchema, useForm, useFormEffects } from '@formily/react';
import { isValid, uid } from '@formily/shared'; import { isValid, uid } from '@formily/shared';
import { Alert, Tree as AntdTree } from 'antd'; import { Alert, Tree as AntdTree } from 'antd';
@ -21,16 +21,36 @@ import { requestSettingsSchema } from './utils';
const Tree = connect( const Tree = connect(
AntdTree, AntdTree,
mapProps((props, field: any) => { mapProps((props, field: any) => {
useEffect(() => {
field.value = props.defaultCheckedKeys || [];
}, []);
const [checkedKeys, setCheckedKeys] = useState(props.defaultCheckedKeys || []); const [checkedKeys, setCheckedKeys] = useState(props.defaultCheckedKeys || []);
const onCheck = (checkedKeys) => { const onCheck = (checkedKeys) => {
setCheckedKeys(checkedKeys); setCheckedKeys(checkedKeys);
field.value = checkedKeys; field.value = checkedKeys;
}; };
field.onCheck = onCheck; field.onCheck = onCheck;
const form = useForm();
return { return {
...props, ...props,
checkedKeys, checkedKeys,
onCheck, 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 ( return (
<SchemaSettings.ModalItem <SchemaSettings.ModalItem
title={t('Duplicate mode')} title={t('Duplicate mode')}
@ -276,6 +307,8 @@ function DuplicationMode() {
getOnLoadData, getOnLoadData,
getOnCheck, getOnCheck,
treeData: fieldSchema['x-component-props']?.treeData, treeData: fieldSchema['x-component-props']?.treeData,
duplicateValues,
onFieldInputValueChange,
}} }}
schema={ schema={
{ {
@ -352,7 +385,7 @@ function DuplicationMode() {
dependencies: ['.duplicateMode'], dependencies: ['.duplicateMode'],
fulfill: { fulfill: {
state: { state: {
visible: `{{ $deps[0]==="quickDulicate" }}`, visible: `{{ $deps[0]==="quickDulicate"}}`,
}, },
}, },
}, },
@ -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: { duplicateFields: {
type: 'array', type: 'array',
title: '{{ t("Data fields") }}', title: '{{ t("Data fields") }}',
@ -392,7 +448,7 @@ function DuplicationMode() {
}, },
'x-reactions': [ 'x-reactions': [
{ {
dependencies: ['.collection'], dependencies: ['.collection', '.duplicateMode'],
fulfill: { fulfill: {
state: { state: {
disabled: '{{ !$deps[0] }}', disabled: '{{ !$deps[0] }}',
@ -415,7 +471,7 @@ function DuplicationMode() {
fieldSchema['x-component-props'].duplicateMode = duplicateMode; fieldSchema['x-component-props'].duplicateMode = duplicateMode;
fieldSchema['x-component-props'].duplicateFields = fields; fieldSchema['x-component-props'].duplicateFields = fields;
fieldSchema['x-component-props'].duplicateCollection = collection; fieldSchema['x-component-props'].duplicateCollection = collection;
fieldSchema['x-component-props'].treeData = treeData; fieldSchema['x-component-props'].treeData = treeData || field.componentProps?.treeData;
dn.emit('patch', { dn.emit('patch', {
schema: { schema: {
['x-uid']: fieldSchema['x-uid'], ['x-uid']: fieldSchema['x-uid'],