index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import React, { PureComponent, createElement } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Breadcrumb, Tabs } from 'antd';
  4. import classNames from 'classnames';
  5. import styles from './index.less';
  6. const { TabPane } = Tabs;
  7. function getBreadcrumb(breadcrumbNameMap, url) {
  8. if (breadcrumbNameMap[url]) {
  9. return breadcrumbNameMap[url];
  10. }
  11. const urlWithoutSplash = url.replace(/\/$/, '');
  12. if (breadcrumbNameMap[urlWithoutSplash]) {
  13. return breadcrumbNameMap[urlWithoutSplash];
  14. }
  15. let breadcrumb = {};
  16. Object.keys(breadcrumbNameMap).forEach((item) => {
  17. const itemRegExpStr = `^${item.replace(/:[\w-]+/g, '[\\w-]+')}$`;
  18. const itemRegExp = new RegExp(itemRegExpStr);
  19. if (itemRegExp.test(url)) {
  20. breadcrumb = breadcrumbNameMap[item];
  21. }
  22. });
  23. return breadcrumb;
  24. }
  25. export default class PageHeader extends PureComponent {
  26. static contextTypes = {
  27. routes: PropTypes.array,
  28. params: PropTypes.object,
  29. location: PropTypes.object,
  30. breadcrumbNameMap: PropTypes.object,
  31. };
  32. onChange = (key) => {
  33. if (this.props.onTabChange) {
  34. this.props.onTabChange(key);
  35. }
  36. };
  37. getBreadcrumbProps = () => {
  38. return {
  39. routes: this.props.routes || this.context.routes,
  40. params: this.props.params || this.context.params,
  41. location: this.props.location || this.context.location,
  42. breadcrumbNameMap: this.props.breadcrumbNameMap || this.context.breadcrumbNameMap,
  43. };
  44. };
  45. itemRender = (route, params, routes, paths) => {
  46. const { linkElement = 'a' } = this.props;
  47. const last = routes.indexOf(route) === routes.length - 1;
  48. return (last || !route.component)
  49. ? <span>{route.breadcrumbName}</span>
  50. : createElement(linkElement, {
  51. href: paths.join('/') || '/',
  52. to: paths.join('/') || '/',
  53. }, route.breadcrumbName);
  54. }
  55. render() {
  56. const { routes, params, location, breadcrumbNameMap } = this.getBreadcrumbProps();
  57. const {
  58. title, logo, action, content, extraContent,
  59. breadcrumbList, tabList, className, linkElement = 'a',
  60. activeTabKey,
  61. } = this.props;
  62. const clsString = classNames(styles.pageHeader, className);
  63. let breadcrumb;
  64. if (routes && params) {
  65. breadcrumb = (
  66. <Breadcrumb
  67. className={styles.breadcrumb}
  68. routes={routes.filter(route => route.breadcrumbName)}
  69. params={params}
  70. itemRender={this.itemRender}
  71. />
  72. );
  73. } else if (location && location.pathname && (!breadcrumbList)) {
  74. const pathSnippets = location.pathname.split('/').filter(i => i);
  75. const extraBreadcrumbItems = pathSnippets.map((_, index) => {
  76. const url = `/${pathSnippets.slice(0, index + 1).join('/')}`;
  77. const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url);
  78. const isLinkable = (index !== pathSnippets.length - 1) && currentBreadcrumb.component;
  79. return currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? (
  80. <Breadcrumb.Item key={url}>
  81. {createElement(
  82. isLinkable ? linkElement : 'span',
  83. { [linkElement === 'a' ? 'href' : 'to']: url },
  84. currentBreadcrumb.name,
  85. )}
  86. </Breadcrumb.Item>
  87. ) : null;
  88. });
  89. const breadcrumbItems = [(
  90. <Breadcrumb.Item key="home">
  91. {createElement(linkElement, {
  92. [linkElement === 'a' ? 'href' : 'to']: '/',
  93. }, '首页')}
  94. </Breadcrumb.Item>
  95. )].concat(extraBreadcrumbItems);
  96. breadcrumb = (
  97. <Breadcrumb className={styles.breadcrumb}>
  98. {breadcrumbItems}
  99. </Breadcrumb>
  100. );
  101. } else if (breadcrumbList && breadcrumbList.length) {
  102. breadcrumb = (
  103. <Breadcrumb className={styles.breadcrumb}>
  104. {
  105. breadcrumbList.map(item => (
  106. <Breadcrumb.Item key={item.title}>
  107. {item.href ? (
  108. createElement(linkElement, {
  109. [linkElement === 'a' ? 'href' : 'to']: item.href,
  110. }, item.title)
  111. ) : item.title}
  112. </Breadcrumb.Item>)
  113. )
  114. }
  115. </Breadcrumb>
  116. );
  117. } else {
  118. breadcrumb = null;
  119. }
  120. let tabDefaultValue;
  121. if (activeTabKey !== undefined && tabList) {
  122. tabDefaultValue = tabList.filter(item => item.default)[0] || tabList[0];
  123. }
  124. return (
  125. <div className={clsString}>
  126. {breadcrumb}
  127. <div className={styles.detail}>
  128. {logo && <div className={styles.logo}>{logo}</div>}
  129. <div className={styles.main}>
  130. <div className={styles.row}>
  131. {title && <h1 className={styles.title}>{title}</h1>}
  132. {action && <div className={styles.action}>{action}</div>}
  133. </div>
  134. <div className={styles.row}>
  135. {content && <div className={styles.content}>{content}</div>}
  136. {extraContent && <div className={styles.extraContent}>{extraContent}</div>}
  137. </div>
  138. </div>
  139. </div>
  140. {
  141. tabList &&
  142. tabList.length && (
  143. <Tabs
  144. className={styles.tabs}
  145. defaultActiveKey={(tabDefaultValue && tabDefaultValue.key)}
  146. activeKey={activeTabKey}
  147. onChange={this.onChange}
  148. >
  149. {
  150. tabList.map(item => <TabPane tab={item.tab} key={item.key} />)
  151. }
  152. </Tabs>
  153. )
  154. }
  155. </div>
  156. );
  157. }
  158. }