"src/pages/Account/Center/Center.js" did not exist on "bb35833eb43c7491f2811e41b87329b0e7ac6577"
BaseMenu.js 4.14 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';
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',
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') {
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
   */
陈帅's avatar
陈帅 committed
36
  getNavMenuItems = (menusData, parent) => {
jim's avatar
jim committed
37 38 39 40 41
    if (!menusData) {
      return [];
    }
    return menusData
      .filter(item => item.name && !item.hideInMenu)
陈小聪's avatar
陈小聪 committed
42
      .map(item => this.getSubMenuOrItem(item, parent))
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>
    );
  }
}