SiderMenu.js 2.33 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 21 22 23 24 25 26 27 28 29
  static getDerivedStateFromProps(props, state) {
    const { pathname } = state;
    if (props.location.pathname !== pathname) {
      return {
        pathname: props.location.pathname,
        openKeys: getDefaultCollapsedSubMenus(props),
      };
    }
    return null;
  }
30

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

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

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

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