fix: collectionFieldsOptions cannot get all fields (#1588)

This commit is contained in:
Dunqing 2023-03-21 16:56:05 +08:00 committed by GitHub
parent 50183b065d
commit 0d36caf1c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -83,12 +83,11 @@ export const useCollectionManager = () => {
}; };
// 缓存下面已经获取的 options防止无限循环 // 缓存下面已经获取的 options防止无限循环
const cachedOptions = {};
const getCollectionFieldsOptions = ( const getCollectionFieldsOptions = (
collectionName: string, collectionName: string,
type: string | string[] = 'string', type: string | string[] = 'string',
opts?: { opts?: {
cached?: Record<string, any>;
/** /**
* true * true
* Array<string> * Array<string>
@ -96,12 +95,13 @@ export const useCollectionManager = () => {
association?: boolean | string[]; association?: boolean | string[];
}, },
) => { ) => {
const { association = false, cached = {} } = opts || {};
// 防止无限循环 // 防止无限循环
if (cachedOptions[collectionName]) { if (cached[collectionName]) {
return _.cloneDeep(cachedOptions[collectionName]); return _.cloneDeep(cached[collectionName]);
} }
const { association = false } = opts || {};
if (typeof type === 'string') { if (typeof type === 'string') {
type = [type]; type = [type];
} }
@ -122,7 +122,7 @@ export const useCollectionManager = () => {
...field, ...field,
}; };
if (association && field.target) { if (association && field.target) {
result.children = getCollectionFieldsOptions(field.target, type, opts); result.children = getCollectionFieldsOptions(field.target, type, { ...opts, cached });
if (!result.children?.length) { if (!result.children?.length) {
return null; return null;
} }
@ -132,7 +132,7 @@ export const useCollectionManager = () => {
// 过滤 map 产生为 null 的数据 // 过滤 map 产生为 null 的数据
.filter(Boolean); .filter(Boolean);
cachedOptions[collectionName] = options; cached[collectionName] = options;
return options; return options;
}; };