SliderMenu.js 3.15 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';
zinkey's avatar
zinkey committed
4
import Link from 'umi/link';
jiang's avatar
jiang committed
5
import styles from './index.less';
Erwin Zhang's avatar
Erwin Zhang committed
6
import BaseMenu, { getMenuMatches } from './BaseMenu';
jim's avatar
jim committed
7
import { urlToList } from '../_utils/pathTools';
jiang's avatar
jiang committed
8

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

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

jim's avatar
jim committed
25 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
/**
 * 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
51 52 53
export default class SiderMenu extends PureComponent {
  constructor(props) {
    super(props);
jim's avatar
jim committed
54
    this.flatMenuKeys = getFlatMenuKeys(props.menuData);
jiang's avatar
jiang committed
55
    this.state = {
jim's avatar
jim committed
56
      openKeys: getDefaultCollapsedSubMenus(props),
jiang's avatar
jiang committed
57 58
    };
  }
jim's avatar
jim committed
59

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

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

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

jiang's avatar
jiang committed
88
  render() {
89
    const { logo, collapsed, onCollapse, fixSiderbar, theme } = this.props;
90
    const { openKeys } = this.state;
jim's avatar
jim committed
91
    const defaultProps = collapsed ? {} : { openKeys };
jiang's avatar
jiang committed
92 93 94 95 96
    return (
      <Sider
        trigger={null}
        collapsible
        collapsed={collapsed}
97
        breakpoint="lg"
jiang's avatar
jiang committed
98 99
        onCollapse={onCollapse}
        width={256}
100 101 102
        className={`${styles.sider} ${fixSiderbar ? styles.fixSiderbar : ''} ${
          theme === 'light' ? styles.light : ''
        }`}
jiang's avatar
jiang committed
103
      >
104
        <div className={styles.logo} key="logo" id="logo">
jiang's avatar
jiang committed
105 106 107 108 109
          <Link to="/">
            <img src={logo} alt="logo" />
            <h1>Ant Design Pro</h1>
          </Link>
        </div>
Erwin Zhang's avatar
Erwin Zhang committed
110
        <BaseMenu
jim's avatar
jim committed
111
          {...this.props}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
112
          key="Menu"
jiang's avatar
jiang committed
113
          mode="inline"
jim's avatar
jim committed
114
          handleOpenChange={this.handleOpenChange}
jiang's avatar
jiang committed
115 116
          onOpenChange={this.handleOpenChange}
          style={{ padding: '16px 0', width: '100%' }}
jim's avatar
jim committed
117
          {...defaultProps}
jim's avatar
jim committed
118
        />
jiang's avatar
jiang committed
119 120 121 122
      </Sider>
    );
  }
}