SiderMenu.js 2.63 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
let firstMount = true;

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

22 23 24 25
  componentDidMount() {
    firstMount = false;
  }

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

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

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

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

    const siderClassName = classNames(styles.sider, {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
61
      [styles.fixSiderBar]: fixSiderbar,
afc163's avatar
afc163 committed
62 63
      [styles.light]: theme === 'light',
    });
jiang's avatar
jiang committed
64 65
    return (
      <Sider
afc163's avatar
afc163 committed
66
        trigger={null}
jiang's avatar
jiang committed
67 68
        collapsible
        collapsed={collapsed}
69
        breakpoint="lg"
70 71 72 73 74
        onCollapse={(collapse) => {
          if (firstMount || !isMobile) {
            onCollapse(collapse);
          }
        }}
jiang's avatar
jiang committed
75
        width={256}
afc163's avatar
afc163 committed
76 77
        theme={theme}
        className={siderClassName}
jiang's avatar
jiang committed
78
      >
afc163's avatar
afc163 committed
79
        <div className={styles.logo} id="logo">
jiang's avatar
jiang committed
80 81 82 83 84
          <Link to="/">
            <img src={logo} alt="logo" />
            <h1>Ant Design Pro</h1>
          </Link>
        </div>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
85 86 87 88 89 90 91 92 93 94
        <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
95 96 97 98
      </Sider>
    );
  }
}