BaseMenu.js 4 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)) {
lf7817's avatar
lf7817 committed
18
    return <Icon component={() => <img src={icon} alt="icon" className={styles.icon} />} />;
jim's avatar
jim committed
19 20 21 22 23 24 25
  }
  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)
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>
    );
  }
}