index.js 6.28 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
export default class PageHeader extends PureComponent {
21 22 23 24 25 26 27 28
  state = {
    breadcrumb: null,
  };

  componentDidMount() {
    this.getBreadcrumbDom();
  }

jim's avatar
jim committed
29 30 31 32 33
  componentDidUpdate(preProps) {
    if (preProps.tabActiveKey !== this.props.tabActiveKey) {
      this.getBreadcrumbDom();
    }
  }
jim's avatar
jim committed
34
  onChange = key => {
35 36 37 38 39 40
    if (this.props.onTabChange) {
      this.props.onTabChange(key);
    }
  };
  getBreadcrumbProps = () => {
    return {
jim's avatar
jim committed
41 42 43 44
      routes: this.props.routes,
      params: this.props.params,
      routerLocation: this.props.location,
      breadcrumbNameMap: this.props.breadcrumbNameMap,
45 46
    };
  };
47 48 49 50 51 52
  getBreadcrumbDom = () => {
    const breadcrumb = this.conversionBreadcrumbList();
    this.setState({
      breadcrumb,
    });
  };
陈帅's avatar
陈帅 committed
53
  // Generated according to props
54
  conversionFromProps = () => {
jim's avatar
jim committed
55
    const { breadcrumbList, breadcrumbSeparator, linkElement = 'a' } = this.props;
陈帅's avatar
陈帅 committed
56
    return (
57
      <Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
陈帅's avatar
陈帅 committed
58 59
        {breadcrumbList.map(item => (
          <Breadcrumb.Item key={item.title}>
60 61 62 63 64 65
            {item.href
              ? createElement(
                  linkElement,
                  {
                    [linkElement === 'a' ? 'href' : 'to']: item.href,
                  },
jim's avatar
jim committed
66
                  item.title
67 68
                )
              : item.title}
陈帅's avatar
陈帅 committed
69
          </Breadcrumb.Item>
70
        ))}
陈帅's avatar
陈帅 committed
71 72
      </Breadcrumb>
    );
73
  };
陈帅's avatar
陈帅 committed
74
  conversionFromLocation = (routerLocation, breadcrumbNameMap) => {
75
    const { breadcrumbSeparator, linkElement = 'a' } = this.props;
jim's avatar
jim committed
76 77
    // Convert the url to an array
    const pathSnippets = urlToList(routerLocation.pathname);
陈帅's avatar
陈帅 committed
78
    // Loop data mosaic routing
jim's avatar
jim committed
79
    const extraBreadcrumbItems = pathSnippets.map((url, index) => {
陈帅's avatar
陈帅 committed
80
      const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url);
jim's avatar
jim committed
81
      const isLinkable = index !== pathSnippets.length - 1 && currentBreadcrumb.component;
陈帅's avatar
陈帅 committed
82 83 84 85 86
      return currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? (
        <Breadcrumb.Item key={url}>
          {createElement(
            isLinkable ? linkElement : 'span',
            { [linkElement === 'a' ? 'href' : 'to']: url },
jim's avatar
jim committed
87
            currentBreadcrumb.name
陈帅's avatar
陈帅 committed
88 89 90 91 92 93 94
          )}
        </Breadcrumb.Item>
      ) : null;
    });
    // Add home breadcrumbs to your head
    extraBreadcrumbItems.unshift(
      <Breadcrumb.Item key="home">
95 96 97 98 99
        {createElement(
          linkElement,
          {
            [linkElement === 'a' ? 'href' : 'to']: '/',
          },
jim's avatar
jim committed
100
          '首页'
101
        )}
jim's avatar
jim committed
102
      </Breadcrumb.Item>
陈帅's avatar
陈帅 committed
103 104
    );
    return (
105
      <Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
陈帅's avatar
陈帅 committed
106 107 108
        {extraBreadcrumbItems}
      </Breadcrumb>
    );
109
  };
陈帅's avatar
陈帅 committed
110 111 112 113 114
  /**
   * 将参数转化为面包屑
   * Convert parameters into breadcrumbs
   */
  conversionBreadcrumbList = () => {
115
    const { breadcrumbList, breadcrumbSeparator } = this.props;
jim's avatar
jim committed
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
    return last || !route.component ? (
      <span>{route.breadcrumbName}</span>
    ) : (
      createElement(
        linkElement,
        {
          href: paths.join('/') || '/',
          to: paths.join('/') || '/',
        },
jim's avatar
jim committed
154
        route.breadcrumbName
155 156 157
      )
    );
  };
陈帅's avatar
陈帅 committed
158

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

173
    const clsString = classNames(styles.pageHeader, className);
174 175 176 177
    const activeKeyProps = {};
    if (tabDefaultActiveKey !== undefined) {
      activeKeyProps.defaultActiveKey = tabDefaultActiveKey;
    }
afc163's avatar
afc163 committed
178 179
    if (tabActiveKey !== undefined) {
      activeKeyProps.activeKey = tabActiveKey;
afc163's avatar
afc163 committed
180 181
    }

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