Commit f7fa3988 authored by afc163's avatar afc163

Only open current menu

parent 32693bbd
...@@ -24,6 +24,9 @@ class BasicLayout extends React.PureComponent { ...@@ -24,6 +24,9 @@ class BasicLayout extends React.PureComponent {
super(props); super(props);
// ζŠŠδΈ€ηΊ§ Layout ηš„ children δ½œδΈΊθœε•ι‘Ή // ζŠŠδΈ€ηΊ§ Layout ηš„ children δ½œδΈΊθœε•ι‘Ή
this.menus = getNavData().reduce((arr, current) => arr.concat(current.children), []); this.menus = getNavData().reduce((arr, current) => arr.concat(current.children), []);
this.state = {
openKeys: this.getDefaultCollapsedSubMenus(props),
};
} }
state = { state = {
mode: 'inline', mode: 'inline',
...@@ -48,13 +51,13 @@ class BasicLayout extends React.PureComponent { ...@@ -48,13 +51,13 @@ class BasicLayout extends React.PureComponent {
this.props.dispatch(routerRedux.push('/user/login')); this.props.dispatch(routerRedux.push('/user/login'));
} }
} }
getDefaultCollapsedSubMenus() { getDefaultCollapsedSubMenus(props) {
const currentMenuSelectedKeys = [...this.getCurrentMenuSelectedKeys()]; const currentMenuSelectedKeys = [...this.getCurrentMenuSelectedKeys(props)];
currentMenuSelectedKeys.splice(-1, 1); currentMenuSelectedKeys.splice(-1, 1);
return currentMenuSelectedKeys; return currentMenuSelectedKeys;
} }
getCurrentMenuSelectedKeys() { getCurrentMenuSelectedKeys(props) {
const { location: { pathname } } = this.props; const { location: { pathname } } = props || this.props;
const keys = pathname.split('/').slice(1); const keys = pathname.split('/').slice(1);
if (keys.length === 1 && keys[0] === '') { if (keys.length === 1 && keys[0] === '') {
return [this.menus[0].key]; return [this.menus[0].key];
...@@ -132,6 +135,12 @@ class BasicLayout extends React.PureComponent { ...@@ -132,6 +135,12 @@ class BasicLayout extends React.PureComponent {
}); });
return groupBy(newNotices, 'type'); return groupBy(newNotices, 'type');
} }
handleOpenChange = (openKeys) => {
const latestOpenKey = openKeys.find(key => this.state.openKeys.find(key));
this.setState({
openKeys: latestOpenKey ? [latestOpenKey] : [],
});
}
toggle = () => { toggle = () => {
const { collapsed } = this.props; const { collapsed } = this.props;
this.props.dispatch({ this.props.dispatch({
...@@ -189,7 +198,8 @@ class BasicLayout extends React.PureComponent { ...@@ -189,7 +198,8 @@ class BasicLayout extends React.PureComponent {
<Menu <Menu
theme="dark" theme="dark"
mode="inline" mode="inline"
defaultOpenKeys={this.getDefaultCollapsedSubMenus()} openKeys={this.state.openKeys}
onOpenChange={this.handleOpenChange}
selectedKeys={this.getCurrentMenuSelectedKeys()} selectedKeys={this.getCurrentMenuSelectedKeys()}
style={{ margin: '24px 0', width: '100%' }} style={{ margin: '24px 0', width: '100%' }}
inlineIndent={32} inlineIndent={32}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment