BaseMenu.js 4.36 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';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
8
import { isUrl } from '@/utils/utils';
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 => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
18
  if (typeof icon === 'string' && isUrl(icon)) {
jim's avatar
jim committed
19 20 21 22 23 24 25 26
    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
27
export default class BaseMenu extends PureComponent {
jim's avatar
jim committed
28 29
  constructor(props) {
    super(props);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30
    this.getSelectedMenuKeys = memoizeOne(this.getSelectedMenuKeys, isEqual);
jim's avatar
jim committed
31
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
32

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

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

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

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

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

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

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