fix(bi): issue of parsing label of region & file field (#2366)

* fix: issue of parsing label of region & file field

* fix: test
This commit is contained in:
YANG QIA 2023-08-01 12:50:43 +08:00 committed by GitHub
parent 0fe5b5367a
commit 7151345c40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 13 deletions

View File

@ -64,6 +64,18 @@ describe('hooks', () => {
},
],
}[name]),
getInterface: (i: string) => {
switch (i) {
case 'm2o':
return {
filterable: {
nested: true,
},
};
default:
return {};
}
},
} as any);
});

View File

@ -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,