mirror of
https://github.com/zuiidea/antd-admin
synced 2026-11-05 05:54:26 +00:00
Menu defaultSelected
This commit is contained in:
@@ -2,7 +2,8 @@ import React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import { Menu, Icon } from 'antd'
|
||||
import { Link } from 'dva/router'
|
||||
import { arrayToTree } from '../../utils'
|
||||
import { arrayToTree, queryArray } from '../../utils'
|
||||
import pathToRegexp from 'path-to-regexp'
|
||||
|
||||
const Menus = ({ siderFold, darkTheme, location, handleClickNavMenu, navOpenKeys, changeOpenKeys, menu }) => {
|
||||
// 生成树状
|
||||
@@ -76,13 +77,35 @@ const Menus = ({ siderFold, darkTheme, location, handleClickNavMenu, navOpenKeys
|
||||
openKeys: navOpenKeys,
|
||||
} : {}
|
||||
|
||||
|
||||
// 寻找选中路由
|
||||
let currentMenu
|
||||
for (let item of menu) {
|
||||
if (item.router && pathToRegexp(item.router).exec(location.pathname)) {
|
||||
currentMenu = item
|
||||
break
|
||||
}
|
||||
}
|
||||
const getPathArray = (array, current, pid, id) => {
|
||||
let result = [String(current[id])]
|
||||
const getPath = (item) => {
|
||||
if (item[pid]) {
|
||||
result.unshift(String(item[pid]))
|
||||
getPath(queryArray(array, item[pid], id))
|
||||
}
|
||||
}
|
||||
getPath(current)
|
||||
return result
|
||||
}
|
||||
const defaultSelectedKeys = getPathArray(menu, currentMenu, 'mpid', 'id')
|
||||
|
||||
return (
|
||||
<Menu
|
||||
{...menuProps}
|
||||
mode={siderFold ? 'vertical' : 'inline'}
|
||||
theme={darkTheme ? 'dark' : 'light'}
|
||||
onClick={handleClickNavMenu}
|
||||
defaultSelectedKeys={[location.pathname !== '/' ? location.pathname : menuTree[0].router]}
|
||||
defaultSelectedKeys={defaultSelectedKeys}
|
||||
>
|
||||
{menuItems}
|
||||
</Menu>
|
||||
|
||||
Reference in New Issue
Block a user