SiderMenu.tsx 3.43 KB
Newer Older
1
import { Layout } from 'antd';
afc163's avatar
afc163 committed
2
import classNames from 'classnames';
3
import React, { Component, Suspense } from 'react';
zinkey's avatar
zinkey committed
4
import Link from 'umi/link';
5
import defaultSettings from '../../../config/defaultSettings';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
6
import PageLoading from '../PageLoading';
何乐's avatar
何乐 committed
7
import { BaseMenuProps } from './BaseMenu';
8
import styles from './index.less';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
9
import { getDefaultCollapsedSubMenus } from './SiderMenuUtils';
jiang's avatar
jiang committed
10

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
11
const BaseMenu = React.lazy(() => import('./BaseMenu'));
jim's avatar
jim committed
12
const { Sider } = Layout;
13 14
const { title } = defaultSettings;
let firstMount: boolean = true;
jiang's avatar
jiang committed
15

何乐's avatar
何乐 committed
16
export interface SiderMenuProps extends BaseMenuProps {
17 18 19
  logo?: string;
  fixSiderbar?: boolean;
}
jim's avatar
jim committed
20

21
interface SiderMenuState {
何乐's avatar
何乐 committed
22 23
  pathname?: string;
  openKeys?: string[];
24 25
  flatMenuKeysLen?: number;
}
Yu's avatar
Yu committed
26

27
export default class SiderMenu extends Component<SiderMenuProps, SiderMenuState> {
28
  static defaultProps: Partial<SiderMenuProps> = {
何乐's avatar
何乐 committed
29 30 31 32 33 34 35 36 37 38 39
    flatMenuKeys: [],
    onCollapse: () => void 0,
    isMobile: false,
    openKeys: [],
    collapsed: false,
    handleOpenChange: () => void 0,
    menuData: [],
    onOpenChange: () => void 0,
  };

  static getDerivedStateFromProps(props: SiderMenuProps, state: SiderMenuState) {
Yu's avatar
Yu committed
40
    const { pathname, flatMenuKeysLen } = state;
何乐's avatar
何乐 committed
41
    if (props.location!.pathname !== pathname || props.flatMenuKeys!.length !== flatMenuKeysLen) {
jim's avatar
jim committed
42
      return {
何乐's avatar
何乐 committed
43 44
        pathname: props.location!.pathname,
        flatMenuKeysLen: props.flatMenuKeys!.length,
jim's avatar
jim committed
45 46 47 48 49
        openKeys: getDefaultCollapsedSubMenus(props),
      };
    }
    return null;
  }
何乐's avatar
何乐 committed
50

51 52 53 54 55 56 57 58 59 60
  constructor(props: SiderMenuProps) {
    super(props);
    this.state = {
      openKeys: getDefaultCollapsedSubMenus(props),
    };
  }

  componentDidMount() {
    firstMount = false;
  }
61

62
  isMainMenu: (key: string) => boolean = key => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
63
    const { menuData } = this.props;
何乐's avatar
何乐 committed
64
    return menuData!.some(item => {
jim's avatar
jim committed
65 66 67 68 69
      if (key) {
        return item.key === key || item.path === key;
      }
      return false;
    });
jim's avatar
jim committed
70
  };
71

何乐's avatar
何乐 committed
72
  handleOpenChange: (openKeys: string[]) => void = openKeys => {
ddcat1115's avatar
ddcat1115 committed
73
    const moreThanOne = openKeys.filter(openKey => this.isMainMenu(openKey)).length > 1;
何乐's avatar
何乐 committed
74 75 76 77 78
    if (moreThanOne) {
      this.setState({ openKeys: [openKeys.pop()].filter(item => item) as string[] });
    } else {
      this.setState({ openKeys: [...openKeys] });
    }
79
  };
80

jiang's avatar
jiang committed
81
  render() {
Yu's avatar
Yu committed
82
    const { logo, collapsed, onCollapse, fixSiderbar, theme, isMobile } = this.props;
83
    const { openKeys } = this.state;
jim's avatar
jim committed
84
    const defaultProps = collapsed ? {} : { openKeys };
afc163's avatar
afc163 committed
85 86

    const siderClassName = classNames(styles.sider, {
Yu's avatar
Yu committed
87
      [styles.fixSiderBar]: fixSiderbar,
afc163's avatar
afc163 committed
88 89
      [styles.light]: theme === 'light',
    });
jiang's avatar
jiang committed
90 91
    return (
      <Sider
何乐's avatar
何乐 committed
92
        collapsible
afc163's avatar
afc163 committed
93
        trigger={null}
jiang's avatar
jiang committed
94
        collapsed={collapsed}
95
        breakpoint="lg"
Yu's avatar
Yu committed
96 97
        onCollapse={collapse => {
          if (firstMount || !isMobile) {
何乐's avatar
何乐 committed
98
            onCollapse!(collapse);
Yu's avatar
Yu committed
99 100
          }
        }}
jiang's avatar
jiang committed
101
        width={256}
afc163's avatar
afc163 committed
102 103
        theme={theme}
        className={siderClassName}
jiang's avatar
jiang committed
104
      >
afc163's avatar
afc163 committed
105
        <div className={styles.logo} id="logo">
jiang's avatar
jiang committed
106 107
          <Link to="/">
            <img src={logo} alt="logo" />
Yu's avatar
Yu committed
108
            <h1>{title}</h1>
jiang's avatar
jiang committed
109 110
          </Link>
        </div>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
111 112 113 114 115 116 117 118 119 120
        <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
121 122 123 124
      </Sider>
    );
  }
}