diff --git a/packages/core/client/src/block-provider/FormBlockProvider.tsx b/packages/core/client/src/block-provider/FormBlockProvider.tsx index 817d700d7..e23745775 100644 --- a/packages/core/client/src/block-provider/FormBlockProvider.tsx +++ b/packages/core/client/src/block-provider/FormBlockProvider.tsx @@ -49,7 +49,7 @@ const InternalFormBlockProvider = (props) => { ); }; -const useIsEmptyRecord = () => { +export const useIsEmptyRecord = () => { const record = useRecord(); const keys = Object.keys(record); if (keys.includes('__parent')) { diff --git a/packages/core/client/src/schema-component/antd/action/Action.Designer.tsx b/packages/core/client/src/schema-component/antd/action/Action.Designer.tsx index 0b4c2f2cf..fa932ab2b 100644 --- a/packages/core/client/src/schema-component/antd/action/Action.Designer.tsx +++ b/packages/core/client/src/schema-component/antd/action/Action.Designer.tsx @@ -4,11 +4,10 @@ import { Menu } from 'antd'; import React, { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useDesignable } from '../..'; -import { useFormBlockContext } from '../../../block-provider/FormBlockProvider'; import { useCollection, useCollectionManager } from '../../../collection-manager'; -import { useRecord } from '../../../record-provider'; import { OpenModeSchemaItems } from '../../../schema-items'; import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings'; +import { useLinkageAction } from './hooks'; import { requestSettingsSchema } from './utils'; @@ -49,10 +48,7 @@ export const ActionDesigner = (props) => { const isUpdateModePopupAction = ['customize:bulkUpdate', 'customize:bulkEdit'].includes(fieldSchema['x-action']); const [initialSchema, setInitialSchema] = useState(); const actionType = fieldSchema['x-action'] ?? ''; - const isLinkageAction = - linkageAction || - (Object.keys(useFormBlockContext()).length > 0 && Object.keys(useRecord()).length > 0) || - fieldSchema?.parent?.['x-initializer'] === 'DetailsActionInitializers'; + const isLinkageAction = linkageAction || useLinkageAction(); const isChildCollectionAction = getChildrenCollections(name).length > 0 && fieldSchema['x-action'] === 'create'; const isSupportEditButton = fieldSchema['x-action'] !== 'expandAll'; const isLink = fieldSchema['x-component'] === 'Action.Link'; diff --git a/packages/core/client/src/schema-component/antd/action/hooks.ts b/packages/core/client/src/schema-component/antd/action/hooks.ts index 45d3220a3..607993792 100644 --- a/packages/core/client/src/schema-component/antd/action/hooks.ts +++ b/packages/core/client/src/schema-component/antd/action/hooks.ts @@ -1,8 +1,9 @@ -import { useForm } from '@formily/react'; +import { useForm, useFieldSchema } from '@formily/react'; import { Modal as AntdModal } from 'antd'; import { useContext } from 'react'; import { useTranslation } from 'react-i18next'; import { ActionContext } from './context'; +import { useIsEmptyRecord } from '../../../block-provider/FormBlockProvider'; export const useA = () => { return { @@ -51,3 +52,10 @@ export const useCloseAction = () => { }, }; }; + +export const useLinkageAction = () => { + const fieldSchema = useFieldSchema(); + const isRecordAction = useIsEmptyRecord(); + const isAction = ['Action.Link', 'Action'].includes(fieldSchema['x-component']); + return isAction && isRecordAction; +}; diff --git a/packages/core/client/src/schema-component/antd/action/utils.ts b/packages/core/client/src/schema-component/antd/action/utils.ts index b810c29f4..acd91e971 100644 --- a/packages/core/client/src/schema-component/antd/action/utils.ts +++ b/packages/core/client/src/schema-component/antd/action/utils.ts @@ -81,7 +81,7 @@ export const linkageAction = (operator, field, condition, values) => { } field.linkageProperty = { ...field.linkageProperty, - required: displayResult, + display: displayResult, }; field.display = last(displayResult); break; @@ -100,7 +100,7 @@ export const linkageAction = (operator, field, condition, values) => { } field.linkageProperty = { ...field.linkageProperty, - required: disableResult, + disabled: disableResult, }; field.disabled = last(disableResult); break; @@ -110,7 +110,7 @@ export const linkageAction = (operator, field, condition, values) => { } field.linkageProperty = { ...field.linkageProperty, - required: disableResult, + disabled: disableResult, }; field.disabled = last(disableResult); break; diff --git a/packages/core/client/src/schema-initializer/components/CreateRecordAction.tsx b/packages/core/client/src/schema-initializer/components/CreateRecordAction.tsx index 2bf5388d5..c65310132 100644 --- a/packages/core/client/src/schema-initializer/components/CreateRecordAction.tsx +++ b/packages/core/client/src/schema-initializer/components/CreateRecordAction.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { DownOutlined, PlusOutlined } from '@ant-design/icons'; import { RecursionField, useFieldSchema, useField } from '@formily/react'; import { Dropdown, Menu, Button } from 'antd'; @@ -7,6 +7,9 @@ import { observer } from '@formily/react'; import { useCollectionManager, useCollection, CollectionProvider } from '../../collection-manager'; import { ActionContext, useCompile, useActionContext } from '../../schema-component'; import { useRecordPkValue, useACLRolesCheck } from '../../acl/ACLProvider'; +import { useRecord } from '../../record-provider'; +import { useDesignable } from '../../'; +import { linkageAction } from '../../schema-component/antd/action/utils'; export const actionDesignerCss = css` position: relative; @@ -83,7 +86,7 @@ export const CreateRecordAction = observer((props) => { const collection = useCollection(); const fieldSchema = useFieldSchema(); const enableChildren = fieldSchema['x-enable-children'] || []; - const field = useField(); + const field: any = useField(); const componentType = field.componentProps.type || 'primary'; const { getChildrenCollections } = useCollectionManager(); const totalChildCollections = getChildrenCollections(collection.name); @@ -105,8 +108,11 @@ export const CreateRecordAction = observer((props) => { return v && actionAclCheck(`${v.name}:create`); }); const [currentCollection, setCurrentCollection] = useState(collection.name); + const linkageRules = fieldSchema?.['x-linkage-rules'] || []; + const values = useRecord(); const ctx = useActionContext(); const compile = useCompile(); + const { designable } = useDesignable(); const menu = ( {inheritsCollections.map((option) => { @@ -124,6 +130,16 @@ export const CreateRecordAction = observer((props) => { })} ); + useEffect(() => { + field.linkageProperty = {}; + linkageRules + .filter((k) => !k.disabled) + .map((v) => { + return v.actions?.map((h) => { + linkageAction(h.operator, field, v.condition, values); + }); + }); + }, [linkageRules, values]); return (
@@ -147,12 +163,17 @@ export const CreateRecordAction = observer((props) => { ) : ( @@ -165,10 +186,3 @@ export const CreateRecordAction = observer((props) => { ); }); -// export const CreateRecordAction = observer((props: any) => { -// return ( -// -// {props.children} -// -// ); -// });