SiderMenu.js 2.65 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 28 29 30 31
    const { pathname, flatMenuKeysLen } = state;
    if (
      props.location.pathname !== pathname ||
      props.flatMenuKeys.length !== flatMenuKeysLen
    ) {
jim's avatar
jim committed
32 33
      return {
        pathname: props.location.pathname,
34
        flatMenuKeysLen: props.flatMenuKeys.length,
jim's avatar
jim committed
35 36 37 38 39
        openKeys: getDefaultCollapsedSubMenus(props),
      };
    }
    return null;
  }
40

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

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

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

    const siderClassName = classNames(styles.sider, {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
64
      [styles.fixSiderBar]: fixSiderbar,
afc163's avatar
afc163 committed
65 66
      [styles.light]: theme === 'light',
    });
jiang's avatar
jiang committed
67 68
    return (
      <Sider
afc163's avatar
afc163 committed
69
        trigger={null}
jiang's avatar
jiang committed
70 71
        collapsible
        collapsed={collapsed}
72
        breakpoint="lg"
73 74 75 76 77
        onCollapse={(collapse) => {
          if (firstMount || !isMobile) {
            onCollapse(collapse);
          }
        }}
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>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
88 89 90 91 92 93 94 95 96 97
        <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
98 99 100 101
      </Sider>
    );
  }
}