From 7151345c40751280f0a7c5ddf1794baddf6c1ede Mon Sep 17 00:00:00 2001 From: YANG QIA <2013xile@gmail.com> Date: Tue, 1 Aug 2023 12:50:43 +0800 Subject: [PATCH] fix(bi): issue of parsing label of region & file field (#2366) * fix: issue of parsing label of region & file field * fix: test --- .../src/client/__tests__/hooks.test.ts | 12 +++++ .../data-visualization/src/client/hooks.ts | 48 ++++++++++++++----- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/packages/plugins/data-visualization/src/client/__tests__/hooks.test.ts b/packages/plugins/data-visualization/src/client/__tests__/hooks.test.ts index 61351bf11..d2db9785b 100644 --- a/packages/plugins/data-visualization/src/client/__tests__/hooks.test.ts +++ b/packages/plugins/data-visualization/src/client/__tests__/hooks.test.ts @@ -64,6 +64,18 @@ describe('hooks', () => { }, ], }[name]), + getInterface: (i: string) => { + switch (i) { + case 'm2o': + return { + filterable: { + nested: true, + }, + }; + default: + return {}; + } + }, } as any); }); diff --git a/packages/plugins/data-visualization/src/client/hooks.ts b/packages/plugins/data-visualization/src/client/hooks.ts index b450d1143..85a609808 100644 --- a/packages/plugins/data-visualization/src/client/hooks.ts +++ b/packages/plugins/data-visualization/src/client/hooks.ts @@ -1,6 +1,6 @@ import { ArrayField } from '@formily/core'; import { ISchema, Schema, useForm } from '@formily/react'; -import { useACLRoleContext, useCollectionManager } from '@nocobase/client'; +import { CollectionFieldOptions, useACLRoleContext, useCollectionManager } from '@nocobase/client'; import { useContext } from 'react'; import { useTranslation } from 'react-i18next'; import { ChartConfigContext } from './block/ChartConfigure'; @@ -23,7 +23,13 @@ export type FieldOption = { targetFields?: FieldOption[]; }; -export const useFields = (collection?: string) => { +export const useFields = ( + collection?: string, +): (CollectionFieldOptions & { + key: string; + label: string; + value: string; +})[] => { const { current } = useContext(ChartConfigContext); if (!collection) { collection = current?.collection || ''; @@ -43,24 +49,40 @@ export const useFields = (collection?: string) => { }; export const useFieldsWithAssociation = (collection?: string) => { - const { getCollectionFields } = useCollectionManager(); + const { getCollectionFields, getInterface } = useCollectionManager(); const { t } = useTranslation(); const fields = useFields(collection); return fields.map((field) => { + const filterable = getInterface(field.interface)?.filterable; const label = Schema.compile(field.uiSchema?.title || field.name, { t }); - if (!field.target) { + if (!(filterable && (filterable?.nested || filterable?.children?.length))) { return { ...field, label }; } - const targetFields = (getCollectionFields(field.target) || []) - .filter((targetField) => { - return targetField.interface; - }) - .map((targetField) => ({ - ...targetField, - key: `${field.name}.${targetField.name}`, - label: `${label} / ${Schema.compile(targetField.uiSchema?.title || targetField.name, { t })}`, - value: `${field.name}.${targetField.name}`, + let targetFields = []; + if (filterable?.nested) { + const nestedFields = (getCollectionFields(field.target) || []) + .filter((targetField) => { + return targetField.interface; + }) + .map((targetField) => ({ + ...targetField, + key: `${field.name}.${targetField.name}`, + label: `${label} / ${Schema.compile(targetField.uiSchema?.title || targetField.name, { t })}`, + value: `${field.name}.${targetField.name}`, + })); + targetFields = [...targetFields, ...nestedFields]; + } + + if (filterable?.children?.length) { + const children = filterable.children.map((child: any) => ({ + ...child, + key: `${field.name}.${child.name}`, + label: `${label} / ${Schema.compile(child.schema?.title || child.title || child.name, { t })}`, + value: `${field.name}.${child.name}`, })); + targetFields = [...targetFields, ...children]; + } + return { ...field, label,