Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu defaultSelected #214

Merged
merged 1 commit into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/components/Layout/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
// 生成树状
Expand Down Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion src/models/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default {
siderFold: localStorage.getItem(`${prefix}siderFold`) === 'true',
darkTheme: localStorage.getItem(`${prefix}darkTheme`) === 'true',
isNavbar: document.body.clientWidth < 769,
navOpenKeys: [],
navOpenKeys: JSON.parse(localStorage.getItem(`${prefix}navOpenKeys`)) || [],
},
subscriptions: {
setup ({ dispatch }) {
Expand Down
3 changes: 2 additions & 1 deletion src/routes/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Layout } from '../components'
import { classnames, config, menu } from '../utils'
import { Helmet } from 'react-helmet'
import '../themes/index.less'
const { prefix } = config

const { Header, Bread, Footer, Sider, styles } = Layout

Expand Down Expand Up @@ -43,7 +44,7 @@ const App = ({ children, location, dispatch, app }) => {
dispatch({ type: 'app/changeTheme' })
},
changeOpenKeys (openKeys) {
localStorage.setItem('navOpenKeys', JSON.stringify(openKeys))
localStorage.setItem(`${prefix}navOpenKeys`, JSON.stringify(openKeys))
dispatch({ type: 'app/handleNavOpenKeys', payload: { navOpenKeys: openKeys } })
},
}
Expand Down