index.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import {FontIcon} from '@components';
  2. import {TreeRoleMenuData} from '@models';
  3. import {ReactNode} from 'react';
  4. export type ParseMenuType = {
  5. key: string;
  6. title: string;
  7. label: string;
  8. pid: string;
  9. url: string;
  10. icon: ReactNode;
  11. children?: ParseMenuType[];
  12. };
  13. export function sortMenu(menus: TreeRoleMenuData[]) {
  14. const result: ParseMenuType[] = [];
  15. const tempAreaMap = new Map<string, ParseMenuType[]>();
  16. menus.forEach(function({id, name, url, img, pId}) {
  17. const tempValue: ParseMenuType = {
  18. key: id,
  19. title: name,
  20. label: name,
  21. pid: pId,
  22. url,
  23. icon: img && <FontIcon name={img} />,
  24. };
  25. if (pId === '0') {
  26. // 是一级菜单找是否在临时区域有保存的二级菜单
  27. const tempList = tempAreaMap.get(id);
  28. result.push({...tempValue, children: tempList ? [...tempList] : []});
  29. } else {
  30. // 二级菜单判断是否有对应的一级菜单 如果有加到父组件的children里
  31. // 如果没有放在临时Map中
  32. const idx = result.findIndex(val => val.key === pId);
  33. if (idx >= 0) {
  34. result[idx].children!.push(tempValue);
  35. } else {
  36. if (tempAreaMap.has(pId)) {
  37. tempAreaMap.get(pId)!.push(tempValue);
  38. } else {
  39. tempAreaMap.set(pId, [tempValue]);
  40. }
  41. }
  42. }
  43. });
  44. return result;
  45. }