2015-12-07 12:13:13 +08:00
|
|
|
import React, { cloneElement } from 'react';
|
2015-07-01 20:44:19 +08:00
|
|
|
|
2015-12-07 12:13:13 +08:00
|
|
|
const BreadcrumbItem = React.createClass({
|
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
|
|
|
prefixCls: 'ant-breadcrumb',
|
2015-12-09 16:48:52 +08:00
|
|
|
separator: '/',
|
2015-12-07 12:13:13 +08:00
|
|
|
};
|
|
|
|
},
|
2015-09-01 16:18:46 +08:00
|
|
|
propTypes: {
|
2015-12-07 12:13:13 +08:00
|
|
|
prefixCls: React.PropTypes.string,
|
2015-12-09 16:48:52 +08:00
|
|
|
separator: React.PropTypes.oneOfType([
|
2015-12-07 12:13:13 +08:00
|
|
|
React.PropTypes.string,
|
|
|
|
React.PropTypes.element,
|
|
|
|
]),
|
|
|
|
href: React.PropTypes.string,
|
2015-09-01 16:18:46 +08:00
|
|
|
},
|
2015-07-01 20:44:19 +08:00
|
|
|
render() {
|
2015-12-09 16:48:52 +08:00
|
|
|
const { prefixCls, separator, children } = this.props;
|
2016-02-17 18:04:42 +08:00
|
|
|
let link = <a className={`${prefixCls}-link`} {...this.props}>{children}</a>;
|
2015-07-01 20:44:19 +08:00
|
|
|
if (typeof this.props.href === 'undefined') {
|
2016-02-17 18:04:42 +08:00
|
|
|
link = <span className={`${prefixCls}-link`} {...this.props}>{children}</span>;
|
2015-07-01 20:44:19 +08:00
|
|
|
}
|
2016-01-07 14:21:29 +08:00
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
{link}
|
2016-02-17 18:04:42 +08:00
|
|
|
<span className={`${prefixCls}-separator`}>{separator}</span>
|
2016-01-07 14:21:29 +08:00
|
|
|
</span>
|
|
|
|
);
|
2015-07-01 20:44:19 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-12-07 12:13:13 +08:00
|
|
|
const Breadcrumb = React.createClass({
|
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
|
|
|
prefixCls: 'ant-breadcrumb',
|
2015-12-09 16:48:52 +08:00
|
|
|
separator: '/',
|
2015-12-07 12:13:13 +08:00
|
|
|
};
|
|
|
|
},
|
2015-09-01 16:18:46 +08:00
|
|
|
propTypes: {
|
2015-12-07 12:13:13 +08:00
|
|
|
prefixCls: React.PropTypes.string,
|
2015-12-09 16:48:52 +08:00
|
|
|
separator: React.PropTypes.oneOfType([
|
2015-12-07 12:13:13 +08:00
|
|
|
React.PropTypes.string,
|
|
|
|
React.PropTypes.element,
|
|
|
|
]),
|
2015-09-15 17:35:22 +08:00
|
|
|
routes: React.PropTypes.array,
|
2016-01-23 20:12:09 +08:00
|
|
|
params: React.PropTypes.object,
|
2015-07-02 17:22:26 +08:00
|
|
|
},
|
2015-07-01 20:44:19 +08:00
|
|
|
render() {
|
2015-09-15 17:35:22 +08:00
|
|
|
let crumbs;
|
2016-01-23 20:12:09 +08:00
|
|
|
const { separator, prefixCls, routes, params, children } = this.props;
|
|
|
|
if (routes && routes.length > 0) {
|
|
|
|
const paths = [];
|
2016-01-23 15:22:16 +08:00
|
|
|
crumbs = routes.map((route, i) => {
|
2015-11-23 11:01:17 +08:00
|
|
|
if (!route.breadcrumbName) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-01-23 15:22:16 +08:00
|
|
|
const name = route.breadcrumbName.replace(/\:(.*)/g, (replacement, key) => {
|
2015-07-02 18:05:34 +08:00
|
|
|
return params[key] || replacement;
|
|
|
|
});
|
2016-01-23 20:12:09 +08:00
|
|
|
|
2015-09-01 16:18:46 +08:00
|
|
|
let link;
|
2016-02-01 11:33:02 +08:00
|
|
|
let path = route.path.replace(/^\//, '');
|
2016-01-23 20:12:09 +08:00
|
|
|
Object.keys(params).forEach(key => {
|
2016-02-17 18:04:42 +08:00
|
|
|
path = path.replace(`:${key}`, params[key]);
|
2016-01-23 20:12:09 +08:00
|
|
|
});
|
2016-02-01 11:33:02 +08:00
|
|
|
if (path) {
|
|
|
|
paths.push(path);
|
|
|
|
}
|
2016-01-23 20:12:09 +08:00
|
|
|
|
2015-07-02 18:33:37 +08:00
|
|
|
if (i === routes.length - 1) {
|
|
|
|
link = <span>{name}</span>;
|
|
|
|
} else {
|
2016-02-17 18:04:42 +08:00
|
|
|
link = <a href={`#/${paths.join('/')}`}>{name}</a>;
|
2015-07-02 18:33:37 +08:00
|
|
|
}
|
2015-12-09 16:48:52 +08:00
|
|
|
return <BreadcrumbItem separator={separator} key={name}>{link}</BreadcrumbItem>;
|
2015-07-02 17:22:26 +08:00
|
|
|
});
|
|
|
|
} else {
|
2015-12-07 12:13:13 +08:00
|
|
|
crumbs = React.Children.map(children, (element, index) => {
|
|
|
|
return cloneElement(element, {
|
2015-12-09 16:48:52 +08:00
|
|
|
separator,
|
2015-12-07 12:13:13 +08:00
|
|
|
key: index,
|
|
|
|
});
|
|
|
|
});
|
2015-07-01 21:24:04 +08:00
|
|
|
}
|
2015-07-01 20:44:19 +08:00
|
|
|
return (
|
|
|
|
<div className={prefixCls}>
|
2015-07-02 17:22:26 +08:00
|
|
|
{crumbs}
|
2015-07-01 20:44:19 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Breadcrumb.Item = BreadcrumbItem;
|
|
|
|
export default Breadcrumb;
|