SiderMenu.js 4.89 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;

11 12 13 14
// 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
15
const getIcon = icon => {
16
  if (typeof icon === 'string' && icon.indexOf('http') === 0) {
17
    return <img src={icon} alt="icon" className={`${styles.icon} sider-menu-item-img`} />;
18 19 20 21 22 23 24
  }
  if (typeof icon === 'string') {
    return <Icon type={icon} />;
  }
  return icon;
};

jiang's avatar
jiang committed
25 26 27
export default class SiderMenu extends PureComponent {
  constructor(props) {
    super(props);
ddcat1115's avatar
ddcat1115 committed
28
    this.menus = props.menuData;
29
    this.flatMenuKeys = this.getFlatMenuKeys(props.menuData);
jiang's avatar
jiang committed
30 31 32 33
    this.state = {
      openKeys: this.getDefaultCollapsedSubMenus(props),
    };
  }
34 35 36 37 38 39 40
  componentWillReceiveProps(nextProps) {
    if (nextProps.location.pathname !== this.props.location.pathname) {
      this.setState({
        openKeys: this.getDefaultCollapsedSubMenus(nextProps),
      });
    }
  }
41 42 43 44 45
  /**
   * Recursively flatten the data
   * [{path:string},{path:string}] => {path,path2}
   * @param  menus
   */
jiang's avatar
jiang committed
46 47
  getFlatMenuKeys(menus) {
    let keys = [];
jim's avatar
jim committed
48
    menus.forEach(item => {
jiang's avatar
jiang committed
49 50 51
      if (item.children) {
        keys = keys.concat(this.getFlatMenuKeys(item.children));
      }
52
      keys.push(item.path);
jiang's avatar
jiang committed
53 54 55
    });
    return keys;
  }
56
  /**
jim's avatar
jim committed
57 58 59
   * Convert pathname to openKeys
   * /list/search/articles = > ['list','/list/search']
   * @param  props
60 61 62
   * εˆ€ζ–­ζ˜―ε¦ζ˜―httpι“ΎζŽ₯.θΏ”ε›ž Link ζˆ– a
   * Judge whether it is http link.return a or Link
   * @memberof SiderMenu
63
   */
jim's avatar
jim committed
64
  getMenuItemPath = item => {
ddcat1115's avatar
ddcat1115 committed
65 66 67 68 69 70 71
    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}>
72 73
          {icon}
          <span>{name}</span>
ddcat1115's avatar
ddcat1115 committed
74 75 76 77 78 79 80 81
        </a>
      );
    }
    return (
      <Link
        to={itemPath}
        target={target}
        replace={itemPath === this.props.location.pathname}
82 83 84 85 86 87 88
        onClick={
          this.props.isMobile
            ? () => {
                this.props.onCollapse(true);
              }
            : undefined
        }
ddcat1115's avatar
ddcat1115 committed
89
      >
90 91
        {icon}
        <span>{name}</span>
ddcat1115's avatar
ddcat1115 committed
92 93
      </Link>
    );
94
  };
ddcat1115's avatar
ddcat1115 committed
95 96 97
  /**
   * get SubMenu or Item
   */
jim's avatar
jim committed
98
  getSubMenuOrItem = item => {
ddcat1115's avatar
ddcat1115 committed
99
    if (item.children && item.children.some(child => child.name)) {
hzq's avatar
hzq committed
100 101 102 103 104 105 106 107 108 109 110 111
      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
112 113
                item.name
              )
hzq's avatar
hzq committed
114 115 116 117 118
            }
            key={item.path}
          >
            {childrenItems}
          </SubMenu>
hzq's avatar
hzq committed
119
        );
hzq's avatar
hzq committed
120
      }
hzq's avatar
hzq committed
121
      return null;
ddcat1115's avatar
ddcat1115 committed
122
    } else {
jim's avatar
jim committed
123
      return <Menu.Item key={item.path}>{this.getMenuItemPath(item)}</Menu.Item>;
ddcat1115's avatar
ddcat1115 committed
124
    }
125
  };
ddcat1115's avatar
ddcat1115 committed
126
  /**
127 128 129
   * θŽ·εΎ—θœε•ε­θŠ‚η‚Ή
   * @memberof SiderMenu
   */
jim's avatar
jim committed
130 131 132
  getDefaultCollapsedSubMenus(props) {
    const { location: { pathname } } = props || this.props;
    return urlToList(pathname)
jim's avatar
jim committed
133
      .map(item => {
Erwin Zhang's avatar
Erwin Zhang committed
134
        return getMenuMatches(this.flatMenuKeys, item)[0];
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
135
      })
136
      .filter(item => item);
ddcat1115's avatar
ddcat1115 committed
137
  }
jim's avatar
jim committed
138 139 140 141
  isMainMenu = key => {
    return this.menus.some(item => key && (item.key === key || item.path === key));
  };
  handleOpenChange = openKeys => {
ddcat1115's avatar
ddcat1115 committed
142 143
    const lastOpenKey = openKeys[openKeys.length - 1];
    const moreThanOne = openKeys.filter(openKey => this.isMainMenu(openKey)).length > 1;
jiang's avatar
jiang committed
144
    this.setState({
ddcat1115's avatar
ddcat1115 committed
145
      openKeys: moreThanOne ? [lastOpenKey] : [...openKeys],
jiang's avatar
jiang committed
146
    });
147
  };
jiang's avatar
jiang committed
148
  render() {
jim's avatar
jim committed
149
    const { logo, collapsed, onCollapse, theme } = this.props;
150
    const { openKeys } = this.state;
jim's avatar
jim committed
151
    const defaultProps = collapsed ? {} : { openKeys };
jiang's avatar
jiang committed
152 153 154 155 156
    return (
      <Sider
        trigger={null}
        collapsible
        collapsed={collapsed}
157
        breakpoint="lg"
jiang's avatar
jiang committed
158 159
        onCollapse={onCollapse}
        width={256}
jim's avatar
jim committed
160
        className={`${styles.sider} ${theme === 'ligth' ? styles.ligth : ''}`}
jiang's avatar
jiang committed
161
      >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
162
        <div className={styles.logo} key="logo">
jiang's avatar
jiang committed
163 164 165 166 167
          <Link to="/">
            <img src={logo} alt="logo" />
            <h1>Ant Design Pro</h1>
          </Link>
        </div>
Erwin Zhang's avatar
Erwin Zhang committed
168
        <BaseMenu
jim's avatar
jim committed
169
          {...this.props}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
170
          key="Menu"
jiang's avatar
jiang committed
171
          mode="inline"
jim's avatar
jim committed
172
          handleOpenChange={this.handleOpenChange}
jiang's avatar
jiang committed
173 174
          onOpenChange={this.handleOpenChange}
          style={{ padding: '16px 0', width: '100%' }}
jim's avatar
jim committed
175
          {...defaultProps}
jim's avatar
jim committed
176
        />
jiang's avatar
jiang committed
177 178 179 180
      </Sider>
    );
  }
}