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

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

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

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

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