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

159
  render() {
偏右's avatar
偏右 committed
160
    const {
161 162 163 164 165 166 167 168 169
      title,
      logo,
      action,
      content,
      extraContent,
      tabList,
      className,
      tabActiveKey,
      tabBarExtraContent,
偏右's avatar
偏右 committed
170
    } = this.props;
171 172
    const clsString = classNames(styles.pageHeader, className);

afc163's avatar
afc163 committed
173
    let tabDefaultValue;
174
    if (tabActiveKey === undefined && tabList) {
afc163's avatar
afc163 committed
175 176
      tabDefaultValue = tabList.filter(item => item.default)[0] || tabList[0];
    }
陈帅's avatar
陈帅 committed
177
    const breadcrumb = this.conversionBreadcrumbList();
afc163's avatar
afc163 committed
178 179 180
    const activeKeyProps = {
      defaultActiveKey: tabDefaultValue && tabDefaultValue.key,
    };
afc163's avatar
afc163 committed
181 182
    if (tabActiveKey !== undefined) {
      activeKeyProps.activeKey = tabActiveKey;
afc163's avatar
afc163 committed
183 184
    }

185 186 187 188 189 190 191 192 193 194 195 196
    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>}
197 198 199
              {extraContent && (
                <div className={styles.extraContent}>{extraContent}</div>
              )}
200 201 202
            </div>
          </div>
        </div>
203
        {tabList &&
204 205 206
          tabList.length && (
            <Tabs
              className={styles.tabs}
afc163's avatar
afc163 committed
207
              {...activeKeyProps}
208
              onChange={this.onChange}
209
              tabBarExtraContent={tabBarExtraContent}
210
            >
211
              {tabList.map(item => <TabPane tab={item.tab} key={item.key} />)}
212
            </Tabs>
213
          )}
214 215 216 217
      </div>
    );
  }
}