index.js 6.26 KB
Newer Older
偏右's avatar
偏右 committed
1
import React, { PureComponent, createElement } from 'react';
2
import PropTypes from 'prop-types';
陈帅's avatar
陈帅 committed
3
import pathToRegexp from 'path-to-regexp';
4 5 6
import { Breadcrumb, Tabs } from 'antd';
import classNames from 'classnames';
import styles from './index.less';
jim's avatar
jim committed
7
import { urlToList } from '../_utils/pathTools';
陈帅's avatar
陈帅 committed
8

afc163's avatar
afc163 committed
9
const { TabPane } = Tabs;
jim's avatar
jim committed
10
export function getBreadcrumb(breadcrumbNameMap, url) {
11 12
  let breadcrumb = breadcrumbNameMap[url];
  if (!breadcrumb) {
jim's avatar
jim committed
13
    Object.keys(breadcrumbNameMap).forEach(item => {
14 15 16 17 18 19
      if (pathToRegexp(item).test(url)) {
        breadcrumb = breadcrumbNameMap[item];
      }
    });
  }
  return breadcrumb || {};
20 21
}

22 23 24 25
export default class PageHeader extends PureComponent {
  static contextTypes = {
    routes: PropTypes.array,
    params: PropTypes.object,
ddcat1115's avatar
ddcat1115 committed
26 27
    location: PropTypes.object,
    breadcrumbNameMap: PropTypes.object,
28
  };
jim's avatar
jim committed
29
  onChange = key => {
30 31 32 33 34 35 36 37
    if (this.props.onTabChange) {
      this.props.onTabChange(key);
    }
  };
  getBreadcrumbProps = () => {
    return {
      routes: this.props.routes || this.context.routes,
      params: this.props.params || this.context.params,
陈帅's avatar
陈帅 committed
38
      routerLocation: this.props.location || this.context.location,
jim's avatar
jim committed
39
      breadcrumbNameMap: this.props.breadcrumbNameMap || this.context.breadcrumbNameMap,
40 41
    };
  };
陈帅's avatar
陈帅 committed
42
  // Generated according to props
43
  conversionFromProps = () => {
jim's avatar
jim committed
44
    const { breadcrumbList, breadcrumbSeparator, linkElement = 'a' } = this.props;
陈帅's avatar
陈帅 committed
45
    return (
46
      <Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
陈帅's avatar
陈帅 committed
47 48
        {breadcrumbList.map(item => (
          <Breadcrumb.Item key={item.title}>
49 50 51 52 53 54
            {item.href
              ? createElement(
                  linkElement,
                  {
                    [linkElement === 'a' ? 'href' : 'to']: item.href,
                  },
jim's avatar
jim committed
55
                  item.title
56 57
                )
              : item.title}
陈帅's avatar
陈帅 committed
58
          </Breadcrumb.Item>
59
        ))}
陈帅's avatar
陈帅 committed
60 61
      </Breadcrumb>
    );
62
  };
陈帅's avatar
陈帅 committed
63
  conversionFromLocation = (routerLocation, breadcrumbNameMap) => {
64
    const { breadcrumbSeparator, linkElement = 'a' } = this.props;
jim's avatar
jim committed
65 66
    // Convert the url to an array
    const pathSnippets = urlToList(routerLocation.pathname);
陈帅's avatar
陈帅 committed
67
    // Loop data mosaic routing
jim's avatar
jim committed
68
    const extraBreadcrumbItems = pathSnippets.map((url, index) => {
陈帅's avatar
陈帅 committed
69
      const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url);
jim's avatar
jim committed
70
      const isLinkable = index !== pathSnippets.length - 1 && currentBreadcrumb.component;
陈帅's avatar
陈帅 committed
71 72 73 74 75
      return currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? (
        <Breadcrumb.Item key={url}>
          {createElement(
            isLinkable ? linkElement : 'span',
            { [linkElement === 'a' ? 'href' : 'to']: url },
jim's avatar
jim committed
76
            currentBreadcrumb.name
陈帅's avatar
陈帅 committed
77 78 79 80 81 82 83
          )}
        </Breadcrumb.Item>
      ) : null;
    });
    // Add home breadcrumbs to your head
    extraBreadcrumbItems.unshift(
      <Breadcrumb.Item key="home">
84 85 86 87 88
        {createElement(
          linkElement,
          {
            [linkElement === 'a' ? 'href' : 'to']: '/',
          },
jim's avatar
jim committed
89
          '首页'
90
        )}
jim's avatar
jim committed
91
      </Breadcrumb.Item>
陈帅's avatar
陈帅 committed
92 93
    );
    return (
94
      <Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
陈帅's avatar
陈帅 committed
95 96 97
        {extraBreadcrumbItems}
      </Breadcrumb>
    );
98
  };
陈帅's avatar
陈帅 committed
99 100 101 102 103
  /**
   * 将参数转化为面包屑
   * Convert parameters into breadcrumbs
   */
  conversionBreadcrumbList = () => {
104
    const { breadcrumbList, breadcrumbSeparator } = this.props;
jim's avatar
jim committed
105
    const { routes, params, routerLocation, breadcrumbNameMap } = this.getBreadcrumbProps();
陈帅's avatar
陈帅 committed
106 107 108 109 110 111 112 113 114 115 116 117
    if (breadcrumbList && breadcrumbList.length) {
      return this.conversionFromProps();
    }
    // 如果传入 routes 和 params 属性
    // If pass routes and params attributes
    if (routes && params) {
      return (
        <Breadcrumb
          className={styles.breadcrumb}
          routes={routes.filter(route => route.breadcrumbName)}
          params={params}
          itemRender={this.itemRender}
118
          separator={breadcrumbSeparator}
陈帅's avatar
陈帅 committed
119 120 121 122 123
        />
      );
    }
    // 根据 location 生成 面包屑
    // Generate breadcrumbs based on location
Alexius Lee's avatar
Alexius Lee committed
124
    if (routerLocation && routerLocation.pathname) {
陈帅's avatar
陈帅 committed
125 126 127
      return this.conversionFromLocation(routerLocation, breadcrumbNameMap);
    }
    return null;
128
  };
陈帅's avatar
陈帅 committed
129 130
  // 渲染Breadcrumb 子节点
  // Render the Breadcrumb child node
偏右's avatar
偏右 committed
131 132 133
  itemRender = (route, params, routes, paths) => {
    const { linkElement = 'a' } = this.props;
    const last = routes.indexOf(route) === routes.length - 1;
134 135 136 137 138 139 140 141 142
    return last || !route.component ? (
      <span>{route.breadcrumbName}</span>
    ) : (
      createElement(
        linkElement,
        {
          href: paths.join('/') || '/',
          to: paths.join('/') || '/',
        },
jim's avatar
jim committed
143
        route.breadcrumbName
144 145 146
      )
    );
  };
陈帅's avatar
陈帅 committed
147

148
  render() {
偏右's avatar
偏右 committed
149
    const {
150 151 152 153 154 155 156 157
      title,
      logo,
      action,
      content,
      extraContent,
      tabList,
      className,
      tabActiveKey,
158
      tabDefaultActiveKey,
159
      tabBarExtraContent,
偏右's avatar
偏右 committed
160
    } = this.props;
161
    const clsString = classNames(styles.pageHeader, className);
陈帅's avatar
陈帅 committed
162
    const breadcrumb = this.conversionBreadcrumbList();
163 164 165 166
    const activeKeyProps = {};
    if (tabDefaultActiveKey !== undefined) {
      activeKeyProps.defaultActiveKey = tabDefaultActiveKey;
    }
afc163's avatar
afc163 committed
167 168
    if (tabActiveKey !== undefined) {
      activeKeyProps.activeKey = tabActiveKey;
afc163's avatar
afc163 committed
169 170
    }

171 172 173 174 175 176 177 178 179 180 181 182
    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>}
jim's avatar
jim committed
183
              {extraContent && <div className={styles.extraContent}>{extraContent}</div>}
184 185 186
            </div>
          </div>
        </div>
187
        {tabList &&
188 189 190
          tabList.length && (
            <Tabs
              className={styles.tabs}
afc163's avatar
afc163 committed
191
              {...activeKeyProps}
192
              onChange={this.onChange}
193
              tabBarExtraContent={tabBarExtraContent}
194
            >
195
              {tabList.map(item => <TabPane tab={item.tab} key={item.key} />)}
196
            </Tabs>
197
          )}
198 199 200 201
      </div>
    );
  }
}