2015-12-07 12:13:13 +08:00
|
|
|
import React, { cloneElement } from 'react';
|
2015-07-01 20:44:19 +08:00
|
|
|
|
2016-03-18 10:31:25 +08:00
|
|
|
/* Exported as Breadcrumb.Item */
|
|
|
|
class BreadcrumbItem extends React.Component {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-breadcrumb',
|
|
|
|
separator: '/',
|
|
|
|
}
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
prefixCls: React.PropTypes.string,
|
|
|
|
separator: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.string,
|
|
|
|
React.PropTypes.element,
|
|
|
|
]),
|
|
|
|
href: React.PropTypes.string,
|
|
|
|
}
|
|
|
|
|
2015-07-01 20:44:19 +08:00
|
|
|
render() {
|
2016-03-30 19:30:16 +08:00
|
|
|
const { prefixCls, separator, children, ...restProps } = this.props;
|
|
|
|
let link;
|
|
|
|
if ('href' in this.props) {
|
|
|
|
link = <a className={`${prefixCls}-link`} {...restProps}>{children}</a>;
|
|
|
|
} else {
|
|
|
|
link = <span className={`${prefixCls}-link`} {...restProps}>{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
|
|
|
}
|
2016-03-18 10:31:25 +08:00
|
|
|
}
|
2015-07-01 20:44:19 +08:00
|
|
|
|
2016-03-29 14:01:10 +08:00
|
|
|
export default class Breadcrumb extends React.Component {
|
|
|
|
static Item = BreadcrumbItem;
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-breadcrumb',
|
|
|
|
separator: '/',
|
|
|
|
linkRender: (href, name) => <a href={`#${href}`}>{name}</a>,
|
2016-03-30 19:46:54 +08:00
|
|
|
nameRender: (name) => <span>{name}</span>,
|
2016-03-29 14:01:10 +08:00
|
|
|
}
|
2016-03-18 10:31:25 +08:00
|
|
|
|
2016-03-29 14:01:10 +08:00
|
|
|
static propTypes = {
|
|
|
|
prefixCls: React.PropTypes.string,
|
|
|
|
separator: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.string,
|
|
|
|
React.PropTypes.element,
|
|
|
|
]),
|
|
|
|
routes: React.PropTypes.array,
|
|
|
|
params: React.PropTypes.object,
|
2016-03-30 19:46:54 +08:00
|
|
|
linkRender: React.PropTypes.func,
|
|
|
|
nameRender: React.PropTypes.func,
|
2016-03-29 14:01:10 +08:00
|
|
|
}
|
2016-03-18 10:31:25 +08:00
|
|
|
|
2015-07-01 20:44:19 +08:00
|
|
|
render() {
|
2015-09-15 17:35:22 +08:00
|
|
|
let crumbs;
|
2016-03-30 19:46:54 +08:00
|
|
|
const { separator, prefixCls, routes, params, children, linkRender, nameRender } = this.props;
|
2016-01-23 20:12:09 +08:00
|
|
|
if (routes && routes.length > 0) {
|
|
|
|
const paths = [];
|
2016-01-23 15:22:16 +08:00
|
|
|
crumbs = routes.map((route, i) => {
|
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
|
|
|
|
2016-03-23 00:26:22 +08:00
|
|
|
if (!route.breadcrumbName) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const name = route.breadcrumbName.replace(/\:(.*)/g, (replacement, key) => {
|
|
|
|
return params[key] || replacement;
|
|
|
|
});
|
|
|
|
|
|
|
|
let link;
|
2015-07-02 18:33:37 +08:00
|
|
|
if (i === routes.length - 1) {
|
2016-03-30 19:46:54 +08:00
|
|
|
link = nameRender(name);
|
2015-07-02 18:33:37 +08:00
|
|
|
} else {
|
2016-02-18 11:31:49 +08:00
|
|
|
link = linkRender(`/${paths.join('/')}`, name);
|
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>
|
|
|
|
);
|
|
|
|
}
|
2016-03-18 10:31:25 +08:00
|
|
|
}
|