SiderMenu.js 2.67 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';
8
import { title } from '../../defaultSettings';
jiang's avatar
jiang committed
9

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

13 14
let firstMount = true;

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

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

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

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

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

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

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