ruoyi-plus-vben5/packages/business/layouts/src/basic/menu/helper.ts

38 lines
815 B
TypeScript
Raw Normal View History

2024-05-19 21:20:42 +08:00
import type { MenuRecordRaw } from '@vben-core/typings';
function findMenuByPath(
list: MenuRecordRaw[],
path?: string,
): MenuRecordRaw | null {
for (const menu of list) {
if (menu.path === path) {
return menu;
}
if (menu?.children?.length) {
const findMenu = findMenuByPath(menu.children, path);
if (findMenu) {
return findMenu;
}
}
}
return null;
}
/**
*
* @param menus
* @param path
*/
function findRootMenuByPath(menus: MenuRecordRaw[], path?: string) {
const findMenu = findMenuByPath(menus, path);
const rootMenuPath = findMenu?.parents?.[0];
const rootMenu = menus.find((item) => item.path === rootMenuPath);
return {
findMenu,
rootMenu,
rootMenuPath,
};
}
export { findMenuByPath, findRootMenuByPath };