import type { MenuProps } from 'antd';
|
import type { AppRouteItem } from '@/router/routeConfig';
|
|
type MenuItem = Required<MenuProps>['items'][number];
|
|
/** 把路由树转换成 antd Menu 需要的结构。 */
|
export const buildMenuItems = (routes: AppRouteItem[]): MenuItem[] => {
|
const items: MenuItem[] = [];
|
|
for (const route of routes) {
|
if (route.hideInMenu) continue;
|
|
const children = route.children ? buildMenuItems(route.children) : [];
|
|
items.push({
|
key: route.path,
|
icon: route.icon,
|
label: route.title,
|
...(children.length > 0 ? { children } : {}),
|
} as MenuItem);
|
}
|
|
return items;
|
};
|
|
/** 根据当前地址推导需要展开的父级菜单 key。 */
|
export const getOpenKeys = (pathname: string): string[] => {
|
const segments = pathname.split('/').filter(Boolean);
|
return segments.slice(0, -1).map((_, index) => `/${segments.slice(0, index + 1).join('/')}`);
|
};
|