SiderMenu.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import React, { PureComponent } from 'react';
  2. import { Layout, Menu, Icon } from 'antd';
  3. import pathToRegexp from 'path-to-regexp';
  4. import { Link } from 'dva/router';
  5. import styles from './index.less';
  6. import { urlToList } from '../utils/pathTools';
  7. const { Sider } = Layout;
  8. const { SubMenu } = Menu;
  9. // Allow menu.js config icon as string or ReactNode
  10. // icon: 'setting',
  11. // icon: 'http://demo.com/icon.png',
  12. // icon: <Icon type="setting" />,
  13. const getIcon = (icon) => {
  14. if (typeof icon === 'string' && icon.indexOf('http') === 0) {
  15. return <img src={icon} alt="icon" className={styles.icon} />;
  16. }
  17. if (typeof icon === 'string') {
  18. return <Icon type={icon} />;
  19. }
  20. return icon;
  21. };
  22. export const getMeunMatcheys = (flatMenuKeys, path) => {
  23. return flatMenuKeys.filter((item) => {
  24. return pathToRegexp(item).test(path);
  25. });
  26. };
  27. export default class SiderMenu extends PureComponent {
  28. constructor(props) {
  29. super(props);
  30. this.menus = props.menuData;
  31. this.flatMenuKeys = this.getFlatMenuKeys(props.menuData);
  32. this.state = {
  33. openKeys: this.getDefaultCollapsedSubMenus(props),
  34. };
  35. }
  36. componentWillReceiveProps(nextProps) {
  37. if (nextProps.location.pathname !== this.props.location.pathname) {
  38. this.setState({
  39. openKeys: this.getDefaultCollapsedSubMenus(nextProps),
  40. });
  41. }
  42. }
  43. /**
  44. * Convert pathname to openKeys
  45. * /list/search/articles = > ['list','/list/search']
  46. * @param props
  47. */
  48. getDefaultCollapsedSubMenus(props) {
  49. const { location: { pathname } } = props || this.props;
  50. return urlToList(pathname)
  51. .map((item) => {
  52. return getMeunMatcheys(this.flatMenuKeys, item)[0];
  53. })
  54. .filter(item => item);
  55. }
  56. /**
  57. * Recursively flatten the data
  58. * [{path:string},{path:string}] => {path,path2}
  59. * @param menus
  60. */
  61. getFlatMenuKeys(menus) {
  62. let keys = [];
  63. menus.forEach((item) => {
  64. if (item.children) {
  65. keys = keys.concat(this.getFlatMenuKeys(item.children));
  66. }
  67. keys.push(item.path);
  68. });
  69. return keys;
  70. }
  71. /**
  72. * 判断是否是http链接.返回 Link 或 a
  73. * Judge whether it is http link.return a or Link
  74. * @memberof SiderMenu
  75. */
  76. getMenuItemPath = (item) => {
  77. const itemPath = this.conversionPath(item.path);
  78. const icon = getIcon(item.icon);
  79. const { target, name } = item;
  80. // Is it a http link
  81. if (/^https?:\/\//.test(itemPath)) {
  82. return (
  83. <a href={itemPath} target={target}>
  84. {icon}
  85. <span>{name}</span>
  86. </a>
  87. );
  88. }
  89. return (
  90. <Link
  91. to={itemPath}
  92. target={target}
  93. replace={itemPath === this.props.location.pathname}
  94. onClick={
  95. this.props.isMobile
  96. ? () => {
  97. this.props.onCollapse(true);
  98. }
  99. : undefined
  100. }
  101. >
  102. {icon}
  103. <span>{name}</span>
  104. </Link>
  105. );
  106. };
  107. /**
  108. * get SubMenu or Item
  109. */
  110. getSubMenuOrItem = (item) => {
  111. if (item.children && item.children.some(child => child.name)) {
  112. const childrenItems = this.getNavMenuItems(item.children);
  113. // 当无子菜单时就不展示菜单
  114. if (childrenItems && childrenItems.length > 0) {
  115. return (
  116. <SubMenu
  117. title={
  118. item.icon ? (
  119. <span>
  120. {getIcon(item.icon)}
  121. <span>{item.name}</span>
  122. </span>
  123. ) : (
  124. item.name
  125. )
  126. }
  127. key={item.path}
  128. >
  129. {childrenItems}
  130. </SubMenu>
  131. );
  132. }
  133. return null;
  134. } else {
  135. return (
  136. <Menu.Item key={item.path}>{this.getMenuItemPath(item)}</Menu.Item>
  137. );
  138. }
  139. };
  140. /**
  141. * 获得菜单子节点
  142. * @memberof SiderMenu
  143. */
  144. getNavMenuItems = (menusData) => {
  145. if (!menusData) {
  146. return [];
  147. }
  148. return menusData
  149. .filter(item => item.name && !item.hideInMenu)
  150. .map((item) => {
  151. // make dom
  152. const ItemDom = this.getSubMenuOrItem(item);
  153. return this.checkPermissionItem(item.authority, ItemDom);
  154. })
  155. .filter(item => item);
  156. };
  157. // Get the currently selected menu
  158. getSelectedMenuKeys = () => {
  159. const { location: { pathname } } = this.props;
  160. return urlToList(pathname).map(itemPath =>
  161. getMeunMatcheys(this.flatMenuKeys, itemPath).pop(),
  162. );
  163. };
  164. // conversion Path
  165. // 转化路径
  166. conversionPath = (path) => {
  167. if (path && path.indexOf('http') === 0) {
  168. return path;
  169. } else {
  170. return `/${path || ''}`.replace(/\/+/g, '/');
  171. }
  172. };
  173. // permission to check
  174. checkPermissionItem = (authority, ItemDom) => {
  175. if (this.props.Authorized && this.props.Authorized.check) {
  176. const { check } = this.props.Authorized;
  177. return check(authority, ItemDom);
  178. }
  179. return ItemDom;
  180. };
  181. isMainMenu = (key) => {
  182. return this.menus.some(
  183. item =>
  184. key && (item.key === key || item.path === key),
  185. );
  186. }
  187. handleOpenChange = (openKeys) => {
  188. const lastOpenKey = openKeys[openKeys.length - 1];
  189. const moreThanOne = openKeys.filter(openKey => this.isMainMenu(openKey)).length > 1;
  190. this.setState({
  191. openKeys: moreThanOne ? [lastOpenKey] : [...openKeys],
  192. });
  193. };
  194. render() {
  195. const { logo, collapsed, onCollapse } = this.props;
  196. const { openKeys } = this.state;
  197. // Don't show popup menu when it is been collapsed
  198. const menuProps = collapsed
  199. ? {}
  200. : {
  201. openKeys,
  202. };
  203. // if pathname can't match, use the nearest parent's key
  204. let selectedKeys = this.getSelectedMenuKeys();
  205. if (!selectedKeys.length) {
  206. selectedKeys = [openKeys[openKeys.length - 1]];
  207. }
  208. return (
  209. <Sider
  210. trigger={null}
  211. collapsible
  212. collapsed={collapsed}
  213. breakpoint="lg"
  214. onCollapse={onCollapse}
  215. width={256}
  216. className={styles.sider}
  217. >
  218. <div className={styles.logo} key="logo">
  219. <Link to="/">
  220. <img src={logo} alt="logo" />
  221. <h1>Ant Design Pro</h1>
  222. </Link>
  223. </div>
  224. <Menu
  225. key="Menu"
  226. theme="dark"
  227. mode="inline"
  228. {...menuProps}
  229. onOpenChange={this.handleOpenChange}
  230. selectedKeys={selectedKeys}
  231. style={{ padding: '16px 0', width: '100%' }}
  232. >
  233. {this.getNavMenuItems(this.menus)}
  234. </Menu>
  235. </Sider>
  236. );
  237. }
  238. }