mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 13:39:24 +08:00
refactor: simplify SchemaInitializerItemGroup and update menu item handling
This commit is contained in:
parent
c10579e57f
commit
4f29541598
@ -15,7 +15,6 @@ import { SchemaInitializerOptions } from '../types';
|
|||||||
import { SchemaInitializerChildren } from './SchemaInitializerChildren';
|
import { SchemaInitializerChildren } from './SchemaInitializerChildren';
|
||||||
import { SchemaInitializerDivider } from './SchemaInitializerDivider';
|
import { SchemaInitializerDivider } from './SchemaInitializerDivider';
|
||||||
import { useSchemaInitializerStyles } from './style';
|
import { useSchemaInitializerStyles } from './style';
|
||||||
import { useMenuSearch } from './SchemaInitializerItemSearchFields';
|
|
||||||
export interface SchemaInitializerItemGroupProps {
|
export interface SchemaInitializerItemGroupProps {
|
||||||
title: string;
|
title: string;
|
||||||
children?: SchemaInitializerOptions['items'];
|
children?: SchemaInitializerOptions['items'];
|
||||||
@ -47,11 +46,5 @@ export const SchemaInitializerItemGroup: FC<SchemaInitializerItemGroupProps> = (
|
|||||||
|
|
||||||
export const SchemaInitializerItemGroupInternal = () => {
|
export const SchemaInitializerItemGroupInternal = () => {
|
||||||
const itemConfig: any = useSchemaInitializerItem<SchemaInitializerItemGroupProps>();
|
const itemConfig: any = useSchemaInitializerItem<SchemaInitializerItemGroupProps>();
|
||||||
|
return <SchemaInitializerItemGroup {...itemConfig} />;
|
||||||
const searchedChildren = useMenuSearch(itemConfig);
|
|
||||||
if (itemConfig.name && itemConfig.name !== 'displayFields') {
|
|
||||||
return <SchemaInitializerItemGroup {...itemConfig} />;
|
|
||||||
}
|
|
||||||
/* eslint-disable react/no-children-prop */
|
|
||||||
return <SchemaInitializerItemGroup {...itemConfig} children={searchedChildren} />;
|
|
||||||
};
|
};
|
||||||
|
@ -68,12 +68,17 @@ export function useGetSchemaInitializerMenuItems(onClick?: (args: any) => void)
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (item.type === 'subMenu') {
|
if (item.type === 'subMenu') {
|
||||||
const label = compiledTitle;
|
|
||||||
const key = item.key || item.name || `${parentKey}-sub-menu-${indexA}`;
|
const key = item.key || item.name || `${parentKey}-sub-menu-${indexA}`;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
key,
|
key,
|
||||||
label,
|
label: <SchemaInitializerChild {...item} title={compiledTitle} />,
|
||||||
children: item?.children?.length ? getMenuItems(item.children, key) : [],
|
// 子菜单的内容已经完全交给 SchemaInitializerChild 处理了,所以这些属性不需要传递下去。但是为了兼容旧版的代码,这里还暂时保留。
|
||||||
|
item_deprecated: {
|
||||||
|
key,
|
||||||
|
label: compiledTitle,
|
||||||
|
children: item?.children?.length ? getMenuItems(item.children, key) : [],
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (item.isMenuType) {
|
if (item.isMenuType) {
|
||||||
|
@ -147,22 +147,30 @@ export function useMenuSearch({
|
|||||||
const currentItems = useMemo(() => {
|
const currentItems = useMemo(() => {
|
||||||
if (isMuliSource) {
|
if (isMuliSource) {
|
||||||
if (!openKey) return [];
|
if (!openKey) return [];
|
||||||
return data.find((item) => (item.key || item.name) === openKey)?.children || [];
|
let result = data.find((item) => (item.key || item.name) === openKey);
|
||||||
|
result = result.item_deprecated || result;
|
||||||
|
|
||||||
|
return result?.children || [];
|
||||||
}
|
}
|
||||||
return data[0]?.children || [];
|
|
||||||
|
let result = data[0];
|
||||||
|
result = result.item_deprecated || result;
|
||||||
|
return result?.children || [];
|
||||||
}, [data, isMuliSource, openKey]);
|
}, [data, isMuliSource, openKey]);
|
||||||
|
|
||||||
// 根据搜索的值进行处理
|
// 根据搜索的值进行处理
|
||||||
const searchedItems = useMemo(() => {
|
const searchedItems = useMemo(() => {
|
||||||
if (!searchValue) return currentItems;
|
if (!searchValue) return currentItems;
|
||||||
const lowerSearchValue = searchValue.toLocaleLowerCase();
|
const lowerSearchValue = searchValue.toLocaleLowerCase();
|
||||||
return currentItems.filter(
|
return currentItems.filter((item) => {
|
||||||
(item) =>
|
item = item.item_deprecated || item;
|
||||||
|
return (
|
||||||
(item.label || item.title) &&
|
(item.label || item.title) &&
|
||||||
String(item.label || item.title)
|
String(item.label || item.title)
|
||||||
.toLocaleLowerCase()
|
.toLocaleLowerCase()
|
||||||
.includes(lowerSearchValue),
|
.includes(lowerSearchValue)
|
||||||
);
|
);
|
||||||
|
});
|
||||||
}, [searchValue, currentItems]);
|
}, [searchValue, currentItems]);
|
||||||
|
|
||||||
const shouldLoadMore = useMemo(() => searchedItems.length > count, [count, searchedItems]);
|
const shouldLoadMore = useMemo(() => searchedItems.length > count, [count, searchedItems]);
|
||||||
@ -248,6 +256,8 @@ export function useMenuSearch({
|
|||||||
const res = useMemo(() => {
|
const res = useMemo(() => {
|
||||||
if (!isMuliSource) return resultItems;
|
if (!isMuliSource) return resultItems;
|
||||||
return data.map((item) => {
|
return data.map((item) => {
|
||||||
|
item = item.item_deprecated || item;
|
||||||
|
|
||||||
if (openKey && item.key === openKey) {
|
if (openKey && item.key === openKey) {
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
|
@ -219,92 +219,98 @@ function getGroupItemForTable({
|
|||||||
depth: number;
|
depth: number;
|
||||||
t: any;
|
t: any;
|
||||||
}) {
|
}) {
|
||||||
const subFields = getCollectionFields(field.target);
|
const getChildren = () => {
|
||||||
const items = subFields
|
const subFields = getCollectionFields(field.target);
|
||||||
?.filter(
|
const items = subFields
|
||||||
(subField) => subField?.interface && !['subTable'].includes(subField?.interface) && !subField?.treeChildren,
|
?.filter(
|
||||||
)
|
(subField) => subField?.interface && !['subTable'].includes(subField?.interface) && !subField?.treeChildren,
|
||||||
?.map((subField) => {
|
)
|
||||||
const interfaceConfig = getInterface(subField.interface);
|
?.map((subField) => {
|
||||||
const newSchemaName = `${schemaName}.${subField.name}`;
|
const interfaceConfig = getInterface(subField.interface);
|
||||||
const schema = {
|
const newSchemaName = `${schemaName}.${subField.name}`;
|
||||||
// type: 'string',
|
const schema = {
|
||||||
name: newSchemaName,
|
// type: 'string',
|
||||||
// title: subField?.uiSchema?.title || subField.name,
|
name: newSchemaName,
|
||||||
'x-component': 'CollectionField',
|
// title: subField?.uiSchema?.title || subField.name,
|
||||||
'x-read-pretty': true,
|
'x-component': 'CollectionField',
|
||||||
'x-collection-field': `${field.target}.${subField.name}`,
|
'x-read-pretty': true,
|
||||||
'x-component-props': {},
|
'x-collection-field': `${field.target}.${subField.name}`,
|
||||||
};
|
'x-component-props': {},
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: 'item',
|
type: 'item',
|
||||||
key: `${field.target}_${subField.name}_${newSchemaName}`,
|
key: `${field.target}_${subField.name}_${newSchemaName}`,
|
||||||
name: newSchemaName,
|
name: newSchemaName,
|
||||||
title: subField?.uiSchema?.title || subField.name,
|
title: subField?.uiSchema?.title || subField.name,
|
||||||
Component: 'TableCollectionFieldInitializer',
|
Component: 'TableCollectionFieldInitializer',
|
||||||
find: findTableColumn,
|
find: findTableColumn,
|
||||||
remove: removeTableColumn,
|
remove: removeTableColumn,
|
||||||
schemaInitialize: (s) => {
|
schemaInitialize: (s) => {
|
||||||
interfaceConfig?.schemaInitialize?.(s, {
|
interfaceConfig?.schemaInitialize?.(s, {
|
||||||
field: subField,
|
field: subField,
|
||||||
readPretty: true,
|
readPretty: true,
|
||||||
block: 'Table',
|
block: 'Table',
|
||||||
targetCollection: getCollection(field.target),
|
targetCollection: getCollection(field.target),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
field: subField,
|
|
||||||
schema,
|
|
||||||
} as SchemaInitializerItemType;
|
|
||||||
});
|
|
||||||
|
|
||||||
const displayCollectionFields = {
|
|
||||||
type: 'itemGroup',
|
|
||||||
key: `${field.target}_${schemaName}_displayFields`,
|
|
||||||
name: `${schemaName}-displayCollectionFields`,
|
|
||||||
title: t('Display fields'),
|
|
||||||
children: items,
|
|
||||||
};
|
|
||||||
|
|
||||||
const children = [displayCollectionFields];
|
|
||||||
|
|
||||||
if (depth < maxDepth) {
|
|
||||||
const subChildren = subFields
|
|
||||||
?.filter((subField) => {
|
|
||||||
return ['o2o', 'oho', 'obo', 'm2o'].includes(subField.interface);
|
|
||||||
})
|
|
||||||
.map((subField) => {
|
|
||||||
return getGroupItemForTable({
|
|
||||||
getCollectionFields,
|
|
||||||
field: subField,
|
field: subField,
|
||||||
getInterface,
|
schema,
|
||||||
getCollection,
|
} as SchemaInitializerItemType;
|
||||||
schemaName: `${schemaName}.${subField.name}`,
|
|
||||||
maxDepth,
|
|
||||||
depth: depth + 1,
|
|
||||||
t,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (subChildren.length) {
|
const displayCollectionFields = {
|
||||||
const group: any = {
|
type: 'itemGroup',
|
||||||
type: 'itemGroup',
|
key: `${field.target}_${schemaName}_displayFields`,
|
||||||
key: `${field.target}_${schemaName}_associationFields`,
|
name: `${schemaName}-displayCollectionFields`,
|
||||||
name: `${schemaName}-associationFields`,
|
title: t('Display fields'),
|
||||||
title: t('Display association fields'),
|
children: items,
|
||||||
children: subChildren,
|
};
|
||||||
};
|
|
||||||
|
|
||||||
children.push(group);
|
const children = [displayCollectionFields];
|
||||||
|
|
||||||
|
if (depth < maxDepth) {
|
||||||
|
const subChildren = subFields
|
||||||
|
?.filter((subField) => {
|
||||||
|
return ['o2o', 'oho', 'obo', 'm2o'].includes(subField.interface);
|
||||||
|
})
|
||||||
|
.map((subField) => {
|
||||||
|
return getGroupItemForTable({
|
||||||
|
getCollectionFields,
|
||||||
|
field: subField,
|
||||||
|
getInterface,
|
||||||
|
getCollection,
|
||||||
|
schemaName: `${schemaName}.${subField.name}`,
|
||||||
|
maxDepth,
|
||||||
|
depth: depth + 1,
|
||||||
|
t,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (subChildren.length) {
|
||||||
|
const group: any = {
|
||||||
|
type: 'itemGroup',
|
||||||
|
key: `${field.target}_${schemaName}_associationFields`,
|
||||||
|
name: `${schemaName}-associationFields`,
|
||||||
|
title: t('Display association fields'),
|
||||||
|
children: subChildren,
|
||||||
|
};
|
||||||
|
|
||||||
|
children.push(group);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
return children;
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: 'subMenu',
|
type: 'subMenu',
|
||||||
key: `${field.target}_${schemaName}_submenu`,
|
key: `${field.target}_${schemaName}_submenu`,
|
||||||
name: schemaName,
|
name: schemaName,
|
||||||
title: field.uiSchema?.title,
|
title: field.uiSchema?.title,
|
||||||
children,
|
useChildren() {
|
||||||
|
return getChildren() || [];
|
||||||
|
},
|
||||||
} as SchemaInitializerItemType;
|
} as SchemaInitializerItemType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user