BaseMenu.js 3.97 KB
Newer Older
jim's avatar
jim committed
1
import React, { PureComponent } from 'react';
2
import classNames from 'classnames';
jim's avatar
jim committed
3
import { Menu, Icon } from 'antd';
zinkey's avatar
zinkey committed
4
import Link from 'umi/link';
jim's avatar
jim committed
5
import { urlToList } from '../_utils/pathTools';
陈帅's avatar
陈帅 committed
6
import { getMenuMatches } from './SiderMenuUtils';
陈帅's avatar
陈帅 committed
7
import { isUrl } from '@/utils/utils';
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 => {
陈帅's avatar
陈帅 committed
17
  if (typeof icon === 'string' && isUrl(icon)) {
jim's avatar
jim committed
18 19 20 21 22 23 24 25
    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 {
陈帅's avatar
陈帅 committed
27

jim's avatar
jim committed
28 29 30 31
  /**
   * 获得菜单子节点
   * @memberof SiderMenu
   */
陈帅's avatar
陈帅 committed
32
  getNavMenuItems = (menusData, parent) => {
jim's avatar
jim committed
33 34 35 36 37
    if (!menusData) {
      return [];
    }
    return menusData
      .filter(item => item.name && !item.hideInMenu)
陈小聪's avatar
陈小聪 committed
38
      .map(item => this.getSubMenuOrItem(item, parent))
jim's avatar
jim committed
39 40
      .filter(item => item);
  };
陈帅's avatar
陈帅 committed
41

jim's avatar
jim committed
42
  // Get the currently selected menu
43 44 45 46
  getSelectedMenuKeys = pathname => {
    const { flatMenuKeys } = this.props;
    return urlToList(pathname).map(itemPath => getMenuMatches(flatMenuKeys, itemPath).pop());
  };
陈帅's avatar
陈帅 committed
47

jim's avatar
jim committed
48 49 50
  /**
   * get SubMenu or Item
   */
陈帅's avatar
陈帅 committed
51
  getSubMenuOrItem = item => {
afc163's avatar
afc163 committed
52 53
    // doc: add hideChildrenInMenu
    if (item.children && !item.hideChildrenInMenu && item.children.some(child => child.name)) {
陈帅's avatar
陈帅 committed
54
      const { name } = item;
jim's avatar
jim committed
55 56 57 58 59 60
      return (
        <SubMenu
          title={
            item.icon ? (
              <span>
                {getIcon(item.icon)}
陈帅's avatar
陈帅 committed
61
                <span>{name}</span>
jim's avatar
jim committed
62 63
              </span>
            ) : (
陈帅's avatar
陈帅 committed
64
              name
jim's avatar
jim committed
65 66 67 68
            )
          }
          key={item.path}
        >
陈帅's avatar
陈帅 committed
69
          {this.getNavMenuItems(item.children)}
jim's avatar
jim committed
70 71 72
        </SubMenu>
      );
    }
73
    return <Menu.Item key={item.path}>{this.getMenuItemPath(item)}</Menu.Item>;
jim's avatar
jim committed
74
  };
陈帅's avatar
陈帅 committed
75

jim's avatar
jim committed
76 77 78 79 80
  /**
   * 判断是否是http链接.返回 Link 或 a
   * Judge whether it is http link.return a or Link
   * @memberof SiderMenu
   */
陈帅's avatar
陈帅 committed
81
  getMenuItemPath = item => {
陈帅's avatar
陈帅 committed
82
    const { name } = item;
jim's avatar
jim committed
83 84
    const itemPath = this.conversionPath(item.path);
    const icon = getIcon(item.icon);
陈帅's avatar
陈帅 committed
85
    const { target } = item;
jim's avatar
jim committed
86 87 88 89 90 91 92 93 94
    // Is it a http link
    if (/^https?:\/\//.test(itemPath)) {
      return (
        <a href={itemPath} target={target}>
          {icon}
          <span>{name}</span>
        </a>
      );
    }
陈帅's avatar
陈帅 committed
95
    const { location, isMobile, onCollapse } = this.props;
jim's avatar
jim committed
96 97 98 99
    return (
      <Link
        to={itemPath}
        target={target}
陈帅's avatar
陈帅 committed
100
        replace={itemPath === location.pathname}
jim's avatar
jim committed
101
        onClick={
陈帅's avatar
陈帅 committed
102
          isMobile
jim's avatar
jim committed
103
            ? () => {
陈帅's avatar
陈帅 committed
104
                onCollapse(true);
jim's avatar
jim committed
105 106 107 108 109 110 111 112 113
              }
            : undefined
        }
      >
        {icon}
        <span>{name}</span>
      </Link>
    );
  };
114

jim's avatar
jim committed
115
  conversionPath = path => {
jim's avatar
jim committed
116 117 118
    if (path && path.indexOf('http') === 0) {
      return path;
    }
119
    return `/${path || ''}`.replace(/\/+/g, '/');
jim's avatar
jim committed
120
  };
陈帅's avatar
陈帅 committed
121

jim's avatar
jim committed
122
  render() {
陈帅's avatar
陈帅 committed
123 124 125 126 127
    const {
      openKeys,
      theme,
      mode,
      location: { pathname },
128
      className,
Ez-Z's avatar
Ez-Z committed
129
      collapsed,
陈帅's avatar
陈帅 committed
130
    } = this.props;
jim's avatar
jim committed
131
    // if pathname can't match, use the nearest parent's key
陈帅's avatar
陈帅 committed
132
    let selectedKeys = this.getSelectedMenuKeys(pathname);
jim's avatar
jim committed
133 134 135
    if (!selectedKeys.length && openKeys) {
      selectedKeys = [openKeys[openKeys.length - 1]];
    }
jim's avatar
jim committed
136
    let props = {};
Ez-Z's avatar
Ez-Z committed
137
    if (openKeys && !collapsed) {
jim's avatar
jim committed
138
      props = {
Ez-Z's avatar
Ez-Z committed
139
        openKeys: openKeys.length === 0 ? [...selectedKeys] : openKeys,
jim's avatar
jim committed
140 141
      };
    }
陈帅's avatar
陈帅 committed
142
    const { handleOpenChange, style, menuData } = this.props;
143 144 145 146
    const cls = classNames(className, {
      'top-nav-menu': mode === 'horizontal',
    });

jim's avatar
jim committed
147 148 149
    return (
      <Menu
        key="Menu"
jim's avatar
jim committed
150 151
        mode={mode}
        theme={theme}
陈帅's avatar
陈帅 committed
152
        onOpenChange={handleOpenChange}
jim's avatar
jim committed
153
        selectedKeys={selectedKeys}
陈帅's avatar
陈帅 committed
154
        style={style}
155
        className={cls}
jim's avatar
jim committed
156
        {...props}
jim's avatar
jim committed
157
      >
陈帅's avatar
陈帅 committed
158
        {this.getNavMenuItems(menuData)}
jim's avatar
jim committed
159 160 161 162
      </Menu>
    );
  }
}