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

25
export const getMenuMatches = (flatMenuKeys, path) =>
26
  flatMenuKeys.filter(item => item && pathToRegexp(item).test(path));
jim's avatar
jim committed
27

Erwin Zhang's avatar
Erwin Zhang committed
28
export default class BaseMenu extends PureComponent {
jim's avatar
jim committed
29 30 31 32
  constructor(props) {
    super(props);
    this.flatMenuKeys = this.getFlatMenuKeys(props.menuData);
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
33

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

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

jim's avatar
jim committed
68 69
  // Get the currently selected menu
  getSelectedMenuKeys = () => {
jim's avatar
jim committed
70 71 72
    const {
      location: { pathname },
    } = this.props;
Erwin Zhang's avatar
Erwin Zhang committed
73
    return urlToList(pathname).map(itemPath => getMenuMatches(this.flatMenuKeys, itemPath).pop());
jim's avatar
jim committed
74
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
75

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

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

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

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

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