index.js 6.21 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 29 30 31
  state = {
    breadcrumb: null,
  };

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

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

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

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

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