From 12f95810d045a5fbb2b95300735902bdf8650d3c Mon Sep 17 00:00:00 2001 From: katherinehhh Date: Mon, 17 Apr 2023 17:19:54 +0800 Subject: [PATCH] fix(linkage rule):multiple select condition judgment failed (#1715) * fix: linkageCollectionFilterOptions * fix: multiple select condition judgment failed * fix: multiple select condition judgment failed * fix: multiple select condition judgment failed --- .../src/collection-manager/action-hooks.ts | 54 +++++++++++++++++++ .../client/src/collection-manager/index.tsx | 2 +- .../schema-component/antd/form-v2/Form.tsx | 1 - .../schema-component/common/utils/logic.js | 24 +++++++++ .../src/schema-settings/SchemaSettings.tsx | 7 +-- 5 files changed, 81 insertions(+), 7 deletions(-) diff --git a/packages/core/client/src/collection-manager/action-hooks.ts b/packages/core/client/src/collection-manager/action-hooks.ts index 0bac4bcfa..9942b9ad4 100644 --- a/packages/core/client/src/collection-manager/action-hooks.ts +++ b/packages/core/client/src/collection-manager/action-hooks.ts @@ -200,6 +200,60 @@ export const useCollectionFilterOptions = (collectionName: string) => { return options; }; +export const useLinkageCollectionFilterOptions = (collectionName: string) => { + const { getCollectionFields, getInterface } = useCollectionManager(); + const fields = getCollectionFields(collectionName).filter((v)=>!['o2m', 'm2m'].includes(v.interface)) + const field2option = (field, depth) => { + if (!field.interface) { + return; + } + const fieldInterface = getInterface(field.interface); + if (!fieldInterface?.filterable) { + return; + } + const { nested, children, operators } = fieldInterface.filterable; + const option = { + name: field.name, + title: field?.uiSchema?.title || field.name, + schema: field?.uiSchema, + operators: + operators?.filter?.((operator) => { + return !operator?.visible || operator.visible(field); + }) || [], + interface: field.interface, + }; + if (field.target && depth > 2) { + return; + } + if (depth > 2) { + return option; + } + if (children?.length) { + option['children'] = children; + } + if (nested) { + const targetFields = getCollectionFields(field.target).filter((v)=>!['o2m', 'm2m'].includes(v.interface)) + const options = getOptions(targetFields, depth + 1).filter(Boolean); + option['children'] = option['children'] || []; + option['children'].push(...options); + } + return option; + }; + const getOptions = (fields, depth) => { + const options = []; + fields.forEach((field) => { + const option = field2option(field, depth); + if (option) { + options.push(option); + } + }); + return options; + }; + const options = getOptions(fields, 1); + return options; +}; + + export const useFilterDataSource = (options) => { const { name } = useCollection(); const data = useCollectionFilterOptions(name); diff --git a/packages/core/client/src/collection-manager/index.tsx b/packages/core/client/src/collection-manager/index.tsx index d227b5152..00c0066ad 100644 --- a/packages/core/client/src/collection-manager/index.tsx +++ b/packages/core/client/src/collection-manager/index.tsx @@ -1,4 +1,4 @@ -export { useCollectionFilterOptions, useSortFields } from './action-hooks'; +export { useCollectionFilterOptions, useSortFields, useLinkageCollectionFilterOptions } from './action-hooks'; export * from './CollectionField'; export * from './CollectionFieldProvider'; export * from './CollectionManagerProvider'; diff --git a/packages/core/client/src/schema-component/antd/form-v2/Form.tsx b/packages/core/client/src/schema-component/antd/form-v2/Form.tsx index 49bdad5bc..992b4bd2a 100644 --- a/packages/core/client/src/schema-component/antd/form-v2/Form.tsx +++ b/packages/core/client/src/schema-component/antd/form-v2/Form.tsx @@ -58,7 +58,6 @@ const FormDecorator: React.FC = (props) => { const getLinkageRules = (fieldSchema) => { let linkageRules = null; fieldSchema.mapProperties((schema) => { - console.log(schema); if (schema['x-linkage-rules']) { linkageRules = schema['x-linkage-rules']; } diff --git a/packages/core/client/src/schema-component/common/utils/logic.js b/packages/core/client/src/schema-component/common/utils/logic.js index fd3f84a61..f498710bf 100644 --- a/packages/core/client/src/schema-component/common/utils/logic.js +++ b/packages/core/client/src/schema-component/common/utils/logic.js @@ -100,6 +100,30 @@ http://ricostacruz.com/cheatsheets/umdjs.html if (!a || typeof a.indexOf === 'undefined') return false; return !(a.indexOf(b) !== -1); }, + $anyOf: function (a, b) { + if (a.length === 0) { + return false; + } + return b.every((item) => a.includes(item)); + }, + $noneOf: function (a, b) { + if (a.length === 0) { + return true; + } + return b.some((item) => !a.includes(item)); + }, + $notMatch:function(a,b){ + if (a.length !== b.length) { + return true; + } + + for (let i = 0; i < a.length; i++) { + if (a[i] !== b[i]) { + return true; + } + } + return false; + }, $isTruly: function (a) { return a === true || a === 1; }, diff --git a/packages/core/client/src/schema-settings/SchemaSettings.tsx b/packages/core/client/src/schema-settings/SchemaSettings.tsx index 16ed49259..b978586ab 100644 --- a/packages/core/client/src/schema-settings/SchemaSettings.tsx +++ b/packages/core/client/src/schema-settings/SchemaSettings.tsx @@ -35,7 +35,7 @@ import { findFormBlock, useAPIClient, useCollection, - useCollectionFilterOptions, + useLinkageCollectionFilterOptions, useCollectionManager, useCompile, useDesignable, @@ -965,9 +965,7 @@ SchemaSettings.LinkageRules = (props) => { 'x-component': FormLinkageRules, 'x-component-props': { useProps: () => { - const options = useCollectionFilterOptions(collectionName).filter( - (v) => !['o2m', 'm2m'].includes(v.interface), - ); + const options = useLinkageCollectionFilterOptions(collectionName); return { options, defaultValues: gridSchema?.['x-linkage-rules'] || fieldSchema?.['x-linkage-rules'], @@ -1062,7 +1060,6 @@ SchemaSettings.DataTemplates = (props) => { SchemaSettings.EnableChildCollections = (props) => { const { collectionName } = props; const fieldSchema = useFieldSchema(); - console.log(fieldSchema); const { dn } = useDesignable(); const { t } = useTranslation(); const allowAddToCurrent = fieldSchema?.['x-allow-add-to-current'];