BaseMenu.js 4.5 KB
Newer Older
jim's avatar
jim committed
1 2
import React, { PureComponent } from 'react';
import { Menu, Icon } from 'antd';
zinkey's avatar
zinkey committed
3
import Link from 'umi/link';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
4 5
import isEqual from 'lodash/isEqual';
import memoizeOne from 'memoize-one';
jim's avatar
jim committed
6
import pathToRegexp from 'path-to-regexp';
jim's avatar
jim committed
7
import { urlToList } from '../_utils/pathTools';
jim's avatar
jim committed
8 9 10 11 12 13 14 15
import styles from './index.less';

const { SubMenu } = Menu;

// Allow menu.js config icon as string or ReactNode
//   icon: 'setting',
//   icon: 'http://demo.com/icon.png',
//   icon: <Icon type="setting" />,
jim's avatar
jim committed
16
const getIcon = icon => {
jim's avatar
jim committed
17 18 19 20 21 22 23 24 25
  if (typeof icon === 'string' && icon.indexOf('http') === 0) {
    return <img src={icon} alt="icon" className={styles.icon} />;
  }
  if (typeof icon === 'string') {
    return <Icon type={icon} />;
  }
  return icon;
};

26 27 28 29 30 31 32
export const getMenuMatches = (flatMenuKeys, path) =>
  flatMenuKeys.filter(item => {
    if (item) {
      return pathToRegexp(item).test(path);
    }
    return false;
  });
jim's avatar
jim committed
33

Erwin Zhang's avatar
Erwin Zhang committed
34
export default class BaseMenu extends PureComponent {
jim's avatar
jim committed
35 36
  constructor(props) {
    super(props);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
37
    this.getSelectedMenuKeys = memoizeOne(this.getSelectedMenuKeys, isEqual);
jim's avatar
jim committed
38
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
39

jim's avatar
jim committed
40 41 42 43
  /**
   * θŽ·εΎ—θœε•ε­θŠ‚η‚Ή
   * @memberof SiderMenu
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
44
  getNavMenuItems = (menusData, parent) => {
jim's avatar
jim committed
45 46 47 48 49
    if (!menusData) {
      return [];
    }
    return menusData
      .filter(item => item.name && !item.hideInMenu)
jim's avatar
jim committed
50
      .map(item => {
jim's avatar
jim committed
51
        // make dom
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
52
        const ItemDom = this.getSubMenuOrItem(item, parent);
jim's avatar
jim committed
53 54 55 56
        return this.checkPermissionItem(item.authority, ItemDom);
      })
      .filter(item => item);
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
57

jim's avatar
jim committed
58
  // Get the currently selected menu
59 60 61 62
  getSelectedMenuKeys = pathname => {
    const { flatMenuKeys } = this.props;
    return urlToList(pathname).map(itemPath => getMenuMatches(flatMenuKeys, itemPath).pop());
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
63

jim's avatar
jim committed
64 65 66
  /**
   * get SubMenu or Item
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
67
  getSubMenuOrItem = item => {
afc163's avatar
afc163 committed
68 69
    // doc: add hideChildrenInMenu
    if (item.children && !item.hideChildrenInMenu && item.children.some(child => child.name)) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
70
      const { name } = item;
jim's avatar
jim committed
71 72 73 74 75 76
      return (
        <SubMenu
          title={
            item.icon ? (
              <span>
                {getIcon(item.icon)}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
77
                <span>{name}</span>
jim's avatar
jim committed
78 79
              </span>
            ) : (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
80
              name
jim's avatar
jim committed
81 82 83 84
            )
          }
          key={item.path}
        >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
85
          {this.getNavMenuItems(item.children)}
jim's avatar
jim committed
86 87 88
        </SubMenu>
      );
    }
89
    return <Menu.Item key={item.path}>{this.getMenuItemPath(item)}</Menu.Item>;
jim's avatar
jim committed
90
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
91

jim's avatar
jim committed
92 93 94 95 96
  /**
   * εˆ€ζ–­ζ˜―ε¦ζ˜―httpι“ΎζŽ₯.θΏ”ε›ž Link ζˆ– a
   * Judge whether it is http link.return a or Link
   * @memberof SiderMenu
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
97
  getMenuItemPath = item => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
98
    const { name } = item;
jim's avatar
jim committed
99 100
    const itemPath = this.conversionPath(item.path);
    const icon = getIcon(item.icon);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
101
    const { target } = item;
jim's avatar
jim committed
102 103 104 105 106 107 108 109 110
    // Is it a http link
    if (/^https?:\/\//.test(itemPath)) {
      return (
        <a href={itemPath} target={target}>
          {icon}
          <span>{name}</span>
        </a>
      );
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
111
    const { location, isMobile, onCollapse } = this.props;
jim's avatar
jim committed
112 113 114 115
    return (
      <Link
        to={itemPath}
        target={target}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
116
        replace={itemPath === location.pathname}
jim's avatar
jim committed
117
        onClick={
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
118
          isMobile
jim's avatar
jim committed
119
            ? () => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
120
                onCollapse(true);
jim's avatar
jim committed
121 122 123 124 125 126 127 128 129
              }
            : undefined
        }
      >
        {icon}
        <span>{name}</span>
      </Link>
    );
  };
130

jim's avatar
jim committed
131 132
  // permission to check
  checkPermissionItem = (authority, ItemDom) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
133 134 135
    const { Authorized } = this.props;
    if (Authorized && Authorized.check) {
      const { check } = Authorized;
jim's avatar
jim committed
136 137 138 139
      return check(authority, ItemDom);
    }
    return ItemDom;
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
140

jim's avatar
jim committed
141
  conversionPath = path => {
jim's avatar
jim committed
142 143 144
    if (path && path.indexOf('http') === 0) {
      return path;
    }
145
    return `/${path || ''}`.replace(/\/+/g, '/');
jim's avatar
jim committed
146
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
147

jim's avatar
jim committed
148
  render() {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
149 150 151 152 153 154
    const {
      openKeys,
      theme,
      mode,
      location: { pathname },
    } = this.props;
jim's avatar
jim committed
155
    // if pathname can't match, use the nearest parent's key
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
156
    let selectedKeys = this.getSelectedMenuKeys(pathname);
jim's avatar
jim committed
157 158 159
    if (!selectedKeys.length && openKeys) {
      selectedKeys = [openKeys[openKeys.length - 1]];
    }
jim's avatar
jim committed
160 161 162 163 164 165
    let props = {};
    if (openKeys) {
      props = {
        openKeys,
      };
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
166
    const { handleOpenChange, style, menuData } = this.props;
jim's avatar
jim committed
167 168 169
    return (
      <Menu
        key="Menu"
jim's avatar
jim committed
170 171
        mode={mode}
        theme={theme}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
172
        onOpenChange={handleOpenChange}
jim's avatar
jim committed
173
        selectedKeys={selectedKeys}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
174
        style={style}
175
        className={mode === 'horizontal' ? 'top-nav-menu' : ''}
jim's avatar
jim committed
176
        {...props}
jim's avatar
jim committed
177
      >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
178
        {this.getNavMenuItems(menuData)}
jim's avatar
jim committed
179 180 181 182
      </Menu>
    );
  }
}