index.js 6.83 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, Skeleton } 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;
陈帅's avatar
陈帅 committed
9
export const 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 || {};
陈帅's avatar
陈帅 committed
19 20
};

21
export default class PageHeader extends PureComponent {
22 23 24 25 26 27 28 29
  state = {
    breadcrumb: null,
  };

  componentDidMount() {
    this.getBreadcrumbDom();
  }

jim's avatar
jim committed
30
  componentDidUpdate(preProps) {
陈帅's avatar
陈帅 committed
31 32 33 34 35 36
    const {
      tabActiveKey,
      location: { pathname },
    } = this.props;
    const prePathname = preProps.location.pathname;
    if (preProps.tabActiveKey !== tabActiveKey || prePathname !== pathname) {
jim's avatar
jim committed
37 38 39
      this.getBreadcrumbDom();
    }
  }
陈帅's avatar
陈帅 committed
40

jim's avatar
jim committed
41
  onChange = key => {
陈帅's avatar
陈帅 committed
42 43 44
    const { onTabChange } = this.props;
    if (onTabChange) {
      onTabChange(key);
45 46
    }
  };
陈帅's avatar
陈帅 committed
47

48
  getBreadcrumbProps = () => {
陈帅's avatar
陈帅 committed
49
    const { routes, params, location, breadcrumbNameMap } = this.props;
50
    return {
陈帅's avatar
陈帅 committed
51 52 53 54
      routes,
      params,
      routerLocation: location,
      breadcrumbNameMap,
55 56
    };
  };
陈帅's avatar
陈帅 committed
57

58 59 60 61 62 63
  getBreadcrumbDom = () => {
    const breadcrumb = this.conversionBreadcrumbList();
    this.setState({
      breadcrumb,
    });
  };
陈帅's avatar
陈帅 committed
64

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

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

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

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

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

194
    const clsString = classNames(styles.pageHeader, className);
195 196 197 198
    const activeKeyProps = {};
    if (tabDefaultActiveKey !== undefined) {
      activeKeyProps.defaultActiveKey = tabDefaultActiveKey;
    }
afc163's avatar
afc163 committed
199 200
    if (tabActiveKey !== undefined) {
      activeKeyProps.activeKey = tabActiveKey;
afc163's avatar
afc163 committed
201
    }
202
    return (
203 204 205 206 207 208 209 210 211 212 213 214 215 216
      <div className={clsString}>
        <Skeleton loading={loading}>
          {breadcrumb}
          <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>}
                {extraContent && <div className={styles.extraContent}>{extraContent}</div>}
              </div>
217 218
            </div>
          </div>
219 220 221 222 223 224 225 226 227 228 229 230 231 232
          {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}
        </Skeleton>
      </div>
233 234 235
    );
  }
}