11import { Menu , MenuProps } from 'antd' ;
22import Color from 'color' ;
3- import { useCallback , useMemo , useState } from 'react' ;
3+ import { useMemo , useState } from 'react' ;
44import { useMatches , useNavigate } from 'react-router-dom' ;
55
66import Scrollbar from '@/components/scrollbar' ;
7- import { useRouteToMenuFn , usePermissionRoutes , useFlattenedRoutes } from '@/router/hooks' ;
7+ import {
8+ useRouteToMenuFn ,
9+ usePermissionRoutes ,
10+ useFlattenedRoutes ,
11+ usePathname ,
12+ } from '@/router/hooks' ;
813import { menuFilter } from '@/router/utils' ;
914import { useSettingActions , useSettings } from '@/store/settingStore' ;
1015import { useThemeToken } from '@/theme/hooks' ;
@@ -22,6 +27,7 @@ export default function NavVertical(props: Props) {
2227 const navigate = useNavigate ( ) ;
2328 const matches = useMatches ( ) ;
2429 const { colorBgElevated, colorBorder } = useThemeToken ( ) ;
30+ const pathname = usePathname ( ) ;
2531
2632 const settings = useSettings ( ) ;
2733 const { themeLayout } = settings ;
@@ -31,27 +37,32 @@ export default function NavVertical(props: Props) {
3137 const permissionRoutes = usePermissionRoutes ( ) ;
3238 const flattenedRoutes = useFlattenedRoutes ( ) ;
3339
40+ const [ collapsed , setCollapsed ] = useState ( themeLayout === ThemeLayout . Mini ) ;
3441 const menuList = useMemo ( ( ) => {
3542 const menuRoutes = menuFilter ( permissionRoutes ) ;
3643 return routeToMenuFn ( menuRoutes ) ;
3744 } , [ routeToMenuFn , permissionRoutes ] ) ;
3845
39- const [ currentOpenKeys , setCurrentOpenKeys ] = useState < string [ ] > ( ( ) => {
40- return matches . filter ( ( match ) => match . pathname !== '/' ) . map ( ( match ) => match . pathname ) ;
41- } ) ;
46+ const selectedKeys = useMemo ( ( ) => [ pathname ] , [ pathname ] ) ;
47+ const openKeys = useMemo ( ( ) => {
48+ const keys = matches
49+ . filter ( ( match ) => match . pathname !== '/' )
50+ . filter ( ( match ) => match . pathname !== pathname )
51+ . map ( ( match ) => match . pathname ) ;
52+ return keys ;
53+ } , [ matches , pathname ] ) ;
4254
43- const collapsed = useMemo ( ( ) => themeLayout === ThemeLayout . Mini , [ themeLayout ] ) ;
44-
45- const handleToggleCollapsed = useCallback ( ( ) => {
55+ const handleToggleCollapsed = ( ) => {
4656 setSettings ( {
4757 ...settings ,
4858 themeLayout : collapsed ? ThemeLayout . Vertical : ThemeLayout . Mini ,
4959 } ) ;
50- } , [ collapsed , settings , setSettings ] ) ;
60+ setCollapsed ( ! collapsed ) ;
61+ } ;
5162
5263 const onClick : MenuProps [ 'onClick' ] = ( { key, keyPath } ) => {
64+ console . log ( 'click' , key , keyPath ) ;
5365 const nextLink = flattenedRoutes ?. find ( ( el ) => el . key === key ) ;
54- setCurrentOpenKeys ( keyPath ) ;
5566 // Handle special case for external links in menu items
5667 // For external links: skip internal routing, avoid adding new tab in current project,
5768 // prevent selecting current route, and open link in new browser tab
@@ -65,29 +76,26 @@ export default function NavVertical(props: Props) {
6576 } ;
6677
6778 return (
68- < div className = "z-50 h-full flex-shrink-0" >
69- < div
70- className = "flex h-full flex-col"
71- style = { {
72- width : collapsed ? NAV_COLLAPSED_WIDTH : NAV_WIDTH ,
73- borderRight : `1px dashed ${ Color ( colorBorder ) . alpha ( 0.6 ) . toString ( ) } ` ,
74- } }
75- >
76- < NavLogo collapsed = { collapsed } onToggle = { handleToggleCollapsed } />
79+ < div
80+ style = { {
81+ width : collapsed ? NAV_COLLAPSED_WIDTH : NAV_WIDTH ,
82+ borderRight : `1px dashed ${ Color ( colorBorder ) . alpha ( 0.6 ) . toString ( ) } ` ,
83+ } }
84+ >
85+ < NavLogo collapsed = { collapsed } onToggle = { handleToggleCollapsed } />
7786
78- < Scrollbar style = { { height : `calc(100vh - ${ HEADER_HEIGHT } px)` } } >
79- < Menu
80- mode = "inline"
81- inlineCollapsed = { collapsed }
82- items = { menuList }
83- defaultOpenKeys = { themeLayout === ThemeLayout . Vertical ? currentOpenKeys : [ ] }
84- defaultSelectedKeys = { currentOpenKeys }
85- style = { { backgroundColor : colorBgElevated } }
86- className = "h-full !border-none"
87- onClick = { onClick }
88- />
89- </ Scrollbar >
90- </ div >
87+ < Scrollbar style = { { height : `calc(100vh - ${ HEADER_HEIGHT } px)` } } >
88+ < Menu
89+ mode = "inline"
90+ inlineCollapsed = { collapsed }
91+ items = { menuList }
92+ { ...( ! collapsed && { openKeys } ) }
93+ selectedKeys = { selectedKeys }
94+ style = { { backgroundColor : colorBgElevated } }
95+ className = "h-full !border-none"
96+ onClick = { onClick }
97+ />
98+ </ Scrollbar >
9199 </ div >
92100 ) ;
93101}
0 commit comments