SiderMenu.js 2.57 KB
Newer Older
jiang's avatar
jiang committed
1
import React, { PureComponent } from 'react';
jim's avatar
jim committed
2
import { Layout } from 'antd';
jiang's avatar
jiang committed
3 4
import { Link } from 'dva/router';
import styles from './index.less';
jim's avatar
jim committed
5
import BaseMeun, { getMeunMatcheys } from './BaseMeun';
6
import { urlToList } from '../utils/pathTools';
jiang's avatar
jiang committed
7

8

jim's avatar
jim committed
9
const { Sider } = Layout;
10

jiang's avatar
jiang committed
11 12 13
export default class SiderMenu extends PureComponent {
  constructor(props) {
    super(props);
ddcat1115's avatar
ddcat1115 committed
14
    this.menus = props.menuData;
15
    this.flatMenuKeys = this.getFlatMenuKeys(props.menuData);
jiang's avatar
jiang committed
16 17 18 19
    this.state = {
      openKeys: this.getDefaultCollapsedSubMenus(props),
    };
  }
20 21 22 23 24 25 26
  componentWillReceiveProps(nextProps) {
    if (nextProps.location.pathname !== this.props.location.pathname) {
      this.setState({
        openKeys: this.getDefaultCollapsedSubMenus(nextProps),
      });
    }
  }
27 28 29 30 31
  /**
   * Recursively flatten the data
   * [{path:string},{path:string}] => {path,path2}
   * @param  menus
   */
jiang's avatar
jiang committed
32 33 34 35 36 37
  getFlatMenuKeys(menus) {
    let keys = [];
    menus.forEach((item) => {
      if (item.children) {
        keys = keys.concat(this.getFlatMenuKeys(item.children));
      }
38
      keys.push(item.path);
jiang's avatar
jiang committed
39 40 41
    });
    return keys;
  }
42
  /**
jim's avatar
jim committed
43 44 45
   * Convert pathname to openKeys
   * /list/search/articles = > ['list','/list/search']
   * @param  props
46
   */
jim's avatar
jim committed
47 48 49
  getDefaultCollapsedSubMenus(props) {
    const { location: { pathname } } = props || this.props;
    return urlToList(pathname)
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
50
      .map((item) => {
jim's avatar
jim committed
51
        return getMeunMatcheys(this.flatMenuKeys, item)[0];
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
52
      })
53
      .filter(item => item);
ddcat1115's avatar
ddcat1115 committed
54 55 56 57
  }
  handleOpenChange = (openKeys) => {
    const lastOpenKey = openKeys[openKeys.length - 1];
    const moreThanOne = openKeys.filter(openKey => this.isMainMenu(openKey)).length > 1;
jiang's avatar
jiang committed
58
    this.setState({
ddcat1115's avatar
ddcat1115 committed
59
      openKeys: moreThanOne ? [lastOpenKey] : [...openKeys],
jiang's avatar
jiang committed
60
    });
61
  };
jiang's avatar
jiang committed
62
  render() {
63
    const { logo, collapsed, onCollapse } = this.props;
64
    const { openKeys } = this.state;
jiang's avatar
jiang committed
65 66 67 68 69
    return (
      <Sider
        trigger={null}
        collapsible
        collapsed={collapsed}
70
        breakpoint="lg"
jiang's avatar
jiang committed
71 72 73 74
        onCollapse={onCollapse}
        width={256}
        className={styles.sider}
      >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
75
        <div className={styles.logo} key="logo">
jiang's avatar
jiang committed
76 77 78 79 80
          <Link to="/">
            <img src={logo} alt="logo" />
            <h1>Ant Design Pro</h1>
          </Link>
        </div>
jim's avatar
jim committed
81 82
        <BaseMeun
          {...this.props}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
83
          key="Menu"
jiang's avatar
jiang committed
84 85
          theme="dark"
          mode="inline"
jim's avatar
jim committed
86 87
          handleOpenChange={this.handleOpenChange}
          openKeys={collapsed ? [] : openKeys}
jiang's avatar
jiang committed
88 89
          onOpenChange={this.handleOpenChange}
          style={{ padding: '16px 0', width: '100%' }}
jim's avatar
jim committed
90
        />
jiang's avatar
jiang committed
91 92 93 94
      </Sider>
    );
  }
}