SiderMenu.js 4.35 KB
Newer Older
jiang's avatar
jiang committed
1 2 3 4
import React, { PureComponent } from 'react';
import { Layout, Menu, Icon } from 'antd';
import { Link } from 'dva/router';
import styles from './index.less';
Erwin Zhang's avatar
Erwin Zhang committed
5
import BaseMenu, { getMenuMatches } from './BaseMenu';
jim's avatar
jim committed
6
import { urlToList } from '../_utils/pathTools';
jiang's avatar
jiang committed
7

jim's avatar
jim committed
8
const { Sider } = Layout;
jiang's avatar
jiang committed
9 10
const { SubMenu } = Menu;

jim's avatar
jim committed
11 12 13 14 15 16 17 18 19 20 21 22 23
/**
 * θŽ·εΎ—θœε•ε­θŠ‚η‚Ή
 * @memberof SiderMenu
 */
const getDefaultCollapsedSubMenus = props => {
  const { location: { pathname } } = props;
  return urlToList(pathname)
    .map(item => {
      return getMenuMatches(props.flatMenuKeys, item)[0];
    })
    .filter(item => item);
};

24 25 26 27
// Allow menu.js config icon as string or ReactNode
//   icon: 'setting',
//   icon: 'http://demo.com/icon.png',
//   icon: <Icon type="setting" />,
jim's avatar
jim committed
28
const getIcon = icon => {
29
  if (typeof icon === 'string' && icon.indexOf('http') === 0) {
30
    return <img src={icon} alt="icon" className={`${styles.icon} sider-menu-item-img`} />;
31 32 33 34 35 36 37
  }
  if (typeof icon === 'string') {
    return <Icon type={icon} />;
  }
  return icon;
};

jiang's avatar
jiang committed
38
export default class SiderMenu extends PureComponent {
jim's avatar
jim committed
39 40 41 42 43
  static getDerivedStateFromProps(nextProps) {
    return {
      openKeys: getDefaultCollapsedSubMenus(nextProps),
    };
  }
jiang's avatar
jiang committed
44 45 46
  constructor(props) {
    super(props);
    this.state = {
jim's avatar
jim committed
47
      openKeys: getDefaultCollapsedSubMenus(props),
jiang's avatar
jiang committed
48 49
    };
  }
jim's avatar
jim committed
50

51
  /**
jim's avatar
jim committed
52 53 54
   * Convert pathname to openKeys
   * /list/search/articles = > ['list','/list/search']
   * @param  props
55 56 57
   * εˆ€ζ–­ζ˜―ε¦ζ˜―httpι“ΎζŽ₯.θΏ”ε›ž Link ζˆ– a
   * Judge whether it is http link.return a or Link
   * @memberof SiderMenu
58
   */
jim's avatar
jim committed
59
  getMenuItemPath = item => {
ddcat1115's avatar
ddcat1115 committed
60 61 62 63 64 65 66
    const itemPath = this.conversionPath(item.path);
    const icon = getIcon(item.icon);
    const { target, name } = item;
    // Is it a http link
    if (/^https?:\/\//.test(itemPath)) {
      return (
        <a href={itemPath} target={target}>
67 68
          {icon}
          <span>{name}</span>
ddcat1115's avatar
ddcat1115 committed
69 70 71 72 73 74 75 76
        </a>
      );
    }
    return (
      <Link
        to={itemPath}
        target={target}
        replace={itemPath === this.props.location.pathname}
77 78 79 80 81 82 83
        onClick={
          this.props.isMobile
            ? () => {
                this.props.onCollapse(true);
              }
            : undefined
        }
ddcat1115's avatar
ddcat1115 committed
84
      >
85 86
        {icon}
        <span>{name}</span>
ddcat1115's avatar
ddcat1115 committed
87 88
      </Link>
    );
89
  };
ddcat1115's avatar
ddcat1115 committed
90 91 92
  /**
   * get SubMenu or Item
   */
jim's avatar
jim committed
93
  getSubMenuOrItem = item => {
ddcat1115's avatar
ddcat1115 committed
94
    if (item.children && item.children.some(child => child.name)) {
hzq's avatar
hzq committed
95 96 97 98 99 100 101 102 103 104 105 106
      const childrenItems = this.getNavMenuItems(item.children);
      // ε½“ζ— ε­θœε•ζ—Άε°±δΈε±•η€Ίθœε•
      if (childrenItems && childrenItems.length > 0) {
        return (
          <SubMenu
            title={
              item.icon ? (
                <span>
                  {getIcon(item.icon)}
                  <span>{item.name}</span>
                </span>
              ) : (
jim's avatar
jim committed
107 108
                item.name
              )
hzq's avatar
hzq committed
109 110 111 112 113
            }
            key={item.path}
          >
            {childrenItems}
          </SubMenu>
hzq's avatar
hzq committed
114
        );
hzq's avatar
hzq committed
115
      }
hzq's avatar
hzq committed
116
      return null;
ddcat1115's avatar
ddcat1115 committed
117
    } else {
jim's avatar
jim committed
118
      return <Menu.Item key={item.path}>{this.getMenuItemPath(item)}</Menu.Item>;
ddcat1115's avatar
ddcat1115 committed
119
    }
120
  };
jim's avatar
jim committed
121
  isMainMenu = key => {
jim's avatar
jim committed
122
    return this.props.menuData.some(item => key && (item.key === key || item.path === key));
jim's avatar
jim committed
123 124
  };
  handleOpenChange = openKeys => {
ddcat1115's avatar
ddcat1115 committed
125 126
    const lastOpenKey = openKeys[openKeys.length - 1];
    const moreThanOne = openKeys.filter(openKey => this.isMainMenu(openKey)).length > 1;
jiang's avatar
jiang committed
127
    this.setState({
ddcat1115's avatar
ddcat1115 committed
128
      openKeys: moreThanOne ? [lastOpenKey] : [...openKeys],
jiang's avatar
jiang committed
129
    });
130
  };
jiang's avatar
jiang committed
131
  render() {
jim's avatar
jim committed
132
    const { logo, collapsed, onCollapse, theme } = this.props;
133
    const { openKeys } = this.state;
jim's avatar
jim committed
134
    const defaultProps = collapsed ? {} : { openKeys };
jiang's avatar
jiang committed
135 136 137 138 139
    return (
      <Sider
        trigger={null}
        collapsible
        collapsed={collapsed}
140
        breakpoint="lg"
jiang's avatar
jiang committed
141 142
        onCollapse={onCollapse}
        width={256}
jim's avatar
jim committed
143
        className={`${styles.sider} ${theme === 'ligth' ? styles.ligth : ''}`}
jiang's avatar
jiang committed
144
      >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
145
        <div className={styles.logo} key="logo">
jiang's avatar
jiang committed
146 147 148 149 150
          <Link to="/">
            <img src={logo} alt="logo" />
            <h1>Ant Design Pro</h1>
          </Link>
        </div>
Erwin Zhang's avatar
Erwin Zhang committed
151
        <BaseMenu
jim's avatar
jim committed
152
          {...this.props}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
153
          key="Menu"
jiang's avatar
jiang committed
154
          mode="inline"
jim's avatar
jim committed
155
          handleOpenChange={this.handleOpenChange}
jiang's avatar
jiang committed
156 157
          onOpenChange={this.handleOpenChange}
          style={{ padding: '16px 0', width: '100%' }}
jim's avatar
jim committed
158
          {...defaultProps}
jim's avatar
jim committed
159
        />
jiang's avatar
jiang committed
160 161 162 163
      </Sider>
    );
  }
}