index.js 6.35 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
  componentDidUpdate(preProps) {
陈帅's avatar
陈帅 committed
30 31
    const { tabActiveKey } = this.props;
    if (preProps.tabActiveKey !== tabActiveKey) {
jim's avatar
jim committed
32 33 34
      this.getBreadcrumbDom();
    }
  }
陈帅's avatar
陈帅 committed
35

jim's avatar
jim committed
36
  onChange = key => {
陈帅's avatar
陈帅 committed
37 38 39
    const { onTabChange } = this.props;
    if (onTabChange) {
      onTabChange(key);
40 41
    }
  };
陈帅's avatar
陈帅 committed
42

43
  getBreadcrumbProps = () => {
陈帅's avatar
陈帅 committed
44
    const { routes, params, location, breadcrumbNameMap } = this.props;
45
    return {
陈帅's avatar
陈帅 committed
46 47 48 49
      routes,
      params,
      routerLocation: location,
      breadcrumbNameMap,
50 51
    };
  };
陈帅's avatar
陈帅 committed
52

53 54 55 56 57 58
  getBreadcrumbDom = () => {
    const breadcrumb = this.conversionBreadcrumbList();
    this.setState({
      breadcrumb,
    });
  };
陈帅's avatar
陈帅 committed
59

陈帅'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

陈帅's avatar
陈帅 committed
82
  conversionFromLocation = (routerLocation, breadcrumbNameMap) => {
83
    const { breadcrumbSeparator, linkElement = 'a' } = this.props;
jim's avatar
jim committed
84 85
    // Convert the url to an array
    const pathSnippets = urlToList(routerLocation.pathname);
陈帅's avatar
陈帅 committed
86
    // Loop data mosaic routing
jim's avatar
jim committed
87
    const extraBreadcrumbItems = pathSnippets.map((url, index) => {
陈帅's avatar
陈帅 committed
88
      const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url);
jim's avatar
jim committed
89
      const isLinkable = index !== pathSnippets.length - 1 && currentBreadcrumb.component;
陈帅's avatar
陈帅 committed
90 91 92 93 94
      return currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? (
        <Breadcrumb.Item key={url}>
          {createElement(
            isLinkable ? linkElement : 'span',
            { [linkElement === 'a' ? 'href' : 'to']: url },
jim's avatar
jim committed
95
            currentBreadcrumb.name
陈帅's avatar
陈帅 committed
96 97 98 99 100 101 102
          )}
        </Breadcrumb.Item>
      ) : null;
    });
    // Add home breadcrumbs to your head
    extraBreadcrumbItems.unshift(
      <Breadcrumb.Item key="home">
103 104 105 106 107
        {createElement(
          linkElement,
          {
            [linkElement === 'a' ? 'href' : 'to']: '/',
          },
jim's avatar
jim committed
108
          '首页'
109
        )}
jim's avatar
jim committed
110
      </Breadcrumb.Item>
陈帅's avatar
陈帅 committed
111 112
    );
    return (
113
      <Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
陈帅's avatar
陈帅 committed
114 115 116
        {extraBreadcrumbItems}
      </Breadcrumb>
    );
117
  };
陈帅's avatar
陈帅 committed
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

陈帅's avatar
陈帅 committed
150 151
  // 渲染Breadcrumb 子节点
  // Render the Breadcrumb child node
偏右's avatar
偏右 committed
152 153 154
  itemRender = (route, params, routes, paths) => {
    const { linkElement = 'a' } = this.props;
    const last = routes.indexOf(route) === routes.length - 1;
155 156 157 158 159 160 161 162 163
    return last || !route.component ? (
      <span>{route.breadcrumbName}</span>
    ) : (
      createElement(
        linkElement,
        {
          href: paths.join('/') || '/',
          to: paths.join('/') || '/',
        },
jim's avatar
jim committed
164
        route.breadcrumbName
165 166 167
      )
    );
  };
陈帅's avatar
陈帅 committed
168

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

183
    const clsString = classNames(styles.pageHeader, className);
184 185 186 187
    const activeKeyProps = {};
    if (tabDefaultActiveKey !== undefined) {
      activeKeyProps.defaultActiveKey = tabDefaultActiveKey;
    }
afc163's avatar
afc163 committed
188 189
    if (tabActiveKey !== undefined) {
      activeKeyProps.activeKey = tabActiveKey;
afc163's avatar
afc163 committed
190
    }
陈帅's avatar
陈帅 committed
191
    const { breadcrumb } = this.state;
192 193
    return (
      <div className={clsString}>
陈帅's avatar
陈帅 committed
194
        {breadcrumb}
195 196 197 198 199 200 201 202 203
        <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
204
              {extraContent && <div className={styles.extraContent}>{extraContent}</div>}
205 206 207
            </div>
          </div>
        </div>
208
        {tabList &&
209 210 211
          tabList.length && (
            <Tabs
              className={styles.tabs}
afc163's avatar
afc163 committed
212
              {...activeKeyProps}
213
              onChange={this.onChange}
214
              tabBarExtraContent={tabBarExtraContent}
215
            >
216
              {tabList.map(item => <TabPane tab={item.tab} key={item.key} />)}
217
            </Tabs>
218
          )}
219 220 221 222
      </div>
    );
  }
}