index.js 6.7 KB
Newer Older
偏右's avatar
偏右 committed
1
import React, { PureComponent, createElement } from 'react';
陈帅's avatar
陈帅 committed
2
import pathToRegexp from 'path-to-regexp';
3
import { Breadcrumb, Tabs, Card } from 'antd';
4 5
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

陈帅's avatar
陈帅 committed
64
    return (
65
      <Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
陈帅's avatar
陈帅 committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        {breadcrumbList.map(item => {
          const title = this.props.itemRender ? this.props.itemRender(item) : item.title;
          return (
            <Breadcrumb.Item key={item.title}>
              {item.href
                ? createElement(
                    linkElement,
                    {
                      [linkElement === 'a' ? 'href' : 'to']: item.href,
                    },
                    title
                  )
                : title}
            </Breadcrumb.Item>
          );
        })}
陈帅's avatar
陈帅 committed
82 83
      </Breadcrumb>
    );
84
  };
陈帅's avatar
陈帅 committed
85

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

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

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

177
  render() {
偏右's avatar
偏右 committed
178
    const {
179 180 181 182 183 184 185 186
      title,
      logo,
      action,
      content,
      extraContent,
      tabList,
      className,
      tabActiveKey,
187
      tabDefaultActiveKey,
188
      tabBarExtraContent,
陈帅's avatar
陈帅 committed
189
      loading = false,
偏右's avatar
偏右 committed
190
    } = this.props;
191
    const { breadcrumb } = this.state;
192

193
    const clsString = classNames(styles.pageHeader, className);
194 195 196 197
    const activeKeyProps = {};
    if (tabDefaultActiveKey !== undefined) {
      activeKeyProps.defaultActiveKey = tabDefaultActiveKey;
    }
afc163's avatar
afc163 committed
198 199
    if (tabActiveKey !== undefined) {
      activeKeyProps.activeKey = tabActiveKey;
afc163's avatar
afc163 committed
200
    }
201
    return (
陈帅's avatar
陈帅 committed
202
      <Card className={clsString} bodyStyle={{ padding: 0 }} loading={loading}>
陈帅's avatar
陈帅 committed
203
        {breadcrumb}
204 205 206 207 208 209 210 211 212
        <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
213
              {extraContent && <div className={styles.extraContent}>{extraContent}</div>}
214 215 216
            </div>
          </div>
        </div>
217
        {tabList &&
218 219 220
          tabList.length && (
            <Tabs
              className={styles.tabs}
afc163's avatar
afc163 committed
221
              {...activeKeyProps}
222
              onChange={this.onChange}
223
              tabBarExtraContent={tabBarExtraContent}
224
            >
225
              {tabList.map(item => <TabPane tab={item.tab} key={item.key} />)}
226
            </Tabs>
227
          )}
228
      </Card>
229 230 231
    );
  }
}