From 04ee7b84ecc6751fe00ff8e72afe752d70da5662 Mon Sep 17 00:00:00 2001 From: jack zhang <1098626505@qq.com> Date: Fri, 15 Mar 2024 15:38:33 +0800 Subject: [PATCH] feat: add data source filter (#3724) --- .../core/data-source/data-source-manager.md | 5 +++- .../core/data-source/data-source-manager.md | 5 +++- .../data-source/DataSourceManager.ts | 29 ++++++++++++------- .../items/DataBlockInitializer.tsx | 4 +++ .../client/src/schema-initializer/utils.ts | 16 ++++++---- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/packages/core/client/docs/en-US/core/data-source/data-source-manager.md b/packages/core/client/docs/en-US/core/data-source/data-source-manager.md index fd4b43c05..198021e29 100644 --- a/packages/core/client/docs/en-US/core/data-source/data-source-manager.md +++ b/packages/core/client/docs/en-US/core/data-source/data-source-manager.md @@ -280,7 +280,10 @@ const MyComponent = () => { ```tsx | pure class DataSourceManager { - getAllCollections(predicate?: (collection: Collection) => boolean): (DataSourceOptions & { collections: Collection[] })[]; + getAllCollections(options?: { + filterCollection?: (collection: Collection) => boolean; + filterDataSource?: (dataSource: DataSource) => boolean; + }): (DataSourceOptions & { collections: Collection[] })[]; } ``` diff --git a/packages/core/client/docs/zh-CN/core/data-source/data-source-manager.md b/packages/core/client/docs/zh-CN/core/data-source/data-source-manager.md index fd4b43c05..7ef124b94 100644 --- a/packages/core/client/docs/zh-CN/core/data-source/data-source-manager.md +++ b/packages/core/client/docs/zh-CN/core/data-source/data-source-manager.md @@ -280,7 +280,10 @@ const MyComponent = () => { ```tsx | pure class DataSourceManager { - getAllCollections(predicate?: (collection: Collection) => boolean): (DataSourceOptions & { collections: Collection[] })[]; + getAllCollections(options: { + filterCollection?: (collection: Collection) => boolean; + filterDataSource?: (dataSource: DataSource) => boolean; + }): (DataSourceOptions & { collections: Collection[] })[]; } ``` diff --git a/packages/core/client/src/data-source/data-source/DataSourceManager.ts b/packages/core/client/src/data-source/data-source/DataSourceManager.ts index a84cc0beb..14b732d12 100644 --- a/packages/core/client/src/data-source/data-source/DataSourceManager.ts +++ b/packages/core/client/src/data-source/data-source/DataSourceManager.ts @@ -4,6 +4,7 @@ import type { Application } from '../../application/Application'; import { type DataSourceOptions, DataSource, LocalDataSource, DataSourceFactory } from './DataSource'; import { type CollectionTemplateFactory, CollectionTemplateManager } from '../collection-template'; import { type CollectionFieldInterfaceFactory, CollectionFieldInterfaceManager } from '../collection-field-interface'; +import _ from 'lodash'; export const DEFAULT_DATA_SOURCE_KEY = 'main'; export const DEFAULT_DATA_SOURCE_TITLE = '{{t("Main")}}'; @@ -55,8 +56,9 @@ export class DataSourceManager { this.getDataSources().forEach((dataSource) => dataSource.collectionManager.reAddCollections()); } - getDataSources() { - return Object.values(this.dataSourceInstancesMap); + getDataSources(filterCollection?: (dataSource: DataSource) => boolean) { + const allDataSources = Object.values(this.dataSourceInstancesMap); + return filterCollection ? _.filter(allDataSources, filterCollection) : allDataSources; } getDataSource(key?: string) { @@ -81,15 +83,22 @@ export class DataSourceManager { } getAllCollections( - predicate?: (collection: Collection) => boolean, + options: { + filterCollection?: (collection: Collection) => boolean; + filterDataSource?: (dataSource: DataSource) => boolean; + } = {}, ): (DataSourceOptions & { collections: Collection[] })[] { - return this.getDataSources().reduce<(DataSourceOptions & { collections: Collection[] })[]>((acc, dataSource) => { - acc.push({ - ...dataSource.getOptions(), - collections: dataSource.collectionManager.getCollections(predicate), - }); - return acc; - }, []); + const { filterCollection, filterDataSource } = options; + return this.getDataSources(filterDataSource).reduce<(DataSourceOptions & { collections: Collection[] })[]>( + (acc, dataSource) => { + acc.push({ + ...dataSource.getOptions(), + collections: dataSource.collectionManager.getCollections(filterCollection), + }); + return acc; + }, + [], + ); } addFieldInterfaceGroups(options: Record) { diff --git a/packages/core/client/src/schema-initializer/items/DataBlockInitializer.tsx b/packages/core/client/src/schema-initializer/items/DataBlockInitializer.tsx index 98dc3c113..e7baad433 100644 --- a/packages/core/client/src/schema-initializer/items/DataBlockInitializer.tsx +++ b/packages/core/client/src/schema-initializer/items/DataBlockInitializer.tsx @@ -12,6 +12,7 @@ import { Collection, CollectionFieldOptions } from '../../data-source/collection import { useCompile } from '../../schema-component'; import { useSchemaTemplateManager } from '../../schema-templates'; import { useCollectionDataSourceItems } from '../utils'; +import { DataSource } from '../../data-source'; const MENU_ITEM_HEIGHT = 40; const STEP = 15; @@ -269,6 +270,7 @@ export interface DataBlockInitializerProps { name: string; title: string; filter?: (options: { collection: Collection; associationField: CollectionFieldOptions }) => boolean; + filterDataSource?: (dataSource: DataSource) => boolean; componentType: string; onlyCurrentDataSource?: boolean; hideSearch?: boolean; @@ -290,6 +292,7 @@ export const DataBlockInitializer = (props: DataBlockInitializerProps) => { hideSearch, showAssociationFields, showChildren, + filterDataSource, } = props; const { insert, setVisible } = useSchemaInitializer(); const compile = useCompile(); @@ -311,6 +314,7 @@ export const DataBlockInitializer = (props: DataBlockInitializerProps) => { const items = useCollectionDataSourceItems({ componentName: componentType, filter, + filterDataSource, onlyCurrentDataSource, showAssociationFields, }); diff --git a/packages/core/client/src/schema-initializer/utils.ts b/packages/core/client/src/schema-initializer/utils.ts index 913780650..9f020252e 100644 --- a/packages/core/client/src/schema-initializer/utils.ts +++ b/packages/core/client/src/schema-initializer/utils.ts @@ -4,6 +4,7 @@ import { uid } from '@formily/shared'; import { useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import { + DataSource, SchemaInitializerItemType, useCollection, useCollectionManager, @@ -838,11 +839,13 @@ export const useCollectionDataSourceItems = ({ filter = () => true, onlyCurrentDataSource = false, showAssociationFields, + filterDataSource, }: { componentName; filter?: (options: { collection?: Collection; associationField?: CollectionFieldOptions }) => boolean; onlyCurrentDataSource?: boolean; showAssociationFields?: boolean; + filterDataSource?: (dataSource?: DataSource) => boolean; }) => { const { t } = useTranslation(); const dm = useDataSourceManager(); @@ -850,11 +853,14 @@ export const useCollectionDataSourceItems = ({ const collection = useCollection(); const associationFields = useAssociationFields({ componentName, filterCollections: filter, showAssociationFields }); - let allCollections = dm.getAllCollections((collection) => { - if (onlyCurrentDataSource && collection.dataSource !== dataSourceKey) { - return false; - } - return filter({ collection }); + let allCollections = dm.getAllCollections({ + filterCollection: (collection) => { + if (onlyCurrentDataSource && collection.dataSource !== dataSourceKey) { + return false; + } + return filter({ collection }); + }, + filterDataSource, }); if (onlyCurrentDataSource) { allCollections = allCollections.filter((collection) => collection.key === dataSourceKey);