BaseMenu.js 3.89 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)
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,
ι™ˆεΈ…'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 136 137 138 139 140
    let props = {};
    if (openKeys) {
      props = {
        openKeys,
      };
    }
ι™ˆεΈ…'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>
    );
  }
}