index.js 6.76 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
  componentDidUpdate(preProps) {
39 40
    const { tabActiveKey } = this.props;
    if (preProps.tabActiveKey !== tabActiveKey) {
jim's avatar
jim committed
41 42 43
      this.getBreadcrumbDom();
    }
  }
陈帅's avatar
陈帅 committed
44

jim's avatar
jim committed
45
  onChange = key => {
46 47 48
    const { onTabChange } = this.props;
    if (onTabChange) {
      onTabChange(key);
49 50
    }
  };
陈帅's avatar
陈帅 committed
51

52
  getBreadcrumbProps = () => {
53 54 55 56 57 58 59
    const { routes, params, location, breadcrumbNameMap } = this.props;
    const {
      routes: croutes,
      params: cparams,
      location: clocation,
      breadcrumbNameMap: cbreadcrumbNameMap,
    } = this.context;
60
    return {
61 62 63 64
      routes: routes || croutes,
      params: params || cparams,
      routerLocation: location || clocation,
      breadcrumbNameMap: breadcrumbNameMap || cbreadcrumbNameMap,
65 66
    };
  };
陈帅's avatar
陈帅 committed
67

68 69 70 71 72 73
  getBreadcrumbDom = () => {
    const breadcrumb = this.conversionBreadcrumbList();
    this.setState({
      breadcrumb,
    });
  };
陈帅's avatar
陈帅 committed
74

陈帅's avatar
陈帅 committed
75
  // Generated according to props
76
  conversionFromProps = () => {
jim's avatar
jim committed
77
    const { breadcrumbList, breadcrumbSeparator, linkElement = 'a' } = this.props;
陈帅's avatar
陈帅 committed
78
    return (
79
      <Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
陈帅's avatar
陈帅 committed
80 81
        {breadcrumbList.map(item => (
          <Breadcrumb.Item key={item.title}>
82 83 84 85 86 87
            {item.href
              ? createElement(
                  linkElement,
                  {
                    [linkElement === 'a' ? 'href' : 'to']: item.href,
                  },
jim's avatar
jim committed
88
                  item.title
89 90
                )
              : item.title}
陈帅's avatar
陈帅 committed
91
          </Breadcrumb.Item>
92
        ))}
陈帅's avatar
陈帅 committed
93 94
      </Breadcrumb>
    );
95
  };
陈帅's avatar
陈帅 committed
96

陈帅's avatar
陈帅 committed
97
  conversionFromLocation = (routerLocation, breadcrumbNameMap) => {
98
    const { breadcrumbSeparator, linkElement = 'a' } = this.props;
jim's avatar
jim committed
99 100
    // Convert the url to an array
    const pathSnippets = urlToList(routerLocation.pathname);
陈帅's avatar
陈帅 committed
101
    // Loop data mosaic routing
jim's avatar
jim committed
102
    const extraBreadcrumbItems = pathSnippets.map((url, index) => {
陈帅's avatar
陈帅 committed
103
      const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url);
jim's avatar
jim committed
104
      const isLinkable = index !== pathSnippets.length - 1 && currentBreadcrumb.component;
陈帅's avatar
陈帅 committed
105 106 107 108 109
      return currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? (
        <Breadcrumb.Item key={url}>
          {createElement(
            isLinkable ? linkElement : 'span',
            { [linkElement === 'a' ? 'href' : 'to']: url },
jim's avatar
jim committed
110
            currentBreadcrumb.name
陈帅's avatar
陈帅 committed
111 112 113 114 115 116 117
          )}
        </Breadcrumb.Item>
      ) : null;
    });
    // Add home breadcrumbs to your head
    extraBreadcrumbItems.unshift(
      <Breadcrumb.Item key="home">
118 119 120 121 122
        {createElement(
          linkElement,
          {
            [linkElement === 'a' ? 'href' : 'to']: '/',
          },
jim's avatar
jim committed
123
          '首页'
124
        )}
jim's avatar
jim committed
125
      </Breadcrumb.Item>
陈帅's avatar
陈帅 committed
126 127
    );
    return (
128
      <Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
陈帅's avatar
陈帅 committed
129 130 131
        {extraBreadcrumbItems}
      </Breadcrumb>
    );
132
  };
陈帅's avatar
陈帅 committed
133

陈帅's avatar
陈帅 committed
134 135 136 137 138
  /**
   * 将参数转化为面包屑
   * Convert parameters into breadcrumbs
   */
  conversionBreadcrumbList = () => {
139
    const { breadcrumbList, breadcrumbSeparator } = this.props;
jim's avatar
jim committed
140
    const { routes, params, routerLocation, breadcrumbNameMap } = this.getBreadcrumbProps();
陈帅's avatar
陈帅 committed
141 142 143 144 145 146 147 148 149 150 151 152
    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}
153
          separator={breadcrumbSeparator}
陈帅's avatar
陈帅 committed
154 155 156 157 158
        />
      );
    }
    // 根据 location 生成 面包屑
    // Generate breadcrumbs based on location
Alexius Lee's avatar
Alexius Lee committed
159
    if (routerLocation && routerLocation.pathname) {
陈帅's avatar
陈帅 committed
160 161 162
      return this.conversionFromLocation(routerLocation, breadcrumbNameMap);
    }
    return null;
163
  };
陈帅's avatar
陈帅 committed
164

陈帅's avatar
陈帅 committed
165 166
  // 渲染Breadcrumb 子节点
  // Render the Breadcrumb child node
偏右's avatar
偏右 committed
167 168 169
  itemRender = (route, params, routes, paths) => {
    const { linkElement = 'a' } = this.props;
    const last = routes.indexOf(route) === routes.length - 1;
170 171 172 173 174 175 176 177 178
    return last || !route.component ? (
      <span>{route.breadcrumbName}</span>
    ) : (
      createElement(
        linkElement,
        {
          href: paths.join('/') || '/',
          to: paths.join('/') || '/',
        },
jim's avatar
jim committed
179
        route.breadcrumbName
180 181 182
      )
    );
  };
陈帅's avatar
陈帅 committed
183

184
  render() {
偏右's avatar
偏右 committed
185
    const {
186 187 188 189 190 191 192 193
      title,
      logo,
      action,
      content,
      extraContent,
      tabList,
      className,
      tabActiveKey,
194
      tabDefaultActiveKey,
195
      tabBarExtraContent,
偏右's avatar
偏右 committed
196
    } = this.props;
197
    const { breadcrumb } = this.state;
198

199
    const clsString = classNames(styles.pageHeader, className);
200 201 202 203
    const activeKeyProps = {};
    if (tabDefaultActiveKey !== undefined) {
      activeKeyProps.defaultActiveKey = tabDefaultActiveKey;
    }
afc163's avatar
afc163 committed
204 205
    if (tabActiveKey !== undefined) {
      activeKeyProps.activeKey = tabActiveKey;
afc163's avatar
afc163 committed
206 207
    }

208 209
    return (
      <div className={clsString}>
210
        {breadcrumb}
211 212 213 214 215 216 217 218 219
        <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
220
              {extraContent && <div className={styles.extraContent}>{extraContent}</div>}
221 222 223
            </div>
          </div>
        </div>
Sean Bao's avatar
Sean Bao committed
224 225 226 227 228 229 230 231 232 233
        {(tabList && tabList.length) ? (
          <Tabs
            className={styles.tabs}
            {...activeKeyProps}
            onChange={this.onChange}
            tabBarExtraContent={tabBarExtraContent}
          >
            {tabList.map(item => <TabPane tab={item.tab} key={item.key} />)}
          </Tabs>
        ) : null}
234 235 236 237
      </div>
    );
  }
}