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

const { SubMenu } = Menu;

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

33
export interface MenuDataItem {
何乐's avatar
何乐 committed
34 35 36 37 38 39 40 41 42 43
  authority?: string[] | string;
  children?: MenuDataItem[];
  hideChildrenInMenu?: boolean;
  hideInMenu?: boolean;
  icon?: string;
  locale?: string;
  name?: string;
  path: string;
  [key: string]: any;
}
44

45 46 47 48 49
export interface Route extends MenuDataItem {
  routes?: Route[];
}

export interface BaseMenuProps extends Partial<RouterTypes<Route>> {
何乐's avatar
何乐 committed
50 51
  className?: string;
  collapsed?: boolean;
52
  flatMenuKeys?: any[];
何乐's avatar
何乐 committed
53
  handleOpenChange?: (openKeys: string[]) => void;
54
  isMobile?: boolean;
何乐's avatar
何乐 committed
55
  menuData?: MenuDataItem[];
56
  mode?: MenuMode;
何乐's avatar
何乐 committed
57
  onCollapse?: (collapsed: boolean) => void;
58
  onOpenChange?: (openKeys: string[]) => void;
何乐's avatar
何乐 committed
59 60 61
  openKeys?: string[];
  style?: React.CSSProperties;
  theme?: MenuTheme;
62 63
}

何乐's avatar
何乐 committed
64
export default class BaseMenu extends Component<BaseMenuProps> {
65
  static defaultProps: Partial<BaseMenuProps> = {
何乐's avatar
何乐 committed
66 67 68 69 70 71 72 73 74
    flatMenuKeys: [],
    onCollapse: () => void 0,
    isMobile: false,
    openKeys: [],
    collapsed: false,
    handleOpenChange: () => void 0,
    menuData: [],
    onOpenChange: () => void 0,
  };
75

jim's avatar
jim committed
76 77 78
  /**
   * θŽ·εΎ—θœε•ε­θŠ‚η‚Ή
   */
何乐's avatar
何乐 committed
79
  getNavMenuItems = (menusData: MenuDataItem[] = []): React.ReactNode[] => {
jim's avatar
jim committed
80 81
    return menusData
      .filter(item => item.name && !item.hideInMenu)
Yu's avatar
Yu committed
82
      .map(item => this.getSubMenuOrItem(item))
jim's avatar
jim committed
83 84
      .filter(item => item);
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
85

jim's avatar
jim committed
86
  // Get the currently selected menu
何乐's avatar
何乐 committed
87
  getSelectedMenuKeys = (pathname: string): string[] => {
88
    const { flatMenuKeys } = this.props;
何乐's avatar
何乐 committed
89 90 91
    return urlToList(pathname)
      .map(itemPath => getMenuMatches(flatMenuKeys, itemPath).pop())
      .filter(item => item) as string[];
92
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
93

jim's avatar
jim committed
94 95 96
  /**
   * get SubMenu or Item
   */
何乐's avatar
何乐 committed
97 98 99 100 101 102
  getSubMenuOrItem = (item: MenuDataItem): React.ReactNode => {
    if (
      Array.isArray(item.children) &&
      !item.hideChildrenInMenu &&
      item.children.some(child => (child.name ? true : false))
    ) {
jim's avatar
jim committed
103 104 105 106 107 108
      return (
        <SubMenu
          title={
            item.icon ? (
              <span>
                {getIcon(item.icon)}
109
                <span>{item.name}</span>
jim's avatar
jim committed
110 111
              </span>
            ) : (
何乐's avatar
何乐 committed
112
              item.name
jim's avatar
jim committed
113 114 115 116
            )
          }
          key={item.path}
        >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
117
          {this.getNavMenuItems(item.children)}
jim's avatar
jim committed
118 119 120
        </SubMenu>
      );
    }
121
    return <Menu.Item key={item.path}>{this.getMenuItemPath(item)}</Menu.Item>;
jim's avatar
jim committed
122
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
123

jim's avatar
jim committed
124 125 126 127 128
  /**
   * εˆ€ζ–­ζ˜―ε¦ζ˜―httpι“ΎζŽ₯.θΏ”ε›ž Link ζˆ– a
   * Judge whether it is http link.return a or Link
   * @memberof SiderMenu
   */
何乐's avatar
何乐 committed
129
  getMenuItemPath = (item: MenuDataItem) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
130
    const { name } = item;
jim's avatar
jim committed
131 132
    const itemPath = this.conversionPath(item.path);
    const icon = getIcon(item.icon);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
133
    const { target } = item;
jim's avatar
jim committed
134 135 136 137 138 139 140 141 142
    // Is it a http link
    if (/^https?:\/\//.test(itemPath)) {
      return (
        <a href={itemPath} target={target}>
          {icon}
          <span>{name}</span>
        </a>
      );
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
143
    const { location, isMobile, onCollapse } = this.props;
jim's avatar
jim committed
144 145 146 147
    return (
      <Link
        to={itemPath}
        target={target}
何乐's avatar
何乐 committed
148 149
        replace={itemPath === location!.pathname}
        onClick={isMobile ? () => onCollapse!(true) : void 0}
jim's avatar
jim committed
150 151 152 153 154 155
      >
        {icon}
        <span>{name}</span>
      </Link>
    );
  };
156

何乐's avatar
何乐 committed
157
  conversionPath = (path: string) => {
jim's avatar
jim committed
158 159 160
    if (path && path.indexOf('http') === 0) {
      return path;
    }
161
    return `/${path || ''}`.replace(/\/+/g, '/');
jim's avatar
jim committed
162
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
163

jim's avatar
jim committed
164
  render() {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
165 166 167 168
    const {
      openKeys,
      theme,
      mode,
何乐's avatar
何乐 committed
169
      location,
170
      className,
Ez-Z's avatar
Ez-Z committed
171
      collapsed,
172 173 174
      handleOpenChange,
      style,
      menuData,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
175
    } = this.props;
jim's avatar
jim committed
176
    // if pathname can't match, use the nearest parent's key
何乐's avatar
何乐 committed
177
    let selectedKeys = this.getSelectedMenuKeys(location!.pathname);
jim's avatar
jim committed
178 179 180
    if (!selectedKeys.length && openKeys) {
      selectedKeys = [openKeys[openKeys.length - 1]];
    }
jim's avatar
jim committed
181
    let props = {};
Ez-Z's avatar
Ez-Z committed
182
    if (openKeys && !collapsed) {
jim's avatar
jim committed
183
      props = {
Ez-Z's avatar
Ez-Z committed
184
        openKeys: openKeys.length === 0 ? [...selectedKeys] : openKeys,
jim's avatar
jim committed
185 186
      };
    }
187 188 189 190
    const cls = classNames(className, {
      'top-nav-menu': mode === 'horizontal',
    });

jim's avatar
jim committed
191 192 193
    return (
      <Menu
        key="Menu"
jim's avatar
jim committed
194 195
        mode={mode}
        theme={theme}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
196
        onOpenChange={handleOpenChange}
jim's avatar
jim committed
197
        selectedKeys={selectedKeys}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
198
        style={style}
199
        className={cls}
jim's avatar
jim committed
200
        {...props}
jim's avatar
jim committed
201
      >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
202
        {this.getNavMenuItems(menuData)}
jim's avatar
jim committed
203 204 205 206
      </Menu>
    );
  }
}