diff --git a/src/layouts/BasicLayout.js b/src/layouts/BasicLayout.js index 86b61beee1406433e5c5c8d774e990c49dc06031..0a0a285248c183014c5360ec3b47fadccca5679c 100644 --- a/src/layouts/BasicLayout.js +++ b/src/layouts/BasicLayout.js @@ -24,6 +24,9 @@ class BasicLayout extends React.PureComponent { super(props); // 把一级 Layout 的 children 作为菜单项 this.menus = getNavData().reduce((arr, current) => arr.concat(current.children), []); + this.state = { + openKeys: this.getDefaultCollapsedSubMenus(props), + }; } state = { mode: 'inline', @@ -48,13 +51,13 @@ class BasicLayout extends React.PureComponent { this.props.dispatch(routerRedux.push('/user/login')); } } - getDefaultCollapsedSubMenus() { - const currentMenuSelectedKeys = [...this.getCurrentMenuSelectedKeys()]; + getDefaultCollapsedSubMenus(props) { + const currentMenuSelectedKeys = [...this.getCurrentMenuSelectedKeys(props)]; currentMenuSelectedKeys.splice(-1, 1); return currentMenuSelectedKeys; } - getCurrentMenuSelectedKeys() { - const { location: { pathname } } = this.props; + getCurrentMenuSelectedKeys(props) { + const { location: { pathname } } = props || this.props; const keys = pathname.split('/').slice(1); if (keys.length === 1 && keys[0] === '') { return [this.menus[0].key]; @@ -132,6 +135,12 @@ class BasicLayout extends React.PureComponent { }); return groupBy(newNotices, 'type'); } + handleOpenChange = (openKeys) => { + const latestOpenKey = openKeys.find(key => this.state.openKeys.find(key)); + this.setState({ + openKeys: latestOpenKey ? [latestOpenKey] : [], + }); + } toggle = () => { const { collapsed } = this.props; this.props.dispatch({ @@ -189,7 +198,8 @@ class BasicLayout extends React.PureComponent {