BaseMenu.js 4.92 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';
6
import { formatMessage } from 'umi/locale';
jim's avatar
jim committed
7
import pathToRegexp from 'path-to-regexp';
jim's avatar
jim committed
8
import { urlToList } from '../_utils/pathTools';
jim's avatar
jim committed
9 10 11 12 13 14 15 16
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
17
const getIcon = icon => {
jim's avatar
jim committed
18 19 20 21 22 23 24 25 26
  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;
};

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
27 28 29 30
export const getMenuMatches = memoizeOne(
  (flatMenuKeys, path) => flatMenuKeys.filter(item => item && pathToRegexp(item).test(path)),
  isEqual
);
jim's avatar
jim committed
31

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

jim's avatar
jim committed
39 40 41 42 43 44 45
  /**
   * Recursively flatten the data
   * [{path:string},{path:string}] => {path,path2}
   * @param  menus
   */
  getFlatMenuKeys(menus) {
    let keys = [];
jim's avatar
jim committed
46
    menus.forEach(item => {
jim's avatar
jim committed
47 48 49 50 51 52 53
      if (item.children) {
        keys = keys.concat(this.getFlatMenuKeys(item.children));
      }
      keys.push(item.path);
    });
    return keys;
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
54

jim's avatar
jim committed
55 56 57 58
  /**
   * θŽ·εΎ—θœε•ε­θŠ‚η‚Ή
   * @memberof SiderMenu
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
59
  getNavMenuItems = (menusData, parent) => {
jim's avatar
jim committed
60 61 62 63 64
    if (!menusData) {
      return [];
    }
    return menusData
      .filter(item => item.name && !item.hideInMenu)
jim's avatar
jim committed
65
      .map(item => {
jim's avatar
jim committed
66
        // make dom
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
67
        const ItemDom = this.getSubMenuOrItem(item, parent);
jim's avatar
jim committed
68 69 70 71
        return this.checkPermissionItem(item.authority, ItemDom);
      })
      .filter(item => item);
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
72

jim's avatar
jim committed
73
  // Get the currently selected menu
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
74
  getSelectedMenuKeys = pathname =>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
75
    urlToList(pathname).map(itemPath => getMenuMatches(this.flatMenuKeys, itemPath).pop());
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
76

jim's avatar
jim committed
77 78 79
  /**
   * get SubMenu or Item
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
80
  getSubMenuOrItem = item => {
afc163's avatar
afc163 committed
81 82
    // doc: add hideChildrenInMenu
    if (item.children && !item.hideChildrenInMenu && item.children.some(child => child.name)) {
83
      const name = formatMessage({ id: item.locale });
jim's avatar
jim committed
84 85 86 87 88 89
      return (
        <SubMenu
          title={
            item.icon ? (
              <span>
                {getIcon(item.icon)}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
90
                <span>{name}</span>
jim's avatar
jim committed
91 92
              </span>
            ) : (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
93
              name
jim's avatar
jim committed
94 95 96 97
            )
          }
          key={item.path}
        >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
98
          {this.getNavMenuItems(item.children)}
jim's avatar
jim committed
99 100 101
        </SubMenu>
      );
    }
102
    return <Menu.Item key={item.path}>{this.getMenuItemPath(item)}</Menu.Item>;
jim's avatar
jim committed
103
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
104

jim's avatar
jim committed
105 106 107 108 109
  /**
   * εˆ€ζ–­ζ˜―ε¦ζ˜―httpι“ΎζŽ₯.θΏ”ε›ž Link ζˆ– a
   * Judge whether it is http link.return a or Link
   * @memberof SiderMenu
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
110
  getMenuItemPath = item => {
111
    const name = formatMessage({ id: item.locale });
jim's avatar
jim committed
112 113
    const itemPath = this.conversionPath(item.path);
    const icon = getIcon(item.icon);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
114
    const { target } = item;
jim's avatar
jim committed
115 116 117 118 119 120 121 122 123
    // Is it a http link
    if (/^https?:\/\//.test(itemPath)) {
      return (
        <a href={itemPath} target={target}>
          {icon}
          <span>{name}</span>
        </a>
      );
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
124
    const { location, isMobile, onCollapse } = this.props;
jim's avatar
jim committed
125 126 127 128
    return (
      <Link
        to={itemPath}
        target={target}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
129
        replace={itemPath === location.pathname}
jim's avatar
jim committed
130
        onClick={
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
131
          isMobile
jim's avatar
jim committed
132
            ? () => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
133
                onCollapse(true);
jim's avatar
jim committed
134 135 136 137 138 139 140 141 142
              }
            : undefined
        }
      >
        {icon}
        <span>{name}</span>
      </Link>
    );
  };
143

jim's avatar
jim committed
144 145
  // permission to check
  checkPermissionItem = (authority, ItemDom) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
146 147 148
    const { Authorized } = this.props;
    if (Authorized && Authorized.check) {
      const { check } = Authorized;
jim's avatar
jim committed
149 150 151 152
      return check(authority, ItemDom);
    }
    return ItemDom;
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
153

jim's avatar
jim committed
154
  conversionPath = path => {
jim's avatar
jim committed
155 156 157
    if (path && path.indexOf('http') === 0) {
      return path;
    }
158
    return `/${path || ''}`.replace(/\/+/g, '/');
jim's avatar
jim committed
159
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
160

jim's avatar
jim committed
161
  render() {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
162 163 164 165 166 167
    const {
      openKeys,
      theme,
      mode,
      location: { pathname },
    } = this.props;
jim's avatar
jim committed
168
    // if pathname can't match, use the nearest parent's key
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
169
    let selectedKeys = this.getSelectedMenuKeys(pathname);
jim's avatar
jim committed
170 171 172
    if (!selectedKeys.length && openKeys) {
      selectedKeys = [openKeys[openKeys.length - 1]];
    }
jim's avatar
jim committed
173 174 175 176 177 178
    let props = {};
    if (openKeys) {
      props = {
        openKeys,
      };
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
179
    const { handleOpenChange, style, menuData } = this.props;
jim's avatar
jim committed
180 181 182
    return (
      <Menu
        key="Menu"
jim's avatar
jim committed
183 184
        mode={mode}
        theme={theme}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
185
        onOpenChange={handleOpenChange}
jim's avatar
jim committed
186
        selectedKeys={selectedKeys}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
187
        style={style}
188
        className={mode === 'horizontal' ? 'top-nav-menu' : ''}
jim's avatar
jim committed
189
        {...props}
jim's avatar
jim committed
190
      >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
191
        {this.getNavMenuItems(menuData)}
jim's avatar
jim committed
192 193 194 195
      </Menu>
    );
  }
}