ruoyi-plus-vben5/packages/utils/src/helpers/find-menu-by-path.ts

38 lines
834 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;
}
2024-06-09 15:39:11 +08:00
const findMenu = menu.children && findMenuByPath(menu.children, path);
if (findMenu) {
return findMenu;
2024-05-19 21:20:42 +08:00
}
}
return null;
}
/**
*
* @param menus
* @param path
*/
function findRootMenuByPath(menus: MenuRecordRaw[], path?: string, level = 0) {
2024-05-19 21:20:42 +08:00
const findMenu = findMenuByPath(menus, path);
const rootMenuPath = findMenu?.parents?.[level];
2024-07-05 18:32:53 +08:00
const rootMenu = rootMenuPath
? menus.find((item) => item.path === rootMenuPath)
: undefined;
2024-05-19 21:20:42 +08:00
return {
findMenu,
rootMenu,
rootMenuPath,
};
}
export { findMenuByPath, findRootMenuByPath };