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

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

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

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

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

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

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