From c86d084f5a1d8981761569644c4b7dcdc7b567bc Mon Sep 17 00:00:00 2001 From: Jian Lu Date: Thu, 23 Jan 2025 17:23:19 +0800 Subject: [PATCH] feat: support action add --- .../ActionsSetting/Action.tsx | 99 ++++++++++++------- .../ActionsSetting/ActionParams.tsx | 45 +++++++++ .../ActionsSetting/Actions.tsx | 74 ++++++++------ .../ActionsSetting/hooks/useActionOptions.ts | 27 +++++ .../hooks/useActionParamOptions.ts | 27 +++++ .../EventSettingItem/ActionsSetting/index.tsx | 5 +- 6 files changed, 209 insertions(+), 68 deletions(-) create mode 100644 packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/ActionParams.tsx create mode 100644 packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/hooks/useActionOptions.ts create mode 100644 packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/hooks/useActionParamOptions.ts diff --git a/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/Action.tsx b/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/Action.tsx index 818a330dc4..30c18a330c 100644 --- a/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/Action.tsx +++ b/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/Action.tsx @@ -7,10 +7,10 @@ * For more information, please refer to: https://www.nocobase.com/agreement. */ -import { ArrayField as ArrayFieldModel, VoidField } from '@formily/core'; +import { ArrayField as ArrayFieldModel, VoidField, ObjectField as ObjectFieldModel } from '@formily/core'; import { ArrayField, ObjectField, observer, useField } from '@formily/react'; -import { Space, TreeSelect } from 'antd'; -import React, { useCallback, useMemo } from 'react'; +import { Button, Cascader, Input, Space, TreeSelect } from 'antd'; +import React, { useCallback, useContext, useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import { withDynamicSchemaProps } from '../../../hoc/withDynamicSchemaProps'; import { @@ -20,40 +20,64 @@ import { } from './LinkageRuleAction'; import { RemoveActionContext } from './context'; import { useControllableValue } from 'ahooks'; +import { CloseCircleOutlined } from '@ant-design/icons'; +import { useActionOptions } from './hooks/useActionOptions'; -const ActionSelect = (props) => { - const { t } = useTranslation(); - const { definitions, className } = props; - const [state, setState] = useControllableValue(props, { - defaultValue: undefined, - }); - let treeData = definitions?.map((module) => ({ - value: module.name, - title: module.title + ' - ' + module.uid, - selectable: false, - children: - module?.events?.map((event) => ({ - value: module.name + '.' + event.name, - title: event.title, - })) || [], - })); - treeData = treeData?.filter((item) => item.children.length > 0); - +const ActionParams = observer((props) => { + const field = useField(); return ( - { - setState(value); - }} - treeData={treeData} - className={className} - /> +
+ {field?.value?.map((item, index) => ( +
+ +
+ ))} +
); -}; +}); + +export const ActionRow = observer( + (props: any): any => { + const { onRemove } = props; + const options = useActionOptions(); + const field = useField(); + const onClick = useCallback(() => { + console.log('field2', field); + // const f = field.query('.params').take() as ObjectFieldModel; + // field.val = field.data || {}; + // field.data.params = field.data.params || []; + // field.data.params.push({}); + // const items = f.value || []; + // items.push({}); + // f.value = items; + // f.initialValue = items; + }, [field]); + return ( +
+ + { + console.log('value', value); + // setState(value); + }} + > +
+ + +
+ {!props.disabled && ( + + + + )} +
+
+ ); + }, + { displayName: 'ActionRow' }, +); export const Action = observer( (props: any): any => { @@ -67,13 +91,16 @@ export const Action = observer( field: FormFieldLinkageRuleAction, style: FormStyleLinkageRuleAction, }; + + // console.log('field?.value', field.value); + return field?.value?.map((item, index) => { return ( field.remove(index)}> - + ); }); }, - { displayName: 'LinkageRuleActions' }, + { displayName: 'Action' }, ); diff --git a/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/ActionParams.tsx b/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/ActionParams.tsx new file mode 100644 index 0000000000..647c21ba1c --- /dev/null +++ b/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/ActionParams.tsx @@ -0,0 +1,45 @@ +/** + * This file is part of the NocoBase (R) project. + * Copyright (c) 2020-2024 NocoBase Co., Ltd. + * Authors: NocoBase Team. + * + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. + * For more information, please refer to: https://www.nocobase.com/agreement. + */ + +import { Field, ObjectField, observer, useField } from '@formily/react'; +import { Button, Input, Space } from 'antd'; +import React from 'react'; +import { ArrayField as ArrayFieldModel } from '@formily/core'; +import { CloseCircleOutlined } from '@ant-design/icons'; + +const ActionParams = observer((props: any) => { + return ( + + + 等于 + + {!props.disabled && ( + + + + )} + + ); +}); + +export const ActionParamsField = observer((props: { action: string }) => { + const field = useField(); + const { action } = props; + console.log('ActionParamsField action', props); + return ( + + {field?.value?.map((item, index) => ( + field.remove(index) }]} /> + ))} + + + ); +}); diff --git a/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/Actions.tsx b/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/Actions.tsx index e49e1d634b..2f6b3190e2 100644 --- a/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/Actions.tsx +++ b/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/Actions.tsx @@ -8,13 +8,15 @@ */ import { ArrayField as ArrayFieldModel, VoidField } from '@formily/core'; -import { ArrayField, ObjectField, observer, useField } from '@formily/react'; -import { Space } from 'antd'; +import { ArrayField, ObjectField, observer, useField, Field } from '@formily/react'; +import { Space, Button, Cascader } from 'antd'; import React, { useCallback, useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import { withDynamicSchemaProps } from '../../../hoc/withDynamicSchemaProps'; -import { Action } from './Action'; import { ArrayBase } from '@formily/antd-v5'; +import { useActionOptions } from './hooks/useActionOptions'; +import { CloseCircleOutlined } from '@ant-design/icons'; +import { ActionParamsField } from './ActionParams'; interface LinkageRuleActionGroupProps { type: 'button' | 'field' | 'style'; @@ -22,34 +24,48 @@ interface LinkageRuleActionGroupProps { collectionName: string; } -export const Actions = withDynamicSchemaProps( - (props: LinkageRuleActionGroupProps) => { - const { t } = useTranslation(); - const field = useField(); - const { category, elementType, linkageOptions, collectionName } = props; - - const components = useMemo( - () => [Action, { category, elementType, linkageOptions, collectionName }], - [collectionName, linkageOptions, category, elementType], - ); - const spaceStyle = useMemo(() => ({ marginTop: 8, marginBottom: 8 }), []); - const style = useMemo(() => ({ marginLeft: 10 }), []); - const onClick = useCallback(() => { - const f = field.query('.actions').take() as ArrayFieldModel; - const items = f.value || []; - items.push({}); - f.value = items; - f.initialValue = items; - }, [field]); - +const Action = observer( + (props: any): any => { + const { onRemove } = props; + const options = useActionOptions(); + const field = useField(); + field.onFieldChange = (field) => { + console.log('field', field); + }; return ( - + + + + {!props.disabled && ( + + + + )} + ); }, + { displayName: 'ActionRow' }, +); + +const Actions = withDynamicSchemaProps( + observer((props: LinkageRuleActionGroupProps) => { + const { t } = useTranslation(); + const field = useField(); + return ( + + {field?.value?.map((item, index) => { + return field.remove(index) }]} />; + })} + field.push({})}>{t('添加动作')} + + ); + }), { displayName: 'Actions' }, ); + +export const ActionsField = withDynamicSchemaProps( + observer((props: LinkageRuleActionGroupProps) => { + return ; + }), + { displayName: 'ActionsField' }, +); diff --git a/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/hooks/useActionOptions.ts b/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/hooks/useActionOptions.ts new file mode 100644 index 0000000000..c78c7d9cbe --- /dev/null +++ b/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/hooks/useActionOptions.ts @@ -0,0 +1,27 @@ +/** + * This file is part of the NocoBase (R) project. + * Copyright (c) 2020-2024 NocoBase Co., Ltd. + * Authors: NocoBase Team. + * + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. + * For more information, please refer to: https://www.nocobase.com/agreement. + */ + +import { useEvent } from '../../../hooks/useEvent'; + +export function useActionOptions() { + const { definitions } = useEvent(); + + let treeData = definitions?.map((module) => ({ + value: module.name, + label: module.title + ' - ' + module.uid, + children: + module?.actions?.map((event) => ({ + value: `${module.name}.${event.name}${module.uid ? `.${module.uid}` : ''}`, + label: event.title, + })) || [], + })); + treeData = treeData?.filter((item) => item.children.length > 0); + + return treeData; +} diff --git a/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/hooks/useActionParamOptions.ts b/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/hooks/useActionParamOptions.ts new file mode 100644 index 0000000000..c78c7d9cbe --- /dev/null +++ b/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/hooks/useActionParamOptions.ts @@ -0,0 +1,27 @@ +/** + * This file is part of the NocoBase (R) project. + * Copyright (c) 2020-2024 NocoBase Co., Ltd. + * Authors: NocoBase Team. + * + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. + * For more information, please refer to: https://www.nocobase.com/agreement. + */ + +import { useEvent } from '../../../hooks/useEvent'; + +export function useActionOptions() { + const { definitions } = useEvent(); + + let treeData = definitions?.map((module) => ({ + value: module.name, + label: module.title + ' - ' + module.uid, + children: + module?.actions?.map((event) => ({ + value: `${module.name}.${event.name}${module.uid ? `.${module.uid}` : ''}`, + label: event.title, + })) || [], + })); + treeData = treeData?.filter((item) => item.children.length > 0); + + return treeData; +} diff --git a/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/index.tsx b/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/index.tsx index fa1d4cef0f..4b39a35a05 100644 --- a/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/index.tsx +++ b/packages/core/client/src/event-flow/EventSettingItem/ActionsSetting/index.tsx @@ -20,7 +20,7 @@ import { SubFormProvider } from '../../../schema-component/antd/association-fiel import { DynamicComponentProps } from '../../../schema-component/antd/filter/DynamicComponent'; import { FilterContext } from '../../../schema-component/antd/filter/context'; import { VariableInput, getShouldChange } from '../../../schema-settings/VariableInput/VariableInput'; -import { Actions } from './Actions'; +import { ActionsField } from './Actions'; import { FormProvider, createSchemaField } from '@formily/react'; import { ArrayCollapse } from '../components/LinkageHeader'; import { Filter } from '../Filter'; @@ -126,7 +126,6 @@ export const ActionsSetting = withDynamicSchemaProps( getAllCollectionsInheritChain, })} returnScope={(scope) => { - // console.log('scope', [...scope, ...variableOptions]); return uniqBy([...scope, ...variableOptions], 'key'); }} /> @@ -140,7 +139,7 @@ export const ActionsSetting = withDynamicSchemaProps( }, actions: { type: 'void', - 'x-component': (_props) => , + 'x-component': ActionsField, }, }, },