ant-design/components/breadcrumb/index.jsx

53 lines
1.5 KiB
React
Raw Normal View History

2015-07-01 20:44:19 +08:00
'use strict';
import React from 'react';
let prefixCls = 'ant-breadcrumb';
let BreadcrumbItem = React.createClass({
render() {
var link = <a className={prefixCls + '-link'} {...this.props}>{this.props.children}</a>;
var slash = <span className={prefixCls + '-slash'}>/</span>;
if (typeof this.props.href === 'undefined') {
link = <span className={prefixCls + '-link'} {...this.props}>{this.props.children}</span>;
}
2015-07-02 17:22:26 +08:00
return <span>{link}{slash}</span>;
2015-07-01 20:44:19 +08:00
}
});
let Breadcrumb = React.createClass({
2015-07-02 17:22:26 +08:00
contextTypes: {
router: React.PropTypes.func.isRequired
},
2015-07-01 20:44:19 +08:00
render() {
2015-07-02 17:22:26 +08:00
var crumbs, routes, params;
if (this.context.router && this.props.Router) {
var Link = this.props.Router.Link;
2015-07-02 17:22:26 +08:00
routes = this.context.router.getCurrentRoutes();
params = this.context.router.getCurrentParams();
2015-07-02 18:33:37 +08:00
crumbs = routes.map(function(route, i) {
var name = route.name.replace(/\:(.*)/g, function(replacement, key) {
return params[key] || replacement;
});
2015-07-02 18:33:37 +08:00
var link;
if (i === routes.length - 1) {
link = <span>{name}</span>;
} else {
link = <Link to={route.path} params={params}>{name}</Link>;
}
return <BreadcrumbItem>{link}</BreadcrumbItem>;
2015-07-02 17:22:26 +08:00
});
} else {
crumbs = this.props.children;
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;