SliderMenu.js 3.18 KB
Newer Older
jiang's avatar
jiang committed
1
import React, { PureComponent } from 'react';
陈帅's avatar
陈帅 committed
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() {
陈帅's avatar
陈帅 committed
90
    const { logo, collapsed, 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',
    });
jiang's avatar
jiang committed
98 99 100 101 102
    return (
      <Sider
        trigger={null}
        collapsible
        collapsed={collapsed}
103
        breakpoint="lg"
jiang's avatar
jiang committed
104
        width={256}
afc163's avatar
afc163 committed
105 106
        theme={theme}
        className={siderClassName}
jiang's avatar
jiang committed
107
      >
afc163's avatar
afc163 committed
108
        <div className={styles.logo} id="logo">
jiang's avatar
jiang committed
109 110 111 112 113
          <Link to="/">
            <img src={logo} alt="logo" />
            <h1>Ant Design Pro</h1>
          </Link>
        </div>
Erwin Zhang's avatar
Erwin Zhang committed
114
        <BaseMenu
jim's avatar
jim committed
115
          {...this.props}
jiang's avatar
jiang committed
116
          mode="inline"
jim's avatar
jim committed
117
          handleOpenChange={this.handleOpenChange}
jiang's avatar
jiang committed
118 119
          onOpenChange={this.handleOpenChange}
          style={{ padding: '16px 0', width: '100%' }}
jim's avatar
jim committed
120
          {...defaultProps}
jim's avatar
jim committed
121
        />
jiang's avatar
jiang committed
122 123 124 125
      </Sider>
    );
  }
}