index.jsx 2.4 KB
Newer Older
A
afc163 已提交
1
import React, { cloneElement } from 'react';
A
afc163 已提交
2

A
afc163 已提交
3 4 5 6
const BreadcrumbItem = React.createClass({
  getDefaultProps() {
    return {
      prefixCls: 'ant-breadcrumb',
A
afc163 已提交
7
      separator: '/',
A
afc163 已提交
8 9
    };
  },
A
afc163 已提交
10
  propTypes: {
A
afc163 已提交
11
    prefixCls: React.PropTypes.string,
A
afc163 已提交
12
    separator: React.PropTypes.oneOfType([
A
afc163 已提交
13 14 15 16
      React.PropTypes.string,
      React.PropTypes.element,
    ]),
    href: React.PropTypes.string,
A
afc163 已提交
17
  },
A
afc163 已提交
18
  render() {
A
afc163 已提交
19
    const { prefixCls, separator, children } = this.props;
20
    let link = <a className={`${prefixCls}-link`} {...this.props}>{children}</a>;
A
afc163 已提交
21
    if (typeof this.props.href === 'undefined') {
22
      link = <span className={`${prefixCls}-link`} {...this.props}>{children}</span>;
A
afc163 已提交
23
    }
24 25 26
    return (
      <span>
        {link}
27
        <span className={`${prefixCls}-separator`}>{separator}</span>
28 29
      </span>
    );
A
afc163 已提交
30 31 32
  }
});

A
afc163 已提交
33 34 35 36
const Breadcrumb = React.createClass({
  getDefaultProps() {
    return {
      prefixCls: 'ant-breadcrumb',
A
afc163 已提交
37
      separator: '/',
A
afc163 已提交
38 39
    };
  },
A
afc163 已提交
40
  propTypes: {
A
afc163 已提交
41
    prefixCls: React.PropTypes.string,
A
afc163 已提交
42
    separator: React.PropTypes.oneOfType([
A
afc163 已提交
43 44 45
      React.PropTypes.string,
      React.PropTypes.element,
    ]),
A
afc163 已提交
46
    routes: React.PropTypes.array,
47
    params: React.PropTypes.object,
A
afc163 已提交
48
  },
A
afc163 已提交
49
  render() {
A
afc163 已提交
50
    let crumbs;
51 52 53
    const { separator, prefixCls, routes, params, children } = this.props;
    if (routes && routes.length > 0) {
      const paths = [];
54
      crumbs = routes.map((route, i) => {
A
afc163 已提交
55 56 57
        if (!route.breadcrumbName) {
          return null;
        }
58
        const name = route.breadcrumbName.replace(/\:(.*)/g, (replacement, key) => {
59 60
          return params[key] || replacement;
        });
61

A
afc163 已提交
62
        let link;
A
afc163 已提交
63
        let path = route.path.replace(/^\//, '');
64
        Object.keys(params).forEach(key => {
65
          path = path.replace(`:${key}`, params[key]);
66
        });
A
afc163 已提交
67 68 69
        if (path) {
          paths.push(path);
        }
70

A
afc163 已提交
71 72 73
        if (i === routes.length - 1) {
          link = <span>{name}</span>;
        } else {
74
          link = <a href={`#/${paths.join('/')}`}>{name}</a>;
A
afc163 已提交
75
        }
A
afc163 已提交
76
        return <BreadcrumbItem separator={separator} key={name}>{link}</BreadcrumbItem>;
A
afc163 已提交
77 78
      });
    } else {
A
afc163 已提交
79 80
      crumbs = React.Children.map(children, (element, index) => {
        return cloneElement(element, {
A
afc163 已提交
81
          separator,
A
afc163 已提交
82 83 84
          key: index,
        });
      });
A
afc163 已提交
85
    }
A
afc163 已提交
86 87
    return (
      <div className={prefixCls}>
A
afc163 已提交
88
        {crumbs}
A
afc163 已提交
89 90 91 92 93 94 95
      </div>
    );
  }
});

Breadcrumb.Item = BreadcrumbItem;
export default Breadcrumb;