From fd493dba9c422db0f1bdf96b94a8d353e66b60c4 Mon Sep 17 00:00:00 2001 From: chenos Date: Thu, 14 Apr 2022 01:03:28 +0800 Subject: [PATCH] feat: custom action initializer --- .../client/src/block-provider/hooks/index.ts | 16 +++- .../antd/action/Action.Designer.tsx | 32 ++++++++ .../antd/action/ActionBar.tsx | 10 ++- .../buttons/FormActionInitializers.tsx | 81 +++++++++++++++++++ .../src/schema-initializer/items/index.tsx | 4 + 5 files changed, 139 insertions(+), 4 deletions(-) diff --git a/packages/client/src/block-provider/hooks/index.ts b/packages/client/src/block-provider/hooks/index.ts index 1bb312cd7..e06f3269f 100644 --- a/packages/client/src/block-provider/hooks/index.ts +++ b/packages/client/src/block-provider/hooks/index.ts @@ -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)) { diff --git a/packages/client/src/schema-component/antd/action/Action.Designer.tsx b/packages/client/src/schema-component/antd/action/Action.Designer.tsx index 1ea3fa2c0..adc29fe79 100644 --- a/packages/client/src/schema-component/antd/action/Action.Designer.tsx +++ b/packages/client/src/schema-component/antd/action/Action.Designer.tsx @@ -77,6 +77,38 @@ export const ActionDesigner = (props) => { }} /> )} + {fieldSchema['x-action-params'] && ( + { + 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) {} + }} + /> + )} { if (layout === 'one-column') { return (
- {props.children &&
{props.children}
} + {props.children && ( +
+ + {fieldSchema.mapProperties((schema, key) => { + return ; + })} + +
+ )} {render()}
); diff --git a/packages/client/src/schema-initializer/buttons/FormActionInitializers.tsx b/packages/client/src/schema-initializer/buttons/FormActionInitializers.tsx index 97923f171..2b9593383 100644 --- a/packages/client/src/schema-initializer/buttons/FormActionInitializers.tsx +++ b/packages/client/src/schema-initializer/buttons/FormActionInitializers.tsx @@ -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 }}', + }, + }, }, ], }, diff --git a/packages/client/src/schema-initializer/items/index.tsx b/packages/client/src/schema-initializer/items/index.tsx index 9d3ca4a9b..09a380bd8 100644 --- a/packages/client/src/schema-initializer/items/index.tsx +++ b/packages/client/src/schema-initializer/items/index.tsx @@ -36,6 +36,10 @@ export const BlockInitializer = (props) => { ); }; +export const CustomizeActionInitializer = (props) => { + return ; +}; + export const InitializerWithSwitch = (props) => { const { type, schema, item, insert } = props; const { exists, remove } = useCurrentSchema(schema?.[type] || item?.schema?.[type], type, item.find, item.remove);