BaseMenu.tsx 5.24 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';
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" />,
何乐's avatar
何乐 committed
19
const getIcon = (icon?: string | React.ReactNode) => {
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;
};

何乐's avatar
何乐 committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/**
 * @type R: is route
 */
export interface MenuDataItem<R extends boolean = false> {
  authority?: string[] | string;
  children?: MenuDataItem[];
  hideChildrenInMenu?: boolean;
  hideInMenu?: boolean;
  icon?: string;
  locale?: string;
  name?: string;
  path: string;
  routes?: R extends true ? MenuDataItem<R>[] : never;
  [key: string]: any;
}
47

何乐's avatar
何乐 committed
48 49 50
export interface BaseMenuProps {
  className?: string;
  collapsed?: boolean;
51
  flatMenuKeys?: any[];
何乐's avatar
何乐 committed
52
  handleOpenChange?: (openKeys: string[]) => void;
53
  isMobile?: boolean;
何乐's avatar
何乐 committed
54 55
  location?: Location;
  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 65 66 67 68 69 70 71 72 73 74 75
export default class BaseMenu extends Component<BaseMenuProps> {
  static defaultProps: BaseMenuProps = {
    flatMenuKeys: [],
    location: window.location,
    onCollapse: () => void 0,
    isMobile: false,
    openKeys: [],
    collapsed: false,
    handleOpenChange: () => void 0,
    menuData: [],
    onOpenChange: () => void 0,
  };
76

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

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

jim's avatar
jim committed
95 96 97
  /**
   * get SubMenu or Item
   */
何乐's avatar
何乐 committed
98 99 100 101 102 103
  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
104 105 106 107 108 109
      return (
        <SubMenu
          title={
            item.icon ? (
              <span>
                {getIcon(item.icon)}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
110
                <span>{name}</span>
jim's avatar
jim committed
111 112
              </span>
            ) : (
何乐's avatar
何乐 committed
113
              item.name
jim's avatar
jim committed
114 115 116 117
            )
          }
          key={item.path}
        >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
118
          {this.getNavMenuItems(item.children)}
jim's avatar
jim committed
119 120 121
        </SubMenu>
      );
    }
122
    return <Menu.Item key={item.path}>{this.getMenuItemPath(item)}</Menu.Item>;
jim's avatar
jim committed
123
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
124

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

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

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

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