SiderMenu.js 2.42 KB
Newer Older
jiang's avatar
jiang committed
1
import React, { PureComponent } from 'react';
2
import { Layout } from 'antd';
afc163's avatar
afc163 committed
3
import classNames from 'classnames';
zinkey's avatar
zinkey committed
4
import Link from 'umi/link';
jiang's avatar
jiang committed
5
import styles from './index.less';
Erwin Zhang's avatar
Erwin Zhang committed
6
import BaseMenu, { getMenuMatches } from './BaseMenu';
jim's avatar
jim committed
7
import { urlToList } from '../_utils/pathTools';
jiang's avatar
jiang committed
8

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

jim's avatar
jim committed
11 12 13 14 15
/**
 * θŽ·εΎ—θœε•ε­θŠ‚η‚Ή
 * @memberof SiderMenu
 */
const getDefaultCollapsedSubMenus = props => {
jim's avatar
jim committed
16 17
  const {
    location: { pathname },
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
18
    flatMenuKeys,
jim's avatar
jim committed
19
  } = props;
jim's avatar
jim committed
20
  return urlToList(pathname)
21
    .map(item => getMenuMatches(flatMenuKeys, item)[0])
jim's avatar
jim committed
22 23 24
    .filter(item => item);
};

jiang's avatar
jiang committed
25 26 27 28
export default class SiderMenu extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
jim's avatar
jim committed
29
      openKeys: getDefaultCollapsedSubMenus(props),
jiang's avatar
jiang committed
30 31
    };
  }
jim's avatar
jim committed
32

jim's avatar
jim committed
33 34 35 36 37 38 39 40 41 42
  static getDerivedStateFromProps(props, state) {
    const { pathname } = state;
    if (props.location.pathname !== pathname) {
      return {
        pathname: props.location.pathname,
        openKeys: getDefaultCollapsedSubMenus(props),
      };
    }
    return null;
  }
43

jim's avatar
jim committed
44
  isMainMenu = key => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
45 46
    const { menuData } = this.props;
    return menuData.some(item => {
jim's avatar
jim committed
47 48 49 50 51
      if (key) {
        return item.key === key || item.path === key;
      }
      return false;
    });
jim's avatar
jim committed
52
  };
53

jim's avatar
jim committed
54
  handleOpenChange = openKeys => {
ddcat1115's avatar
ddcat1115 committed
55
    const moreThanOne = openKeys.filter(openKey => this.isMainMenu(openKey)).length > 1;
jiang's avatar
jiang committed
56
    this.setState({
jim's avatar
jim committed
57
      openKeys: moreThanOne ? [openKeys.pop()] : [...openKeys],
jiang's avatar
jiang committed
58
    });
59
  };
60

jiang's avatar
jiang committed
61
  render() {
afc163's avatar
afc163 committed
62
    const { logo, collapsed, onCollapse, fixSiderbar, theme } = this.props;
63
    const { openKeys } = this.state;
jim's avatar
jim committed
64
    const defaultProps = collapsed ? {} : { openKeys };
afc163's avatar
afc163 committed
65 66 67 68 69

    const siderClassName = classNames(styles.sider, {
      [styles.fixSiderbar]: fixSiderbar,
      [styles.light]: theme === 'light',
    });
afc163's avatar
afc163 committed
70

jiang's avatar
jiang committed
71 72
    return (
      <Sider
afc163's avatar
afc163 committed
73
        trigger={null}
jiang's avatar
jiang committed
74 75
        collapsible
        collapsed={collapsed}
76
        breakpoint="lg"
afc163's avatar
afc163 committed
77
        onCollapse={onCollapse}
jiang's avatar
jiang committed
78
        width={256}
afc163's avatar
afc163 committed
79 80
        theme={theme}
        className={siderClassName}
jiang's avatar
jiang committed
81
      >
afc163's avatar
afc163 committed
82
        <div className={styles.logo} id="logo">
jiang's avatar
jiang committed
83 84 85 86 87
          <Link to="/">
            <img src={logo} alt="logo" />
            <h1>Ant Design Pro</h1>
          </Link>
        </div>
Erwin Zhang's avatar
Erwin Zhang committed
88
        <BaseMenu
jim's avatar
jim committed
89
          {...this.props}
jiang's avatar
jiang committed
90
          mode="inline"
jim's avatar
jim committed
91
          handleOpenChange={this.handleOpenChange}
92
          style={{ padding: '16px 0', width: '100%' }}
jim's avatar
jim committed
93
          {...defaultProps}
jim's avatar
jim committed
94
        />
jiang's avatar
jiang committed
95 96 97 98
      </Sider>
    );
  }
}