| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import {FontIcon} from '@components';
- import {TreeRoleMenuData} from '@models';
- import {ReactNode} from 'react';
- export type ParseMenuType = {
- key: string;
- title: string;
- label: string;
- pid: string;
- url: string;
- icon: ReactNode;
- children?: ParseMenuType[];
- };
- export function sortMenu(menus: TreeRoleMenuData[]) {
- const result: ParseMenuType[] = [];
- const tempAreaMap = new Map<string, ParseMenuType[]>();
- menus.forEach(function({id, name, url, img, pId}) {
- const tempValue: ParseMenuType = {
- key: id,
- title: name,
- label: name,
- pid: pId,
- url,
- icon: img && <FontIcon name={img} />,
- };
- if (pId === '0') {
- // 是一级菜单找是否在临时区域有保存的二级菜单
- const tempList = tempAreaMap.get(id);
- result.push({...tempValue, children: tempList ? [...tempList] : []});
- } else {
- // 二级菜单判断是否有对应的一级菜单 如果有加到父组件的children里
- // 如果没有放在临时Map中
- const idx = result.findIndex(val => val.key === pId);
- if (idx >= 0) {
- result[idx].children!.push(tempValue);
- } else {
- if (tempAreaMap.has(pId)) {
- tempAreaMap.get(pId)!.push(tempValue);
- } else {
- tempAreaMap.set(pId, [tempValue]);
- }
- }
- }
- });
- return result;
- }
|