Skip to content

Commit

Permalink
fix: correct submenu open status handling
Browse files Browse the repository at this point in the history
  • Loading branch information
w2xi committed Oct 7, 2024
1 parent f580341 commit 80a6a16
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 35 deletions.
19 changes: 17 additions & 2 deletions src/pages/layout/menu.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { useEffect, useState } from 'react'
import { Menu } from 'antd'
import { useNavigate, useLocation } from 'react-router-dom'
import useAppStore from '@/stores/app.ts'
import CustomIcon from './custom-icon.tsx'
import { findClosestParentMenuByPath } from '@/utils'
import type { FC } from 'react'
import type { MenuList } from '../../interface/layout/menu.ts'

interface MenuProps {
menuList: MenuList
}

const MenuComponent: FC<MenuProps> = props => {
const { menuList } = props
const MenuComponent: FC<MenuProps> = ({ menuList } ) => {
const [openKey, setOpenKey] = useState<string>('')
const { locale } = useAppStore()
const navigate = useNavigate()
const location = useLocation()
Expand All @@ -19,10 +21,23 @@ const MenuComponent: FC<MenuProps> = props => {
navigate(path)
}

const onOpenChange = (openKeys: string[]) => {
setOpenKey(openKeys.pop() || '')
}

useEffect(() => {
const menu = findClosestParentMenuByPath(menuList, location.pathname)
if (menu) {
setOpenKey(menu.code)
}
}, [location.pathname])

return (
<Menu
mode="inline"
openKeys={openKey ? [openKey] : []}
selectedKeys={[location.pathname]}
onOpenChange={onOpenChange}
onSelect={k => onMenuClick(k.key)}
items={menuList.map(item => {
return item.children
Expand Down
36 changes: 3 additions & 33 deletions src/pages/layout/tabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,11 @@ import { FormattedMessage } from 'react-intl'
import useAppStore from '@/stores/app'
import useTabBarStore from '@/stores/tabBar'
import useUserStore from '@/stores/user'
import type { MenuItem, MenuList } from '@/interface/layout/menu'
import { findMenuByPath, initAffixTags } from '@/utils'
import type { MenuProps } from 'antd'

const { useToken } = antTheme

const findMenuByPath = (menus: MenuList, path: string, locale: 'zh_CN' | 'en_US'): MenuItem | undefined => {
for (const menu of menus) {
if (menu.path === path) {
return menu
}
if (menu.children) {
const target = findMenuByPath(menu.children, path, locale)
if (target) {
return target
}
}
}
}

const initAffixTags = (menus: MenuList) => {
const affixTags: MenuItem[] = []
const findAffixTags = (menus: MenuList) => {
menus.forEach(menu => {
if (menu.affix) {
affixTags.push(menu)
}
if (menu.children) {
findAffixTags(menu.children)
}
})
}
findAffixTags(menus)
return affixTags
}

function TabBar() {
const navigate = useNavigate()
const location = useLocation()
Expand All @@ -63,11 +33,11 @@ function TabBar() {

useEffect(() => {
const path = location.pathname
const menu = findMenuByPath(menuList, path, locale)
const menu = findMenuByPath(menuList, path)
if (menu) {
addVisitedRoute(menu)
}
}, [location.pathname, locale])
}, [location.pathname])

const handleClick: MenuProps['onClick'] = ({ key }) => {
const index = visitedRoutes.findIndex(route => route.path === location.pathname)
Expand Down
31 changes: 31 additions & 0 deletions src/utils/findMenu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { MenuItem, MenuList } from '@/interface/layout/menu'

export const findMenuByPath = (menus: MenuList, path: string): MenuItem | undefined => {
for (const menu of menus) {
if (menu.path === path) {
return menu
}
if (menu.children) {
const target = findMenuByPath(menu.children, path)
if (target) {
return target
}
}
}
}

export const findClosestParentMenuByPath = (menus: MenuList, path: string, parent?: MenuItem): MenuItem | '' => {
for (const menu of menus) {
if (parent && menu.path === path) {
return menu
}
if (menu.children) {
const target = findClosestParentMenuByPath(menu.children, path, menu)
if (target) {
return menu
}
}
}

return ''
}
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './findMenu'
export * from './initAffixTags'
18 changes: 18 additions & 0 deletions src/utils/initAffixTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { MenuList } from '@/interface/layout/menu'

export const initAffixTags = (menus: MenuList) => {
const affixTags: MenuList = []
const findAffixTags = (menus: MenuList) => {
menus.forEach(menu => {
if (menu.affix) {
affixTags.push(menu)
}
if (menu.children) {
findAffixTags(menu.children)
}
})
}
findAffixTags(menus)

return affixTags
}

0 comments on commit 80a6a16

Please sign in to comment.