import styles from "./Menu.module.less"; import clsx from "clsx"; import { useEffect, useState } from "react"; import { useParams, useLocation, useNavigate } from "react-router-dom"; export default function Menu() { let navigate = useNavigate(); const location = useLocation(); const [active, setActive] = useState(""); useEffect(() => { initMenu(); }, []); async function initMenu() { function getActive(arr) { arr.forEach(item => { if(item.hasOwnProperty('path') && (item.path.includes(location.pathname) || item.path === location.pathname ) && !active) { setActive(item.label); } if(item.hasOwnProperty('child') && !active) { getActive(item.child) } }) } getActive(menuConfig) } const menuConfig = [ { label: "常用", child: [ // { // label: "文件整理", // path: "/file-sort", // }, // { // label: "文件清理", // path: "/file-clear", // }, { label: "文件管理", path: "/", }, { label: "chat", path: "/chat", }, { label: "Bookmark管理器", path: "/bookmarksList", }, ], }, { label: "系统", child: [ { label: "字符查询", }, { label: "设置", path: "/setting", }, { label: "关于我们", path: "/about", }, ], }, ]; function menuHandle(path: string, label: string) { setActive(label); navigate(path); } return (
Logo:
{menuConfig.map((item) => (
{item.label}
{item?.child?.length > 0 && item?.child.map((childItem) => (
menuHandle(childItem.path || "", childItem.label) } > {childItem.label}
))}
))}
); }