index.js 6.85 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';
陈帅's avatar
陈帅 committed
4 5
import memoizeOne from 'memoize-one';
import deepEqual from 'lodash.isequal';
6 7
import classNames from 'classnames';
import styles from './index.less';
jim's avatar
jim committed
8
import { urlToList } from '../_utils/pathTools';
陈帅's avatar
陈帅 committed
9

afc163's avatar
afc163 committed
10
const { TabPane } = Tabs;
陈帅's avatar
陈帅 committed
11
export const getBreadcrumb = (breadcrumbNameMap, url) => {
12 13
  let breadcrumb = breadcrumbNameMap[url];
  if (!breadcrumb) {
jim's avatar
jim committed
14
    Object.keys(breadcrumbNameMap).forEach(item => {
15 16 17 18 19 20
      if (pathToRegexp(item).test(url)) {
        breadcrumb = breadcrumbNameMap[item];
      }
    });
  }
  return breadcrumb || {};
陈帅's avatar
陈帅 committed
21 22
};

23
export default class PageHeader extends PureComponent {
陈帅's avatar
陈帅 committed
24 25 26 27 28
  constructor(props) {
    super(props);
    this.conversionFromLocation = memoizeOne(this.conversionFromLocation, deepEqual);
  }

29 30 31 32 33 34 35 36
  state = {
    breadcrumb: null,
  };

  componentDidMount() {
    this.getBreadcrumbDom();
  }

jim's avatar
jim committed
37
  componentDidUpdate(preProps) {
陈帅's avatar
陈帅 committed
38 39
    const { tabActiveKey } = this.props;
    if (preProps.tabActiveKey !== tabActiveKey) {
jim's avatar
jim committed
40 41 42
      this.getBreadcrumbDom();
    }
  }
陈帅's avatar
陈帅 committed
43

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

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

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

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

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

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

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

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

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