F 修复导航栏某些情况下未按预期展示的问题

pull/59/head
vapao 2020-03-16 11:04:52 +08:00
parent bd20860ac0
commit 70aaad416e
1 changed files with 8 additions and 13 deletions

View File

@ -17,14 +17,15 @@ import logo from './logo.svg';
class Sider extends React.Component { class Sider extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.keysMap = {}; this._init(props);
this.state = { this.state = {
selectedKeys: [props.location.pathname], selectedKeys: [],
openKeys: [], openKeys: [lodash.get(this.keysMap, props.location.pathname)],
}; };
} }
componentDidMount() { _init() {
this.keysMap = {};
for (let item of menus.filter(x => x.child)) { for (let item of menus.filter(x => x.child)) {
for (let m of item.child) { for (let m of item.child) {
this.keysMap[m.path] = item.title this.keysMap[m.path] = item.title
@ -61,7 +62,6 @@ class Sider extends React.Component {
}; };
handleSelect = ({key}) => { handleSelect = ({key}) => {
this.setState({selectedKeys: [key]});
history.push(key) history.push(key)
}; };
@ -72,13 +72,8 @@ class Sider extends React.Component {
}; };
render() { render() {
const subKey = lodash.get(this.keysMap, this.props.location.pathname);
let {openKeys} = this.state;
if (subKey && !openKeys.includes(subKey) && !this.props.collapsed) openKeys.push(subKey);
return ( return (
<Layout.Sider <Layout.Sider collapsed={this.props.collapsed}>
collapsed={this.props.collapsed}
>
<div className={styles.logo}> <div className={styles.logo}>
<img src={logo} alt="Logo"/> <img src={logo} alt="Logo"/>
<h1>Spug</h1> <h1>Spug</h1>
@ -86,8 +81,8 @@ class Sider extends React.Component {
<Menu <Menu
theme="dark" theme="dark"
mode="inline" mode="inline"
selectedKeys={this.state.selectedKeys} selectedKeys={[this.props.location.pathname]}
openKeys={openKeys} openKeys={this.state.openKeys}
onSelect={this.handleSelect} onSelect={this.handleSelect}
onClick={this.handleClick} onClick={this.handleClick}
onOpenChange={openKeys => this.setState({openKeys})} onOpenChange={openKeys => this.setState({openKeys})}