ant-design/components/breadcrumb/index.jsx

88 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;
2015-12-07 12:13:13 +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') {
2015-12-07 12:13:13 +08:00
link = <span className={prefixCls + '-link'} {...this.props}>{children}</span>;
2015-07-01 20:44:19 +08:00
}
2015-12-07 12:13:13 +08:00
return <span>
{link}
2015-12-09 16:48:52 +08:00
<span className={prefixCls + '-separator'}>{separator}</span>
2015-12-07 12:13:13 +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
router: React.PropTypes.object,
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;
2015-12-09 16:48:52 +08:00
const { separator, prefixCls, router, routes, params, children } = this.props;
2015-12-07 12:13:13 +08:00
const ReactRouter = router;
2015-09-15 17:35:22 +08:00
if (routes && routes.length > 0 && ReactRouter) {
2015-09-01 16:18:46 +08:00
let Link = ReactRouter.Link;
2015-07-02 18:33:37 +08:00
crumbs = routes.map(function(route, i) {
2015-11-23 11:01:17 +08:00
if (!route.breadcrumbName) {
return null;
}
2015-12-07 12:13:13 +08:00
const name = route.breadcrumbName.replace(/\:(.*)/g, function(replacement, key) {
return params[key] || replacement;
});
2015-09-01 16:18:46 +08:00
let link;
2015-12-07 12:13:13 +08:00
const path = route.path.indexOf('/') === 0 ? route.path : ('/' + route.path);
2015-07-02 18:33:37 +08:00
if (i === routes.length - 1) {
link = <span>{name}</span>;
} else {
2015-09-15 17:35:22 +08:00
link = <Link to={path} params={params}>{name}</Link>;
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;