index.js 5.48 KB
Newer Older
偏右's avatar
偏右 committed
1
import React, { PureComponent, createElement } from 'react';
2 3 4 5 6
import PropTypes from 'prop-types';
import { Breadcrumb, Tabs } from 'antd';
import classNames from 'classnames';
import styles from './index.less';

afc163's avatar
afc163 committed
7
const { TabPane } = Tabs;
8

afc163's avatar
afc163 committed
9 10 11
function getBreadcrumb(breadcrumbNameMap, url) {
  if (breadcrumbNameMap[url]) {
    return breadcrumbNameMap[url];
afc163's avatar
afc163 committed
12 13
  }
  const urlWithoutSplash = url.replace(/\/$/, '');
afc163's avatar
afc163 committed
14 15
  if (breadcrumbNameMap[urlWithoutSplash]) {
    return breadcrumbNameMap[urlWithoutSplash];
afc163's avatar
afc163 committed
16
  }
ddcat1115's avatar
ddcat1115 committed
17
  let breadcrumb = {};
18 19 20 21
  Object.keys(breadcrumbNameMap).forEach((item) => {
    const itemRegExpStr = `^${item.replace(/:[\w-]+/g, '[\\w-]+')}$`;
    const itemRegExp = new RegExp(itemRegExpStr);
    if (itemRegExp.test(url)) {
afc163's avatar
afc163 committed
22
      breadcrumb = breadcrumbNameMap[item];
23 24
    }
  });
afc163's avatar
afc163 committed
25
  return breadcrumb;
26 27
}

28 29 30 31
export default class PageHeader extends PureComponent {
  static contextTypes = {
    routes: PropTypes.array,
    params: PropTypes.object,
ddcat1115's avatar
ddcat1115 committed
32 33
    location: PropTypes.object,
    breadcrumbNameMap: PropTypes.object,
34 35 36 37 38 39 40 41 42 43
  };
  onChange = (key) => {
    if (this.props.onTabChange) {
      this.props.onTabChange(key);
    }
  };
  getBreadcrumbProps = () => {
    return {
      routes: this.props.routes || this.context.routes,
      params: this.props.params || this.context.params,
ddcat1115's avatar
ddcat1115 committed
44 45
      location: this.props.location || this.context.location,
      breadcrumbNameMap: this.props.breadcrumbNameMap || this.context.breadcrumbNameMap,
46 47
    };
  };
偏右's avatar
偏右 committed
48 49 50 51 52 53 54 55 56 57
  itemRender = (route, params, routes, paths) => {
    const { linkElement = 'a' } = this.props;
    const last = routes.indexOf(route) === routes.length - 1;
    return (last || !route.component)
      ? <span>{route.breadcrumbName}</span>
      : createElement(linkElement, {
        href: paths.join('/') || '/',
        to: paths.join('/') || '/',
      }, route.breadcrumbName);
  }
58
  render() {
ddcat1115's avatar
ddcat1115 committed
59
    const { routes, params, location, breadcrumbNameMap } = this.getBreadcrumbProps();
偏右's avatar
偏右 committed
60 61 62
    const {
      title, logo, action, content, extraContent,
      breadcrumbList, tabList, className, linkElement = 'a',
afc163's avatar
afc163 committed
63
      activeTabKey,
偏右's avatar
偏右 committed
64
    } = this.props;
65 66
    const clsString = classNames(styles.pageHeader, className);
    let breadcrumb;
ddcat1115's avatar
ddcat1115 committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    if (breadcrumbList && breadcrumbList.length) {
      breadcrumb = (
        <Breadcrumb className={styles.breadcrumb}>
          {
            breadcrumbList.map(item => (
              <Breadcrumb.Item key={item.title}>
                {item.href ? (
                  createElement(linkElement, {
                    [linkElement === 'a' ? 'href' : 'to']: item.href,
                  }, item.title)
                ) : item.title}
              </Breadcrumb.Item>)
            )
          }
        </Breadcrumb>
      );
    } else if (routes && params) {
84 85 86 87 88
      breadcrumb = (
        <Breadcrumb
          className={styles.breadcrumb}
          routes={routes.filter(route => route.breadcrumbName)}
          params={params}
偏右's avatar
偏右 committed
89
          itemRender={this.itemRender}
90 91
        />
      );
ddcat1115's avatar
ddcat1115 committed
92
    } else if (location && location.pathname) {
ddcat1115's avatar
ddcat1115 committed
93 94 95
      const pathSnippets = location.pathname.split('/').filter(i => i);
      const extraBreadcrumbItems = pathSnippets.map((_, index) => {
        const url = `/${pathSnippets.slice(0, index + 1).join('/')}`;
afc163's avatar
afc163 committed
96 97
        const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url);
        const isLinkable = (index !== pathSnippets.length - 1) && currentBreadcrumb.component;
ddcat1115's avatar
ddcat1115 committed
98
        return currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? (
ddcat1115's avatar
ddcat1115 committed
99
          <Breadcrumb.Item key={url}>
100
            {createElement(
afc163's avatar
afc163 committed
101
              isLinkable ? linkElement : 'span',
102
              { [linkElement === 'a' ? 'href' : 'to']: url },
ddcat1115's avatar
ddcat1115 committed
103
              currentBreadcrumb.name,
104
            )}
ddcat1115's avatar
ddcat1115 committed
105
          </Breadcrumb.Item>
ddcat1115's avatar
ddcat1115 committed
106
        ) : null;
ddcat1115's avatar
ddcat1115 committed
107 108 109
      });
      const breadcrumbItems = [(
        <Breadcrumb.Item key="home">
偏右's avatar
偏右 committed
110
          {createElement(linkElement, {
afc163's avatar
afc163 committed
111
            [linkElement === 'a' ? 'href' : 'to']: '/',
偏右's avatar
偏右 committed
112
          }, '首页')}
ddcat1115's avatar
ddcat1115 committed
113 114 115 116 117 118 119
        </Breadcrumb.Item>
      )].concat(extraBreadcrumbItems);
      breadcrumb = (
        <Breadcrumb className={styles.breadcrumb}>
          {breadcrumbItems}
        </Breadcrumb>
      );
120 121 122 123
    } else {
      breadcrumb = null;
    }

afc163's avatar
afc163 committed
124 125 126 127
    let tabDefaultValue;
    if (activeTabKey !== undefined && tabList) {
      tabDefaultValue = tabList.filter(item => item.default)[0] || tabList[0];
    }
128

afc163's avatar
afc163 committed
129 130 131 132 133 134 135
    const activeKeyProps = {
      defaultActiveKey: tabDefaultValue && tabDefaultValue.key,
    };
    if (activeTabKey !== undefined) {
      activeKeyProps.activeKey = activeTabKey;
    }

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    return (
      <div className={clsString}>
        {breadcrumb}
        <div className={styles.detail}>
          {logo && <div className={styles.logo}>{logo}</div>}
          <div className={styles.main}>
            <div className={styles.row}>
              {title && <h1 className={styles.title}>{title}</h1>}
              {action && <div className={styles.action}>{action}</div>}
            </div>
            <div className={styles.row}>
              {content && <div className={styles.content}>{content}</div>}
              {extraContent && <div className={styles.extraContent}>{extraContent}</div>}
            </div>
          </div>
        </div>
        {
          tabList &&
154 155 156
          tabList.length && (
            <Tabs
              className={styles.tabs}
afc163's avatar
afc163 committed
157
              {...activeKeyProps}
158 159 160 161 162 163 164
              onChange={this.onChange}
            >
              {
                tabList.map(item => <TabPane tab={item.tab} key={item.key} />)
              }
            </Tabs>
          )
165 166 167 168 169
        }
      </div>
    );
  }
}