BaseMenu.js 4.95 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 75 76 77
  getSelectedMenuKeys = pathname =>
    urlToList(pathname).map(function(itemPath) {
      return getMenuMatches(this.flatMenuKeys, itemPath).pop();
    });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
78

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

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

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

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

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