This commit is contained in:
ditiqi 2025-02-06 19:23:19 +08:00
parent 00fcce7a43
commit eb9eec00d9
1 changed files with 48 additions and 51 deletions

View File

@ -1,8 +1,8 @@
import { useState, useMemo } from 'react';
import { NavLink, matchPath, useLocation, useMatches } from 'react-router-dom';
import { Layout, Menu, theme } from 'antd';
import type { MenuProps } from 'antd';
import { CustomRouteObject } from '@web/src/routes/types';
import { useState, useMemo } from "react";
import { NavLink, matchPath, useLocation, useMatches } from "react-router-dom";
import { Layout, Menu, theme } from "antd";
import type { MenuProps } from "antd";
import { CustomRouteObject } from "@web/src/routes/types";
const { Sider } = Layout;
const { useToken } = theme;
@ -14,22 +14,17 @@ type SidebarProps = {
export default function AdminSidebar({ routes }: SidebarProps) {
const [collapsed, setCollapsed] = useState(false);
const { token } = useToken();
let matches = useMatches();
console.log(matches)
const menuItems: MenuProps['items'] = useMemo(() =>
routes.map(route => ({
const matches = useMatches();
console.log(matches);
const menuItems: MenuProps["items"] = useMemo(
() =>
routes.map((route) => ({
key: route.path,
icon: route.icon,
label: (
<NavLink
to={route.path}
>
{route.name}
</NavLink>
),
}))
, [routes]);
label: <NavLink to={route.path}>{route.name}</NavLink>,
})),
[routes]
);
return (
<Sider
@ -40,20 +35,22 @@ export default function AdminSidebar({ routes }: SidebarProps) {
className="h-screen sticky top-0"
style={{
backgroundColor: token.colorBgContainer,
borderRight: `1px solid ${token.colorBorderSecondary}`
}}
>
borderRight: `1px solid ${token.colorBorderSecondary}`,
}}>
<Menu
theme="light"
mode="inline"
selectedKeys={routes
.filter(route => matches.some(match => match.pathname.includes(route.path)))
.map(route => route.path)}
.filter((route) =>
matches.some((match) =>
match.pathname.includes(route.path)
)
)
.map((route) => route.path)}
items={menuItems}
className="border-r-0"
style={{
borderRight: 0
borderRight: 0,
}}
/>
</Sider>