SiderMenu.js 2.46 KB
Newer Older
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
1
import React, { PureComponent, Suspense } 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';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
6 7
import PageLoading from '../PageLoading';
import { getDefaultCollapsedSubMenus } from './SiderMenuUtils';
jiang's avatar
jiang committed
8

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
9
const BaseMenu = React.lazy(() => import('./BaseMenu'));
jim's avatar
jim committed
10
const { Sider } = Layout;
jiang's avatar
jiang committed
11 12 13 14 15

export default class SiderMenu extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
jim's avatar
jim committed
16
      openKeys: getDefaultCollapsedSubMenus(props),
jiang's avatar
jiang committed
17 18
    };
  }
jim's avatar
jim committed
19

jim's avatar
jim committed
20
  static getDerivedStateFromProps(props, state) {
21 22 23 24 25
    const { pathname, flatMenuKeysLen } = state;
    if (
      props.location.pathname !== pathname ||
      props.flatMenuKeys.length !== flatMenuKeysLen
    ) {
jim's avatar
jim committed
26 27
      return {
        pathname: props.location.pathname,
28
        flatMenuKeysLen: props.flatMenuKeys.length,
jim's avatar
jim committed
29 30 31 32 33
        openKeys: getDefaultCollapsedSubMenus(props),
      };
    }
    return null;
  }
34

jim's avatar
jim committed
35
  isMainMenu = key => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
36 37
    const { menuData } = this.props;
    return menuData.some(item => {
jim's avatar
jim committed
38 39 40 41 42
      if (key) {
        return item.key === key || item.path === key;
      }
      return false;
    });
jim's avatar
jim committed
43
  };
44

jim's avatar
jim committed
45
  handleOpenChange = openKeys => {
ddcat1115's avatar
ddcat1115 committed
46
    const moreThanOne = openKeys.filter(openKey => this.isMainMenu(openKey)).length > 1;
jiang's avatar
jiang committed
47
    this.setState({
jim's avatar
jim committed
48
      openKeys: moreThanOne ? [openKeys.pop()] : [...openKeys],
jiang's avatar
jiang committed
49
    });
50
  };
51

jiang's avatar
jiang committed
52
  render() {
afc163's avatar
afc163 committed
53
    const { logo, collapsed, onCollapse, fixSiderbar, theme } = this.props;
54
    const { openKeys } = this.state;
jim's avatar
jim committed
55
    const defaultProps = collapsed ? {} : { openKeys };
afc163's avatar
afc163 committed
56 57

    const siderClassName = classNames(styles.sider, {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
58
      [styles.fixSiderBar]: fixSiderbar,
afc163's avatar
afc163 committed
59 60
      [styles.light]: theme === 'light',
    });
jiang's avatar
jiang committed
61 62
    return (
      <Sider
afc163's avatar
afc163 committed
63
        trigger={null}
jiang's avatar
jiang committed
64 65
        collapsible
        collapsed={collapsed}
66
        breakpoint="lg"
afc163's avatar
afc163 committed
67
        onCollapse={onCollapse}
jiang's avatar
jiang committed
68
        width={256}
afc163's avatar
afc163 committed
69 70
        theme={theme}
        className={siderClassName}
jiang's avatar
jiang committed
71
      >
afc163's avatar
afc163 committed
72
        <div className={styles.logo} id="logo">
jiang's avatar
jiang committed
73 74 75 76 77
          <Link to="/">
            <img src={logo} alt="logo" />
            <h1>Ant Design Pro</h1>
          </Link>
        </div>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
78 79 80 81 82 83 84 85 86 87
        <Suspense fallback={<PageLoading />}>
          <BaseMenu
            {...this.props}
            mode="inline"
            handleOpenChange={this.handleOpenChange}
            onOpenChange={this.handleOpenChange}
            style={{ padding: '16px 0', width: '100%' }}
            {...defaultProps}
          />
        </Suspense>
jiang's avatar
jiang committed
88 89 90 91
      </Sider>
    );
  }
}