SiderMenu.js 3.22 KB
Newer Older
jiang's avatar
jiang committed
1
import React, { PureComponent } from 'react';
2
import { Layout } from 'antd';
jim's avatar
jim committed
3
import pathToRegexp from 'path-to-regexp';
afc163's avatar
afc163 committed
4
import classNames from 'classnames';
zinkey's avatar
zinkey committed
5
import Link from 'umi/link';
jiang's avatar
jiang committed
6
import styles from './index.less';
Erwin Zhang's avatar
Erwin Zhang committed
7
import BaseMenu, { getMenuMatches } from './BaseMenu';
jim's avatar
jim committed
8
import { urlToList } from '../_utils/pathTools';
jiang's avatar
jiang committed
9

jim's avatar
jim committed
10
const { Sider } = Layout;
jiang's avatar
jiang committed
11

jim's avatar
jim committed
12 13 14 15 16
/**
 * θŽ·εΎ—θœε•ε­θŠ‚η‚Ή
 * @memberof SiderMenu
 */
const getDefaultCollapsedSubMenus = props => {
jim's avatar
jim committed
17 18
  const {
    location: { pathname },
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
19
    flatMenuKeys,
jim's avatar
jim committed
20
  } = props;
jim's avatar
jim committed
21
  return urlToList(pathname)
22
    .map(item => getMenuMatches(flatMenuKeys, item)[0])
jim's avatar
jim committed
23 24 25
    .filter(item => item);
};

jim's avatar
jim committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/**
 * Recursively flatten the data
 * [{path:string},{path:string}] => {path,path2}
 * @param  menu
 */
export const getFlatMenuKeys = menu =>
  menu.reduce((keys, item) => {
    keys.push(item.path);
    if (item.children) {
      return keys.concat(getFlatMenuKeys(item.children));
    }
    return keys;
  }, []);

/**
 * Find all matched menu keys based on paths
 * @param  flatMenuKeys: [/abc, /abc/:id, /abc/:id/info]
 * @param  paths: [/abc, /abc/11, /abc/11/info]
 */
export const getMenuMatchKeys = (flatMenuKeys, paths) =>
  paths.reduce(
    (matchKeys, path) =>
      matchKeys.concat(flatMenuKeys.filter(item => pathToRegexp(item).test(path))),
    []
  );

jiang's avatar
jiang committed
52 53 54
export default class SiderMenu extends PureComponent {
  constructor(props) {
    super(props);
jim's avatar
jim committed
55
    this.flatMenuKeys = getFlatMenuKeys(props.menuData);
jiang's avatar
jiang committed
56
    this.state = {
jim's avatar
jim committed
57
      openKeys: getDefaultCollapsedSubMenus(props),
jiang's avatar
jiang committed
58 59
    };
  }
jim's avatar
jim committed
60

jim's avatar
jim committed
61 62 63 64 65 66 67 68 69 70
  static getDerivedStateFromProps(props, state) {
    const { pathname } = state;
    if (props.location.pathname !== pathname) {
      return {
        pathname: props.location.pathname,
        openKeys: getDefaultCollapsedSubMenus(props),
      };
    }
    return null;
  }
71

jim's avatar
jim committed
72
  isMainMenu = key => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
73 74
    const { menuData } = this.props;
    return menuData.some(item => {
jim's avatar
jim committed
75 76 77 78 79
      if (key) {
        return item.key === key || item.path === key;
      }
      return false;
    });
jim's avatar
jim committed
80
  };
81

jim's avatar
jim committed
82
  handleOpenChange = openKeys => {
ddcat1115's avatar
ddcat1115 committed
83
    const moreThanOne = openKeys.filter(openKey => this.isMainMenu(openKey)).length > 1;
jiang's avatar
jiang committed
84
    this.setState({
jim's avatar
jim committed
85
      openKeys: moreThanOne ? [openKeys.pop()] : [...openKeys],
jiang's avatar
jiang committed
86
    });
87
  };
88

jiang's avatar
jiang committed
89
  render() {
afc163's avatar
afc163 committed
90
    const { logo, collapsed, onCollapse, fixSiderbar, theme } = this.props;
91
    const { openKeys } = this.state;
jim's avatar
jim committed
92
    const defaultProps = collapsed ? {} : { openKeys };
afc163's avatar
afc163 committed
93 94 95 96 97

    const siderClassName = classNames(styles.sider, {
      [styles.fixSiderbar]: fixSiderbar,
      [styles.light]: theme === 'light',
    });
afc163's avatar
afc163 committed
98

jiang's avatar
jiang committed
99 100
    return (
      <Sider
afc163's avatar
afc163 committed
101
        trigger={null}
jiang's avatar
jiang committed
102 103
        collapsible
        collapsed={collapsed}
104
        breakpoint="lg"
afc163's avatar
afc163 committed
105
        onCollapse={onCollapse}
jiang's avatar
jiang committed
106
        width={256}
afc163's avatar
afc163 committed
107 108
        theme={theme}
        className={siderClassName}
jiang's avatar
jiang committed
109
      >
afc163's avatar
afc163 committed
110
        <div className={styles.logo} id="logo">
jiang's avatar
jiang committed
111 112 113 114 115
          <Link to="/">
            <img src={logo} alt="logo" />
            <h1>Ant Design Pro</h1>
          </Link>
        </div>
Erwin Zhang's avatar
Erwin Zhang committed
116
        <BaseMenu
jim's avatar
jim committed
117
          {...this.props}
jiang's avatar
jiang committed
118
          mode="inline"
jim's avatar
jim committed
119
          handleOpenChange={this.handleOpenChange}
jiang's avatar
jiang committed
120 121
          onOpenChange={this.handleOpenChange}
          style={{ padding: '16px 0', width: '100%' }}
jim's avatar
jim committed
122
          {...defaultProps}
jim's avatar
jim committed
123
        />
jiang's avatar
jiang committed
124 125 126 127
      </Sider>
    );
  }
}