index.js 6.51 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
  };
29 30 31 32 33 34 35 36 37 38 39 40

  state = {
    breadcrumb: null,
  };

  componentDidMount() {
    this.getBreadcrumbDom();
  }
  componentWillReceiveProps() {
    this.getBreadcrumbDom();
  }

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

166
  render() {
偏右's avatar
偏右 committed
167
    const {
168 169 170 171 172 173 174 175
      title,
      logo,
      action,
      content,
      extraContent,
      tabList,
      className,
      tabActiveKey,
176
      tabDefaultActiveKey,
177
      tabBarExtraContent,
偏右's avatar
偏右 committed
178
    } = this.props;
179

180
    const clsString = classNames(styles.pageHeader, className);
181 182 183 184
    const activeKeyProps = {};
    if (tabDefaultActiveKey !== undefined) {
      activeKeyProps.defaultActiveKey = tabDefaultActiveKey;
    }
afc163's avatar
afc163 committed
185 186
    if (tabActiveKey !== undefined) {
      activeKeyProps.activeKey = tabActiveKey;
afc163's avatar
afc163 committed
187 188
    }

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