feat: custom action initializer

This commit is contained in:
chenos 2022-04-14 01:03:28 +08:00
parent fda9c71d66
commit fd493dba9c
5 changed files with 139 additions and 4 deletions

View File

@ -1,4 +1,4 @@
import { useForm } from '@formily/react';
import { useFieldSchema, useForm } from '@formily/react';
import { Modal } from 'antd';
import { useTranslation } from 'react-i18next';
import { useHistory } from 'react-router-dom';
@ -35,11 +35,16 @@ export const useCreateActionProps = () => {
const { field } = useFormBlockContext();
const history = useHistory();
const { t } = useTranslation();
const actionSchema = useFieldSchema();
return {
async onClick() {
await form.submit();
const initialValues = actionSchema?.['x-action-params']?.initialValues;
await resource.create({
values: form.values,
values: {
...form.values,
...initialValues,
},
});
__parent?.service?.refresh?.();
setVisible?.(false);
@ -73,12 +78,17 @@ export const useUpdateActionProps = () => {
const filterByTk = useFilterByTk();
const { resource, __parent } = useBlockRequestContext();
const { setVisible } = useActionContext();
const actionSchema = useFieldSchema();
return {
async onClick() {
const initialValues = actionSchema?.['x-action-params']?.initialValues;
await form.submit();
await resource.update({
filterByTk,
values: form.values,
values: {
...form.values,
...initialValues,
},
});
__parent?.service?.refresh?.();
if (!(resource instanceof TableFieldResource)) {

View File

@ -77,6 +77,38 @@ export const ActionDesigner = (props) => {
}}
/>
)}
{fieldSchema['x-action-params'] && (
<SchemaSettings.ModalItem
title={'表单默认值'}
schema={
{
type: 'object',
properties: {
initialValues: {
'x-decorator': 'FormItem',
'x-component': 'Input.TextArea',
default: JSON.stringify(fieldSchema?.['x-action-params']?.initialValues),
},
},
} as ISchema
}
onSubmit={({ initialValues }) => {
try {
const values = JSON.parse(initialValues);
fieldSchema['x-action-params']['initialValues'] = values;
dn.emit('patch', {
schema: {
['x-uid']: fieldSchema['x-uid'],
'x-action-params': {
initialValues: values,
},
},
});
dn.refresh();
} catch (e) {}
}}
/>
)}
<SchemaSettings.Divider />
<SchemaSettings.Remove
removeParentsIfNoChildren

View File

@ -11,7 +11,15 @@ export const ActionBar = observer((props: any) => {
if (layout === 'one-column') {
return (
<div style={{ display: 'flex', ...style }} {...others}>
{props.children && <div style={{ marginRight: 8 }}>{props.children}</div>}
{props.children && (
<div style={{ marginRight: 8 }}>
<Space>
{fieldSchema.mapProperties((schema, key) => {
return <RecursionField key={key} name={key} schema={schema} />;
})}
</Space>
</div>
)}
{render()}
</div>
);

View File

@ -11,6 +11,33 @@ export const FormActionInitializers = {
type: 'item',
title: '{{t("Submit")}}',
component: 'CreateSubmitActionInitializer',
schema: {
'x-action-params': {
initialValues: {},
},
},
},
],
},
{
type: 'subMenu',
title: '{{t("Customize")}}',
children: [
{
type: 'item',
title: '{{t("Save action")}}',
component: 'CustomizeActionInitializer',
schema: {
title: '{{ t("Save") }}',
'x-component': 'Action',
'x-designer': 'Action.Designer',
'x-action-params': {
initialValues: {},
},
'x-component-props': {
useProps: '{{ useCreateActionProps }}',
},
},
},
],
},
@ -29,6 +56,33 @@ export const CreateFormActionInitializers = {
type: 'item',
title: '{{t("Submit")}}',
component: 'CreateSubmitActionInitializer',
schema: {
'x-action-params': {
initialValues: {},
},
},
},
],
},
{
type: 'subMenu',
title: '{{t("Customize")}}',
children: [
{
type: 'item',
title: '{{t("Save action")}}',
component: 'CustomizeActionInitializer',
schema: {
title: '{{ t("Save") }}',
'x-component': 'Action',
'x-designer': 'Action.Designer',
'x-action-params': {
initialValues: {},
},
'x-component-props': {
useProps: '{{ useCreateActionProps }}',
},
},
},
],
},
@ -47,6 +101,33 @@ export const UpdateFormActionInitializers = {
type: 'item',
title: '{{t("Submit")}}',
component: 'UpdateSubmitActionInitializer',
schema: {
'x-action-params': {
initialValues: {},
},
},
},
],
},
{
type: 'subMenu',
title: '{{t("Customize")}}',
children: [
{
type: 'item',
title: '{{t("Save action")}}',
component: 'CustomizeActionInitializer',
schema: {
title: '{{ t("Save") }}',
'x-component': 'Action',
'x-designer': 'Action.Designer',
'x-action-params': {
initialValues: {},
},
'x-component-props': {
useProps: '{{ useUpdateActionProps }}',
},
},
},
],
},

View File

@ -36,6 +36,10 @@ export const BlockInitializer = (props) => {
);
};
export const CustomizeActionInitializer = (props) => {
return <BlockInitializer {...props} />;
};
export const InitializerWithSwitch = (props) => {
const { type, schema, item, insert } = props;
const { exists, remove } = useCurrentSchema(schema?.[type] || item?.schema?.[type], type, item.find, item.remove);