index.js 5.86 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 7
import { Breadcrumb, Tabs } from 'antd';
import classNames from 'classnames';
import styles from './index.less';

陈帅's avatar
陈帅 committed
8

afc163's avatar
afc163 committed
9
const { TabPane } = Tabs;
10

afc163's avatar
afc163 committed
11
function getBreadcrumb(breadcrumbNameMap, url) {
ddcat1115's avatar
ddcat1115 committed
12
  let breadcrumb = {};
13
  Object.keys(breadcrumbNameMap).forEach((item) => {
陈帅's avatar
陈帅 committed
14
    if (pathToRegexp(item).test(url)) {
afc163's avatar
afc163 committed
15
      breadcrumb = breadcrumbNameMap[item];
16 17
    }
  });
afc163's avatar
afc163 committed
18
  return breadcrumb;
19 20
}

21 22 23 24
export default class PageHeader extends PureComponent {
  static contextTypes = {
    routes: PropTypes.array,
    params: PropTypes.object,
ddcat1115's avatar
ddcat1115 committed
25 26
    location: PropTypes.object,
    breadcrumbNameMap: PropTypes.object,
27 28 29 30 31 32 33 34 35 36
  };
  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
37
      routerLocation: this.props.location || this.context.location,
ddcat1115's avatar
ddcat1115 committed
38
      breadcrumbNameMap: this.props.breadcrumbNameMap || this.context.breadcrumbNameMap,
39 40
    };
  };
陈帅's avatar
陈帅 committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  // Generated according to props
  conversionFromProps= () => {
    const {
      breadcrumbList, linkElement = 'a',
    } = this.props;
    return (
      <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>
    );
  }
  conversionFromLocation = (routerLocation, breadcrumbNameMap) => {
    const { linkElement = 'a' } = this.props;
    // Convert the path to an array
    const pathSnippets = routerLocation.pathname.split('/').filter(i => i);
    // Loop data mosaic routing
    const extraBreadcrumbItems = pathSnippets.map((_, index) => {
      const url = `/${pathSnippets.slice(0, index + 1).join('/')}`;
      const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url);
      const isLinkable = (index !== pathSnippets.length - 1) && currentBreadcrumb.component;
      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">
        {createElement(linkElement, {
        [linkElement === 'a' ? 'href' : 'to']: '/' }, '首页')}
      </Breadcrumb.Item>
    );
    return (
      <Breadcrumb className={styles.breadcrumb}>
        {extraBreadcrumbItems}
      </Breadcrumb>
    );
  }
  /**
   * 将参数转化为面包屑
   * Convert parameters into breadcrumbs
   */
  conversionBreadcrumbList = () => {
    const { breadcrumbList } = this.props;
    const { routes, params, routerLocation, breadcrumbNameMap } = this.getBreadcrumbProps();
    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}
        />
      );
    }
    // 根据 location 生成 面包屑
    // Generate breadcrumbs based on location
    if (location && location.pathname) {
      return this.conversionFromLocation(routerLocation, breadcrumbNameMap);
    }
    return null;
  }
  // 渲染Breadcrumb 子节点
  // Render the Breadcrumb child node
偏右's avatar
偏右 committed
121 122 123 124 125 126 127 128 129 130
  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);
  }
陈帅's avatar
陈帅 committed
131

132
  render() {
偏右's avatar
偏右 committed
133 134
    const {
      title, logo, action, content, extraContent,
陈帅's avatar
陈帅 committed
135
      tabList, className, tabActiveKey,
偏右's avatar
偏右 committed
136
    } = this.props;
137 138
    const clsString = classNames(styles.pageHeader, className);

afc163's avatar
afc163 committed
139
    let tabDefaultValue;
afc163's avatar
afc163 committed
140
    if (tabActiveKey !== undefined && tabList) {
afc163's avatar
afc163 committed
141 142
      tabDefaultValue = tabList.filter(item => item.default)[0] || tabList[0];
    }
陈帅's avatar
陈帅 committed
143
    const breadcrumb = this.conversionBreadcrumbList();
afc163's avatar
afc163 committed
144 145 146
    const activeKeyProps = {
      defaultActiveKey: tabDefaultValue && tabDefaultValue.key,
    };
afc163's avatar
afc163 committed
147 148
    if (tabActiveKey !== undefined) {
      activeKeyProps.activeKey = tabActiveKey;
afc163's avatar
afc163 committed
149 150
    }

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    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 &&
169 170 171
          tabList.length && (
            <Tabs
              className={styles.tabs}
afc163's avatar
afc163 committed
172
              {...activeKeyProps}
173 174 175 176 177 178 179
              onChange={this.onChange}
            >
              {
                tabList.map(item => <TabPane tab={item.tab} key={item.key} />)
              }
            </Tabs>
          )
180 181 182 183 184
        }
      </div>
    );
  }
}