2022-06-14 15:01:53 +08:00
|
|
|
import { useFieldSchema } from '@formily/react';
|
|
|
|
import { useCollectionManager } from '@nocobase/client';
|
|
|
|
|
|
|
|
export const useFields = (collectionName: string) => {
|
|
|
|
const fieldSchema = useFieldSchema();
|
|
|
|
const nonfilterable = fieldSchema?.['x-component-props']?.nonfilterable || [];
|
2022-07-13 15:05:46 +08:00
|
|
|
const { getCollectionFields } = useCollectionManager();
|
2022-06-14 15:01:53 +08:00
|
|
|
const fields = getCollectionFields(collectionName);
|
|
|
|
const field2option = (field, depth) => {
|
|
|
|
if (!field.interface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const option = {
|
|
|
|
name: field.name,
|
|
|
|
title: field?.uiSchema?.title || field.name,
|
|
|
|
schema: field?.uiSchema,
|
|
|
|
};
|
2022-07-13 15:05:46 +08:00
|
|
|
if (!field.target || depth >= 3) {
|
2022-06-14 15:01:53 +08:00
|
|
|
return option;
|
|
|
|
}
|
2022-07-13 15:05:46 +08:00
|
|
|
|
|
|
|
if (field.target) {
|
2022-06-14 15:01:53 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
return getOptions(fields, 1);
|
|
|
|
};
|