SiderMenu.js 4.95 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';
jim's avatar
jim committed
5
import BaseMeun, { getMeunMatcheys } from './BaseMeun';
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 15 16
// Allow menu.js config icon as string or ReactNode
//   icon: 'setting',
//   icon: 'http://demo.com/icon.png',
//   icon: <Icon type="setting" />,
const getIcon = (icon) => {
  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;
};

25

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