feat: 修正菜单排序在二级菜单不生效问题 (#7007)

* treeUtil增加对树形结构数据进行递归排序

* 菜单sort排序各级菜单均生效
This commit is contained in:
xueyitt 2025-12-22 19:57:21 +08:00 committed by GitHub
parent 1479f159aa
commit ccf70a1b76
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 3 deletions

View File

@ -94,4 +94,32 @@ function mapTree<T, V extends Record<string, any>>(
});
}
export { filterTree, mapTree, traverseTreeValues };
/**
*
* @param treeData -
* @param sortFunction -
* @param options -
* @returns
*/
function sortTree<T extends Record<string, any>>(
treeData: T[],
sortFunction: (a: T, b: T) => number,
options?: TreeConfigOptions,
): T[] {
const { childProps } = options || {
childProps: 'children',
};
return treeData.toSorted(sortFunction).map((item) => {
const children = item[childProps];
if (children && Array.isArray(children) && children.length > 0) {
return {
...item,
[childProps]: sortTree(children, sortFunction, options),
};
}
return item;
});
}
export { filterTree, mapTree, sortTree, traverseTreeValues };

View File

@ -6,7 +6,7 @@ import type {
RouteMeta,
} from '@vben-core/typings';
import { filterTree, mapTree } from '@vben-core/shared/utils';
import { filterTree, mapTree, sortTree } from '@vben-core/shared/utils';
/**
* routes
@ -81,7 +81,7 @@ function generateMenus(
});
// 对菜单进行排序避免order=0时被替换成999的问题
menus = menus.toSorted((a, b) => (a?.order ?? 999) - (b?.order ?? 999));
menus = sortTree(menus, (a, b) => (a?.order ?? 999) - (b?.order ?? 999));
// 过滤掉隐藏的菜单项
return filterTree(menus, (menu) => !!menu.show);