SiderMenu.js 4.36 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
/**
 * θŽ·εΎ—θœε•ε­θŠ‚η‚Ή
 * @memberof SiderMenu
 */
const getDefaultCollapsedSubMenus = props => {
jim's avatar
jim committed
16 17 18
  const {
    location: { pathname },
  } = props;
jim's avatar
jim committed
19 20 21 22 23 24 25
  return urlToList(pathname)
    .map(item => {
      return getMenuMatches(props.flatMenuKeys, item)[0];
    })
    .filter(item => item);
};

26 27 28 29
// 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
30
const getIcon = icon => {
31
  if (typeof icon === 'string' && icon.indexOf('http') === 0) {
32
    return <img src={icon} alt="icon" className={`${styles.icon} sider-menu-item-img`} />;
33 34 35 36 37 38 39
  }
  if (typeof icon === 'string') {
    return <Icon type={icon} />;
  }
  return icon;
};

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

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