BaseMenu.js 4.09 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';
陈帅's avatar
陈帅 committed
5 6
import isEqual from 'lodash/isEqual';
import memoizeOne from 'memoize-one';
jim's avatar
jim committed
7
import { urlToList } from '../_utils/pathTools';
陈帅's avatar
陈帅 committed
8
import { getMenuMatches } from './SiderMenuUtils';
陈帅's avatar
陈帅 committed
9
import { isUrl } from '@/utils/utils';
jim's avatar
jim committed
10 11 12 13 14 15 16 17
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
18
const getIcon = icon => {
陈帅's avatar
陈帅 committed
19
  if (typeof icon === 'string' && isUrl(icon)) {
jim's avatar
jim committed
20 21 22 23 24 25 26 27
    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
28
export default class BaseMenu extends PureComponent {
jim's avatar
jim committed
29 30
  constructor(props) {
    super(props);
陈帅's avatar
陈帅 committed
31
    this.getSelectedMenuKeys = memoizeOne(this.getSelectedMenuKeys, isEqual);
jim's avatar
jim committed
32
  }
陈帅's avatar
陈帅 committed
33

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

jim's avatar
jim committed
48
  // Get the currently selected menu
49 50 51 52
  getSelectedMenuKeys = pathname => {
    const { flatMenuKeys } = this.props;
    return urlToList(pathname).map(itemPath => getMenuMatches(flatMenuKeys, itemPath).pop());
  };
陈帅's avatar
陈帅 committed
53

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

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

jim's avatar
jim committed
121
  conversionPath = path => {
jim's avatar
jim committed
122 123 124
    if (path && path.indexOf('http') === 0) {
      return path;
    }
125
    return `/${path || ''}`.replace(/\/+/g, '/');
jim's avatar
jim committed
126
  };
陈帅's avatar
陈帅 committed
127

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

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