index.js 6.57 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

  state = {
    breadcrumb: null,
  };

  componentDidMount() {
    this.getBreadcrumbDom();
  }

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

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

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

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