index.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import React, { PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Breadcrumb, Tabs } from 'antd';
  4. import { Link } from 'dva/router';
  5. import classNames from 'classnames';
  6. import styles from './index.less';
  7. const { TabPane } = Tabs;
  8. function itemRender(route, params, routes, paths) {
  9. const last = routes.indexOf(route) === routes.length - 1;
  10. return (last || !route.component)
  11. ? <span>{route.breadcrumbName}</span>
  12. : <Link to={paths.join('/') || '/'}>{route.breadcrumbName}</Link>;
  13. }
  14. export default class PageHeader extends PureComponent {
  15. static contextTypes = {
  16. routes: PropTypes.array,
  17. params: PropTypes.object,
  18. };
  19. onChange = (key) => {
  20. if (this.props.onTabChange) {
  21. this.props.onTabChange(key);
  22. }
  23. };
  24. getBreadcrumbProps = () => {
  25. return {
  26. routes: this.props.routes || this.context.routes,
  27. params: this.props.params || this.context.params,
  28. };
  29. };
  30. render() {
  31. const { routes, params } = this.getBreadcrumbProps();
  32. const { title, logo, action, content, extraContent,
  33. breadcrumbList, tabList, className } = this.props;
  34. const clsString = classNames(styles.pageHeader, className);
  35. let breadcrumb;
  36. if (routes && params) {
  37. breadcrumb = (
  38. <Breadcrumb
  39. className={styles.breadcrumb}
  40. routes={routes.filter(route => route.breadcrumbName)}
  41. params={params}
  42. itemRender={itemRender}
  43. />
  44. );
  45. } else if (breadcrumbList && breadcrumbList.length) {
  46. breadcrumb = (
  47. <Breadcrumb className={styles.breadcrumb}>
  48. {
  49. breadcrumbList.map(item => (
  50. <Breadcrumb.Item key={item.title}>
  51. {item.href ? <a href="">{item.title}</a> : item.title}
  52. </Breadcrumb.Item>)
  53. )
  54. }
  55. </Breadcrumb>
  56. );
  57. } else {
  58. breadcrumb = null;
  59. }
  60. const tabDefaultValue = tabList && tabList.filter(item => item.default)[0];
  61. return (
  62. <div className={clsString}>
  63. {breadcrumb}
  64. <div className={styles.detail}>
  65. {logo && <div className={styles.logo}>{logo}</div>}
  66. <div className={styles.main}>
  67. <div className={styles.row}>
  68. {title && <h1 className={styles.title}>{title}</h1>}
  69. {action && <div className={styles.action}>{action}</div>}
  70. </div>
  71. <div className={styles.row}>
  72. {content && <div className={styles.content}>{content}</div>}
  73. {extraContent && <div className={styles.extraContent}>{extraContent}</div>}
  74. </div>
  75. </div>
  76. </div>
  77. {
  78. tabList &&
  79. tabList.length &&
  80. <Tabs
  81. className={styles.tabs}
  82. defaultActiveKey={(tabDefaultValue && tabDefaultValue.key)}
  83. onChange={this.onChange}
  84. >
  85. {
  86. tabList.map(item => <TabPane tab={item.tab} key={item.key} />)
  87. }
  88. </Tabs>
  89. }
  90. </div>
  91. );
  92. }
  93. }