BaseMenu.js 3.98 KB
Newer Older
jim's avatar
jim committed
1 2
import React, { PureComponent } from 'react';
import { Menu, Icon } from 'antd';
zinkey's avatar
zinkey committed
3
import Link from 'umi/link';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
4 5
import isEqual from 'lodash/isEqual';
import memoizeOne from 'memoize-one';
jim's avatar
jim committed
6
import { urlToList } from '../_utils/pathTools';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
7
import { getMenuMatches } from './SiderMenuUtils';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
8
import { isUrl } from '@/utils/utils';
jim's avatar
jim committed
9 10 11 12 13 14 15 16
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
17
const getIcon = icon => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
18
  if (typeof icon === 'string' && isUrl(icon)) {
jim's avatar
jim committed
19 20 21 22 23 24 25 26
    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
27
export default class BaseMenu extends PureComponent {
jim's avatar
jim committed
28 29
  constructor(props) {
    super(props);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30
    this.getSelectedMenuKeys = memoizeOne(this.getSelectedMenuKeys, isEqual);
jim's avatar
jim committed
31
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
32

jim's avatar
jim committed
33 34 35 36
  /**
   * θŽ·εΎ—θœε•ε­θŠ‚η‚Ή
   * @memberof SiderMenu
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
37
  getNavMenuItems = (menusData, parent) => {
jim's avatar
jim committed
38 39 40 41 42
    if (!menusData) {
      return [];
    }
    return menusData
      .filter(item => item.name && !item.hideInMenu)
43
      .map(item => this.getSubMenuOrItem(item, parent))
jim's avatar
jim committed
44 45
      .filter(item => item);
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
46

jim's avatar
jim committed
47
  // Get the currently selected menu
48 49 50 51
  getSelectedMenuKeys = pathname => {
    const { flatMenuKeys } = this.props;
    return urlToList(pathname).map(itemPath => getMenuMatches(flatMenuKeys, itemPath).pop());
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
52

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

jim's avatar
jim committed
81 82 83 84 85
  /**
   * εˆ€ζ–­ζ˜―ε¦ζ˜―httpι“ΎζŽ₯.θΏ”ε›ž Link ζˆ– a
   * Judge whether it is http link.return a or Link
   * @memberof SiderMenu
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
86
  getMenuItemPath = item => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
87
    const { name } = item;
jim's avatar
jim committed
88 89
    const itemPath = this.conversionPath(item.path);
    const icon = getIcon(item.icon);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
90
    const { target } = item;
jim's avatar
jim committed
91 92 93 94 95 96 97 98 99
    // Is it a http link
    if (/^https?:\/\//.test(itemPath)) {
      return (
        <a href={itemPath} target={target}>
          {icon}
          <span>{name}</span>
        </a>
      );
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
100
    const { location, isMobile, onCollapse } = this.props;
jim's avatar
jim committed
101 102 103 104
    return (
      <Link
        to={itemPath}
        target={target}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
105
        replace={itemPath === location.pathname}
jim's avatar
jim committed
106
        onClick={
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
107
          isMobile
jim's avatar
jim committed
108
            ? () => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
109
                onCollapse(true);
jim's avatar
jim committed
110 111 112 113 114 115 116 117 118
              }
            : undefined
        }
      >
        {icon}
        <span>{name}</span>
      </Link>
    );
  };
119

jim's avatar
jim committed
120
  conversionPath = path => {
jim's avatar
jim committed
121 122 123
    if (path && path.indexOf('http') === 0) {
      return path;
    }
124
    return `/${path || ''}`.replace(/\/+/g, '/');
jim's avatar
jim committed
125
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
126

jim's avatar
jim committed
127
  render() {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
128 129 130 131 132 133
    const {
      openKeys,
      theme,
      mode,
      location: { pathname },
    } = this.props;
jim's avatar
jim committed
134
    // if pathname can't match, use the nearest parent's key
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
135
    let selectedKeys = this.getSelectedMenuKeys(pathname);
jim's avatar
jim committed
136 137 138
    if (!selectedKeys.length && openKeys) {
      selectedKeys = [openKeys[openKeys.length - 1]];
    }
jim's avatar
jim committed
139 140 141 142 143 144
    let props = {};
    if (openKeys) {
      props = {
        openKeys,
      };
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
145
    const { handleOpenChange, style, menuData } = this.props;
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={mode === 'horizontal' ? 'top-nav-menu' : ''}
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>
    );
  }
}