Menu.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import styles from "./Menu.module.less";
  2. import clsx from "clsx";
  3. import { useEffect, useState } from "react";
  4. import { useParams, useLocation, useNavigate } from "react-router-dom";
  5. export default function Menu() {
  6. let navigate = useNavigate();
  7. const location = useLocation();
  8. const [active, setActive] = useState<string>("");
  9. useEffect(() => {
  10. initMenu();
  11. }, []);
  12. async function initMenu() {
  13. function getActive(arr) {
  14. arr.forEach(item => {
  15. if(item.hasOwnProperty('path') && (item.path.includes(location.pathname) || item.path === location.pathname ) && !active) {
  16. setActive(item.label);
  17. }
  18. if(item.hasOwnProperty('child') && !active) {
  19. getActive(item.child)
  20. }
  21. })
  22. }
  23. getActive(menuConfig)
  24. }
  25. const menuConfig = [
  26. {
  27. label: "常用",
  28. child: [
  29. // {
  30. // label: "文件整理",
  31. // path: "/file-sort",
  32. // },
  33. // {
  34. // label: "文件清理",
  35. // path: "/file-clear",
  36. // },
  37. {
  38. label: "文件管理",
  39. path: "/",
  40. },
  41. {
  42. label: "chat",
  43. path: "/chat",
  44. },
  45. {
  46. label: "Bookmark管理器",
  47. path: "/bookmarksList",
  48. },
  49. ],
  50. },
  51. {
  52. label: "系统",
  53. child: [
  54. {
  55. label: "字符查询",
  56. },
  57. {
  58. label: "设置",
  59. path: "/setting",
  60. },
  61. {
  62. label: "关于我们",
  63. path: "/about",
  64. },
  65. ],
  66. },
  67. ];
  68. function menuHandle(path: string, label: string) {
  69. setActive(label);
  70. navigate(path);
  71. }
  72. return (
  73. <div className={clsx(styles.box, styles.parent)}>
  74. <div className={styles.logoBox}>Logo:</div>
  75. {menuConfig.map((item) => (
  76. <div className={styles.menuBox} key={item.label}>
  77. <div className={styles.label}>{item.label}</div>
  78. <div className={styles.menuChildBox}>
  79. {item?.child?.length > 0 &&
  80. item?.child.map((childItem) => (
  81. <div
  82. key={childItem.label}
  83. className={clsx(
  84. styles.childItemLabel,
  85. active === childItem.label && styles.active,
  86. )}
  87. onClick={() =>
  88. menuHandle(childItem.path || "", childItem.label)
  89. }
  90. >
  91. {childItem.label}
  92. </div>
  93. ))}
  94. </div>
  95. </div>
  96. ))}
  97. </div>
  98. );
  99. }