feat: add data source filter (#3724)

This commit is contained in:
jack zhang 2024-03-15 15:38:33 +08:00 committed by GitHub
parent e877e3b5d9
commit 04ee7b84ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 42 additions and 17 deletions

View File

@ -280,7 +280,10 @@ const MyComponent = () => {
```tsx | pure ```tsx | pure
class DataSourceManager { class DataSourceManager {
getAllCollections(predicate?: (collection: Collection) => boolean): (DataSourceOptions & { collections: Collection[] })[]; getAllCollections(options?: {
filterCollection?: (collection: Collection) => boolean;
filterDataSource?: (dataSource: DataSource) => boolean;
}): (DataSourceOptions & { collections: Collection[] })[];
} }
``` ```

View File

@ -280,7 +280,10 @@ const MyComponent = () => {
```tsx | pure ```tsx | pure
class DataSourceManager { class DataSourceManager {
getAllCollections(predicate?: (collection: Collection) => boolean): (DataSourceOptions & { collections: Collection[] })[]; getAllCollections(options: {
filterCollection?: (collection: Collection) => boolean;
filterDataSource?: (dataSource: DataSource) => boolean;
}): (DataSourceOptions & { collections: Collection[] })[];
} }
``` ```

View File

@ -4,6 +4,7 @@ import type { Application } from '../../application/Application';
import { type DataSourceOptions, DataSource, LocalDataSource, DataSourceFactory } from './DataSource'; import { type DataSourceOptions, DataSource, LocalDataSource, DataSourceFactory } from './DataSource';
import { type CollectionTemplateFactory, CollectionTemplateManager } from '../collection-template'; import { type CollectionTemplateFactory, CollectionTemplateManager } from '../collection-template';
import { type CollectionFieldInterfaceFactory, CollectionFieldInterfaceManager } from '../collection-field-interface'; import { type CollectionFieldInterfaceFactory, CollectionFieldInterfaceManager } from '../collection-field-interface';
import _ from 'lodash';
export const DEFAULT_DATA_SOURCE_KEY = 'main'; export const DEFAULT_DATA_SOURCE_KEY = 'main';
export const DEFAULT_DATA_SOURCE_TITLE = '{{t("Main")}}'; export const DEFAULT_DATA_SOURCE_TITLE = '{{t("Main")}}';
@ -55,8 +56,9 @@ export class DataSourceManager {
this.getDataSources().forEach((dataSource) => dataSource.collectionManager.reAddCollections()); this.getDataSources().forEach((dataSource) => dataSource.collectionManager.reAddCollections());
} }
getDataSources() { getDataSources(filterCollection?: (dataSource: DataSource) => boolean) {
return Object.values(this.dataSourceInstancesMap); const allDataSources = Object.values(this.dataSourceInstancesMap);
return filterCollection ? _.filter(allDataSources, filterCollection) : allDataSources;
} }
getDataSource(key?: string) { getDataSource(key?: string) {
@ -81,15 +83,22 @@ export class DataSourceManager {
} }
getAllCollections( getAllCollections(
predicate?: (collection: Collection) => boolean, options: {
filterCollection?: (collection: Collection) => boolean;
filterDataSource?: (dataSource: DataSource) => boolean;
} = {},
): (DataSourceOptions & { collections: Collection[] })[] { ): (DataSourceOptions & { collections: Collection[] })[] {
return this.getDataSources().reduce<(DataSourceOptions & { collections: Collection[] })[]>((acc, dataSource) => { const { filterCollection, filterDataSource } = options;
return this.getDataSources(filterDataSource).reduce<(DataSourceOptions & { collections: Collection[] })[]>(
(acc, dataSource) => {
acc.push({ acc.push({
...dataSource.getOptions(), ...dataSource.getOptions(),
collections: dataSource.collectionManager.getCollections(predicate), collections: dataSource.collectionManager.getCollections(filterCollection),
}); });
return acc; return acc;
}, []); },
[],
);
} }
addFieldInterfaceGroups(options: Record<string, { label: string; order?: number }>) { addFieldInterfaceGroups(options: Record<string, { label: string; order?: number }>) {

View File

@ -12,6 +12,7 @@ import { Collection, CollectionFieldOptions } from '../../data-source/collection
import { useCompile } from '../../schema-component'; import { useCompile } from '../../schema-component';
import { useSchemaTemplateManager } from '../../schema-templates'; import { useSchemaTemplateManager } from '../../schema-templates';
import { useCollectionDataSourceItems } from '../utils'; import { useCollectionDataSourceItems } from '../utils';
import { DataSource } from '../../data-source';
const MENU_ITEM_HEIGHT = 40; const MENU_ITEM_HEIGHT = 40;
const STEP = 15; const STEP = 15;
@ -269,6 +270,7 @@ export interface DataBlockInitializerProps {
name: string; name: string;
title: string; title: string;
filter?: (options: { collection: Collection; associationField: CollectionFieldOptions }) => boolean; filter?: (options: { collection: Collection; associationField: CollectionFieldOptions }) => boolean;
filterDataSource?: (dataSource: DataSource) => boolean;
componentType: string; componentType: string;
onlyCurrentDataSource?: boolean; onlyCurrentDataSource?: boolean;
hideSearch?: boolean; hideSearch?: boolean;
@ -290,6 +292,7 @@ export const DataBlockInitializer = (props: DataBlockInitializerProps) => {
hideSearch, hideSearch,
showAssociationFields, showAssociationFields,
showChildren, showChildren,
filterDataSource,
} = props; } = props;
const { insert, setVisible } = useSchemaInitializer(); const { insert, setVisible } = useSchemaInitializer();
const compile = useCompile(); const compile = useCompile();
@ -311,6 +314,7 @@ export const DataBlockInitializer = (props: DataBlockInitializerProps) => {
const items = useCollectionDataSourceItems({ const items = useCollectionDataSourceItems({
componentName: componentType, componentName: componentType,
filter, filter,
filterDataSource,
onlyCurrentDataSource, onlyCurrentDataSource,
showAssociationFields, showAssociationFields,
}); });

View File

@ -4,6 +4,7 @@ import { uid } from '@formily/shared';
import { useMemo } from 'react'; import { useMemo } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { import {
DataSource,
SchemaInitializerItemType, SchemaInitializerItemType,
useCollection, useCollection,
useCollectionManager, useCollectionManager,
@ -838,11 +839,13 @@ export const useCollectionDataSourceItems = ({
filter = () => true, filter = () => true,
onlyCurrentDataSource = false, onlyCurrentDataSource = false,
showAssociationFields, showAssociationFields,
filterDataSource,
}: { }: {
componentName; componentName;
filter?: (options: { collection?: Collection; associationField?: CollectionFieldOptions }) => boolean; filter?: (options: { collection?: Collection; associationField?: CollectionFieldOptions }) => boolean;
onlyCurrentDataSource?: boolean; onlyCurrentDataSource?: boolean;
showAssociationFields?: boolean; showAssociationFields?: boolean;
filterDataSource?: (dataSource?: DataSource) => boolean;
}) => { }) => {
const { t } = useTranslation(); const { t } = useTranslation();
const dm = useDataSourceManager(); const dm = useDataSourceManager();
@ -850,11 +853,14 @@ export const useCollectionDataSourceItems = ({
const collection = useCollection(); const collection = useCollection();
const associationFields = useAssociationFields({ componentName, filterCollections: filter, showAssociationFields }); const associationFields = useAssociationFields({ componentName, filterCollections: filter, showAssociationFields });
let allCollections = dm.getAllCollections((collection) => { let allCollections = dm.getAllCollections({
filterCollection: (collection) => {
if (onlyCurrentDataSource && collection.dataSource !== dataSourceKey) { if (onlyCurrentDataSource && collection.dataSource !== dataSourceKey) {
return false; return false;
} }
return filter({ collection }); return filter({ collection });
},
filterDataSource,
}); });
if (onlyCurrentDataSource) { if (onlyCurrentDataSource) {
allCollections = allCollections.filter((collection) => collection.key === dataSourceKey); allCollections = allCollections.filter((collection) => collection.key === dataSourceKey);