BaseMenu.js 4.12 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
import styles from './index.less';
Yu's avatar
Yu committed
9
import IconFont from '@/components/IconFont';
jim's avatar
jim committed
10 11 12 13 14

const { SubMenu } = Menu;

// Allow menu.js config icon as string or ReactNode
//   icon: 'setting',
Yu's avatar
Yu committed
15
//   icon: 'icon-geren' #For Iconfont ,
jim's avatar
jim committed
16 17
//   icon: 'http://demo.com/icon.png',
//   icon: <Icon type="setting" />,
jim's avatar
jim committed
18
const getIcon = icon => {
jim's avatar
jim committed
19
  if (typeof icon === 'string') {
Yu's avatar
Yu committed
20 21 22 23 24 25
    if (isUrl(icon)) {
      return <Icon component={() => <img src={icon} alt="icon" className={styles.icon} />} />;
    }
    if (icon.startsWith('icon-')) {
      return <IconFont type={icon} />;
    }
jim's avatar
jim committed
26 27 28 29 30
    return <Icon type={icon} />;
  }
  return icon;
};

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

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

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

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

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

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

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