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>
    );
  }
}