BaseMenu.js 4.33 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 { urlToList } from '../_utils/pathTools';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
7
import { getMenuMatches } from './SiderMenuUtils';
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;
};

Erwin Zhang's avatar
Erwin Zhang committed
26
export default class BaseMenu extends PureComponent {
jim's avatar
jim committed
27 28
  constructor(props) {
    super(props);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
29
    this.getSelectedMenuKeys = memoizeOne(this.getSelectedMenuKeys, isEqual);
jim's avatar
jim committed
30
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
31

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

jim's avatar
jim committed
50
  // Get the currently selected menu
51 52 53 54
  getSelectedMenuKeys = pathname => {
    const { flatMenuKeys } = this.props;
    return urlToList(pathname).map(itemPath => getMenuMatches(flatMenuKeys, itemPath).pop());
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
55

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

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

jim's avatar
jim committed
123 124
  // permission to check
  checkPermissionItem = (authority, ItemDom) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
125 126 127
    const { Authorized } = this.props;
    if (Authorized && Authorized.check) {
      const { check } = Authorized;
jim's avatar
jim committed
128 129 130 131
      return check(authority, ItemDom);
    }
    return ItemDom;
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
132

jim's avatar
jim committed
133
  conversionPath = path => {
jim's avatar
jim committed
134 135 136
    if (path && path.indexOf('http') === 0) {
      return path;
    }
137
    return `/${path || ''}`.replace(/\/+/g, '/');
jim's avatar
jim committed
138
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
139

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