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
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
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 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) => {
const { filterCollection, filterDataSource } = options;
return this.getDataSources(filterDataSource).reduce<(DataSourceOptions & { collections: Collection[] })[]>(
(acc, dataSource) => {
acc.push({
...dataSource.getOptions(),
collections: dataSource.collectionManager.getCollections(predicate),
collections: dataSource.collectionManager.getCollections(filterCollection),
});
return acc;
}, []);
},
[],
);
}
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 { 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,
});

View File

@ -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) => {
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);