diff --git a/packages/plugins/@hera/plugin-core/src/client/schema-settings/index.tsx b/packages/plugins/@hera/plugin-core/src/client/schema-settings/index.tsx index 81e4dafb3..ed486e579 100644 --- a/packages/plugins/@hera/plugin-core/src/client/schema-settings/index.tsx +++ b/packages/plugins/@hera/plugin-core/src/client/schema-settings/index.tsx @@ -397,6 +397,59 @@ const findGridSchema = (fieldSchema) => { }, null); }; +export const useCollectionFilterOptions = (collectionName: string) => { + const { getCollectionFields, getInterface } = useCollectionManager_deprecated(); + const fields = getCollectionFields(collectionName); + 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); + 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 SetFilterScope = (props) => { const { t } = useTranslation(); const fieldSchema = useFieldSchema(); @@ -419,7 +472,7 @@ export const SetFilterScope = (props) => { 'x-component': FormFilterScope, 'x-component-props': { useProps: () => { - const options = useLinkageCollectionFilterOptions(collectionName); + const options = useCollectionFilterOptions(collectionName); return { options, defaultValues: gridSchema?.['x-filter-rules'] || fieldSchema?.['x-filter-rules'],