BaseMenu.tsx 4.86 KB
Newer Older
1 2 3
import IconFont from '@/components/IconFont';
import { isUrl } from '@/utils/utils';
import { Icon, Menu } from 'antd';
4
import classNames from 'classnames';
5 6
import * as H from 'history';
import React, { Component } from 'react';
zinkey's avatar
zinkey committed
7
import Link from 'umi/link';
jim's avatar
jim committed
8
import { urlToList } from '../_utils/pathTools';
jim's avatar
jim committed
9
import styles from './index.less';
10
import { getMenuMatches } from './SiderMenuUtils';
jim's avatar
jim committed
11 12 13 14 15

const { SubMenu } = Menu;

// Allow menu.js config icon as string or ReactNode
//   icon: 'setting',
Yu's avatar
Yu committed
16
//   icon: 'icon-geren' #For Iconfont ,
jim's avatar
jim committed
17 18
//   icon: 'http://demo.com/icon.png',
//   icon: <Icon type="setting" />,
jim's avatar
jim committed
19
const getIcon = icon => {
jim's avatar
jim committed
20
  if (typeof icon === 'string') {
Yu's avatar
Yu committed
21 22 23 24 25 26
    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
27 28 29 30 31
    return <Icon type={icon} />;
  }
  return icon;
};

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
export declare type CollapseType = 'clickTrigger' | 'responsive';
export declare type SiderTheme = 'light' | 'dark';
export declare type MenuMode =
  | 'vertical'
  | 'vertical-left'
  | 'vertical-right'
  | 'horizontal'
  | 'inline';

interface BaseMenuProps {
  flatMenuKeys?: any[];
  location?: H.Location;
  onCollapse?: (collapsed: boolean, type?: CollapseType) => void;
  isMobile?: boolean;
  openKeys?: any;
  theme?: SiderTheme;
  mode?: MenuMode;
  className?: string;
  collapsed?: boolean;
  handleOpenChange?: (openKeys: any[]) => void;
  menuData?: any[];
  style?: React.CSSProperties;
  onOpenChange?: (openKeys: string[]) => void;
}

interface BaseMenuState {}

export default class BaseMenu extends Component<BaseMenuProps, BaseMenuState> {
jim's avatar
jim committed
60 61 62 63
  /**
   * θŽ·εΎ—θœε•ε­θŠ‚η‚Ή
   * @memberof SiderMenu
   */
64
  getNavMenuItems: (menusData: any[]) => any[] = menusData => {
jim's avatar
jim committed
65 66 67 68 69
    if (!menusData) {
      return [];
    }
    return menusData
      .filter(item => item.name && !item.hideInMenu)
Yu's avatar
Yu committed
70
      .map(item => this.getSubMenuOrItem(item))
jim's avatar
jim committed
71 72
      .filter(item => item);
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
73

jim's avatar
jim committed
74
  // Get the currently selected menu
75 76 77 78
  getSelectedMenuKeys = pathname => {
    const { flatMenuKeys } = this.props;
    return urlToList(pathname).map(itemPath => getMenuMatches(flatMenuKeys, itemPath).pop());
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
79

jim's avatar
jim committed
80 81 82
  /**
   * get SubMenu or Item
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
83
  getSubMenuOrItem = item => {
afc163's avatar
afc163 committed
84 85
    // doc: add hideChildrenInMenu
    if (item.children && !item.hideChildrenInMenu && item.children.some(child => child.name)) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
86
      const { name } = item;
jim's avatar
jim committed
87 88 89 90 91 92
      return (
        <SubMenu
          title={
            item.icon ? (
              <span>
                {getIcon(item.icon)}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
93
                <span>{name}</span>
jim's avatar
jim committed
94 95
              </span>
            ) : (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
96
              name
jim's avatar
jim committed
97 98 99 100
            )
          }
          key={item.path}
        >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
101
          {this.getNavMenuItems(item.children)}
jim's avatar
jim committed
102 103 104
        </SubMenu>
      );
    }
105
    return <Menu.Item key={item.path}>{this.getMenuItemPath(item)}</Menu.Item>;
jim's avatar
jim committed
106
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
107

jim's avatar
jim committed
108 109 110 111 112
  /**
   * εˆ€ζ–­ζ˜―ε¦ζ˜―httpι“ΎζŽ₯.θΏ”ε›ž Link ζˆ– a
   * Judge whether it is http link.return a or Link
   * @memberof SiderMenu
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
113
  getMenuItemPath = item => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
114
    const { name } = item;
jim's avatar
jim committed
115 116
    const itemPath = this.conversionPath(item.path);
    const icon = getIcon(item.icon);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
117
    const { target } = item;
jim's avatar
jim committed
118 119 120 121 122 123 124 125 126
    // Is it a http link
    if (/^https?:\/\//.test(itemPath)) {
      return (
        <a href={itemPath} target={target}>
          {icon}
          <span>{name}</span>
        </a>
      );
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
127
    const { location, isMobile, onCollapse } = this.props;
jim's avatar
jim committed
128 129 130 131
    return (
      <Link
        to={itemPath}
        target={target}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
132
        replace={itemPath === location.pathname}
jim's avatar
jim committed
133
        onClick={
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
134
          isMobile
jim's avatar
jim committed
135
            ? () => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
136
                onCollapse(true);
jim's avatar
jim committed
137 138 139 140 141 142 143 144 145
              }
            : undefined
        }
      >
        {icon}
        <span>{name}</span>
      </Link>
    );
  };
146

jim's avatar
jim committed
147
  conversionPath = path => {
jim's avatar
jim committed
148 149 150
    if (path && path.indexOf('http') === 0) {
      return path;
    }
151
    return `/${path || ''}`.replace(/\/+/g, '/');
jim's avatar
jim committed
152
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
153

jim's avatar
jim committed
154
  render() {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
155 156 157 158 159
    const {
      openKeys,
      theme,
      mode,
      location: { pathname },
160
      className,
Ez-Z's avatar
Ez-Z committed
161
      collapsed,
162 163 164
      handleOpenChange,
      style,
      menuData,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
165
    } = this.props;
jim's avatar
jim committed
166
    // if pathname can't match, use the nearest parent's key
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
167
    let selectedKeys = this.getSelectedMenuKeys(pathname);
jim's avatar
jim committed
168 169 170
    if (!selectedKeys.length && openKeys) {
      selectedKeys = [openKeys[openKeys.length - 1]];
    }
jim's avatar
jim committed
171
    let props = {};
Ez-Z's avatar
Ez-Z committed
172
    if (openKeys && !collapsed) {
jim's avatar
jim committed
173
      props = {
Ez-Z's avatar
Ez-Z committed
174
        openKeys: openKeys.length === 0 ? [...selectedKeys] : openKeys,
jim's avatar
jim committed
175 176
      };
    }
177 178 179 180
    const cls = classNames(className, {
      'top-nav-menu': mode === 'horizontal',
    });

jim's avatar
jim committed
181 182 183
    return (
      <Menu
        key="Menu"
jim's avatar
jim committed
184 185
        mode={mode}
        theme={theme}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
186
        onOpenChange={handleOpenChange}
jim's avatar
jim committed
187
        selectedKeys={selectedKeys}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
188
        style={style}
189
        className={cls}
jim's avatar
jim committed
190
        {...props}
jim's avatar
jim committed
191
      >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
192
        {this.getNavMenuItems(menuData)}
jim's avatar
jim committed
193 194 195 196
      </Menu>
    );
  }
}