Menu defaultSelected

This commit is contained in:
zuiidea
2017-04-25 12:09:20 +08:00
parent 6c48480031
commit 5fbb1b338a
3 changed files with 28 additions and 4 deletions
+25 -2
View File
@@ -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>