ant-design/components/breadcrumb/index.jsx

96 lines
2.4 KiB
React
Raw Normal View History

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;
let link = <a className={`${prefixCls}-link`} {...this.props}>{children}</a>;
2015-07-01 20:44:19 +08:00
if (typeof this.props.href === 'undefined') {
link = <span className={`${prefixCls}-link`} {...this.props}>{children}</span>;
2015-07-01 20:44:19 +08:00
}
return (
<span>
{link}
<span className={`${prefixCls}-separator`}>{separator}</span>
</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,
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;
const { separator, prefixCls, routes, params, children } = this.props;
if (routes && routes.length > 0) {
const paths = [];
crumbs = routes.map((route, i) => {
2015-11-23 11:01:17 +08:00
if (!route.breadcrumbName) {
return null;
}
const name = route.breadcrumbName.replace(/\:(.*)/g, (replacement, key) => {
return params[key] || replacement;
});
2015-09-01 16:18:46 +08:00
let link;
2016-02-01 11:33:02 +08:00
let path = route.path.replace(/^\//, '');
Object.keys(params).forEach(key => {
path = path.replace(`:${key}`, params[key]);
});
2016-02-01 11:33:02 +08:00
if (path) {
paths.push(path);
}
2015-07-02 18:33:37 +08:00
if (i === routes.length - 1) {
link = <span>{name}</span>;
} else {
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;