动态菜单和动态路由的 antd pro

index.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import React, { PureComponent, createElement } from 'react';
  2. import PropTypes from 'prop-types';
  3. import pathToRegexp from 'path-to-regexp';
  4. import { Breadcrumb, Tabs } from 'antd';
  5. import classNames from 'classnames';
  6. import styles from './index.less';
  7. import { urlToList } from '../_utils/pathTools';
  8. const { TabPane } = Tabs;
  9. export function getBreadcrumb(breadcrumbNameMap, url) {
  10. let breadcrumb = breadcrumbNameMap[url];
  11. if (!breadcrumb) {
  12. Object.keys(breadcrumbNameMap).forEach(item => {
  13. if (pathToRegexp(item).test(url)) {
  14. breadcrumb = breadcrumbNameMap[item];
  15. }
  16. });
  17. }
  18. return breadcrumb || {};
  19. }
  20. export default class PageHeader extends PureComponent {
  21. static contextTypes = {
  22. routes: PropTypes.array,
  23. params: PropTypes.object,
  24. location: PropTypes.object,
  25. breadcrumbNameMap: PropTypes.object,
  26. };
  27. state = {
  28. breadcrumb: null,
  29. };
  30. componentDidMount() {
  31. this.getBreadcrumbDom();
  32. }
  33. componentWillReceiveProps() {
  34. this.getBreadcrumbDom();
  35. }
  36. onChange = key => {
  37. if (this.props.onTabChange) {
  38. this.props.onTabChange(key);
  39. }
  40. };
  41. getBreadcrumbProps = () => {
  42. return {
  43. routes: this.props.routes || this.context.routes,
  44. params: this.props.params || this.context.params,
  45. routerLocation: this.props.location || this.context.location,
  46. breadcrumbNameMap: this.props.breadcrumbNameMap || this.context.breadcrumbNameMap,
  47. };
  48. };
  49. getBreadcrumbDom = () => {
  50. const breadcrumb = this.conversionBreadcrumbList();
  51. this.setState({
  52. breadcrumb,
  53. });
  54. };
  55. // Generated according to props
  56. conversionFromProps = () => {
  57. const { breadcrumbList, breadcrumbSeparator, linkElement = 'a' } = this.props;
  58. return (
  59. <Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
  60. {breadcrumbList.map(item => (
  61. <Breadcrumb.Item key={item.title}>
  62. {item.href
  63. ? createElement(
  64. linkElement,
  65. {
  66. [linkElement === 'a' ? 'href' : 'to']: item.href,
  67. },
  68. item.title
  69. )
  70. : item.title}
  71. </Breadcrumb.Item>
  72. ))}
  73. </Breadcrumb>
  74. );
  75. };
  76. conversionFromLocation = (routerLocation, breadcrumbNameMap) => {
  77. const { breadcrumbSeparator, linkElement = 'a' } = this.props;
  78. // Convert the url to an array
  79. const pathSnippets = urlToList(routerLocation.pathname);
  80. // Loop data mosaic routing
  81. const extraBreadcrumbItems = pathSnippets.map((url, index) => {
  82. const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url);
  83. const isLinkable = index !== pathSnippets.length - 1 && currentBreadcrumb.component;
  84. return currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? (
  85. <Breadcrumb.Item key={url}>
  86. {createElement(
  87. isLinkable ? linkElement : 'span',
  88. { [linkElement === 'a' ? 'href' : 'to']: url },
  89. currentBreadcrumb.name
  90. )}
  91. </Breadcrumb.Item>
  92. ) : null;
  93. });
  94. // Add home breadcrumbs to your head
  95. extraBreadcrumbItems.unshift(
  96. <Breadcrumb.Item key="home">
  97. {createElement(
  98. linkElement,
  99. {
  100. [linkElement === 'a' ? 'href' : 'to']: '/',
  101. },
  102. '首页'
  103. )}
  104. </Breadcrumb.Item>
  105. );
  106. return (
  107. <Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
  108. {extraBreadcrumbItems}
  109. </Breadcrumb>
  110. );
  111. };
  112. /**
  113. * 将参数转化为面包屑
  114. * Convert parameters into breadcrumbs
  115. */
  116. conversionBreadcrumbList = () => {
  117. const { breadcrumbList, breadcrumbSeparator } = this.props;
  118. const { routes, params, routerLocation, breadcrumbNameMap } = this.getBreadcrumbProps();
  119. if (breadcrumbList && breadcrumbList.length) {
  120. return this.conversionFromProps();
  121. }
  122. // 如果传入 routes 和 params 属性
  123. // If pass routes and params attributes
  124. if (routes && params) {
  125. return (
  126. <Breadcrumb
  127. className={styles.breadcrumb}
  128. routes={routes.filter(route => route.breadcrumbName)}
  129. params={params}
  130. itemRender={this.itemRender}
  131. separator={breadcrumbSeparator}
  132. />
  133. );
  134. }
  135. // 根据 location 生成 面包屑
  136. // Generate breadcrumbs based on location
  137. if (routerLocation && routerLocation.pathname) {
  138. return this.conversionFromLocation(routerLocation, breadcrumbNameMap);
  139. }
  140. return null;
  141. };
  142. // 渲染Breadcrumb 子节点
  143. // Render the Breadcrumb child node
  144. itemRender = (route, params, routes, paths) => {
  145. const { linkElement = 'a' } = this.props;
  146. const last = routes.indexOf(route) === routes.length - 1;
  147. return last || !route.component ? (
  148. <span>{route.breadcrumbName}</span>
  149. ) : (
  150. createElement(
  151. linkElement,
  152. {
  153. href: paths.join('/') || '/',
  154. to: paths.join('/') || '/',
  155. },
  156. route.breadcrumbName
  157. )
  158. );
  159. };
  160. render() {
  161. const {
  162. title,
  163. logo,
  164. action,
  165. content,
  166. extraContent,
  167. tabList,
  168. className,
  169. tabActiveKey,
  170. tabDefaultActiveKey,
  171. tabBarExtraContent,
  172. } = this.props;
  173. const clsString = classNames(styles.pageHeader, className);
  174. const activeKeyProps = {};
  175. if (tabDefaultActiveKey !== undefined) {
  176. activeKeyProps.defaultActiveKey = tabDefaultActiveKey;
  177. }
  178. if (tabActiveKey !== undefined) {
  179. activeKeyProps.activeKey = tabActiveKey;
  180. }
  181. return (
  182. <div className={clsString}>
  183. {this.state.breadcrumb}
  184. <div className={styles.detail}>
  185. {logo && <div className={styles.logo}>{logo}</div>}
  186. <div className={styles.main}>
  187. <div className={styles.row}>
  188. {title && <h1 className={styles.title}>{title}</h1>}
  189. {action && <div className={styles.action}>{action}</div>}
  190. </div>
  191. <div className={styles.row}>
  192. {content && <div className={styles.content}>{content}</div>}
  193. {extraContent && <div className={styles.extraContent}>{extraContent}</div>}
  194. </div>
  195. </div>
  196. </div>
  197. {tabList &&
  198. tabList.length && (
  199. <Tabs
  200. className={styles.tabs}
  201. {...activeKeyProps}
  202. onChange={this.onChange}
  203. tabBarExtraContent={tabBarExtraContent}
  204. >
  205. {tabList.map(item => <TabPane tab={item.tab} key={item.key} />)}
  206. </Tabs>
  207. )}
  208. </div>
  209. );
  210. }
  211. }