2016-07-14 13:29:50 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { cloneElement } from 'react';
|
2016-03-31 17:46:35 +08:00
|
|
|
import BreadcrumbItem from './BreadcrumbItem';
|
|
|
|
|
2016-06-11 16:11:08 +08:00
|
|
|
const defaultNameRender = (breadcrumbName, route, params) => {
|
|
|
|
if (!breadcrumbName) {
|
2016-06-10 21:12:52 +08:00
|
|
|
return null;
|
|
|
|
}
|
2016-06-11 17:05:19 +08:00
|
|
|
const paramsKeys = Object.keys(params).join('|');
|
2016-06-11 16:11:08 +08:00
|
|
|
const name = breadcrumbName.replace(
|
2016-06-11 17:05:19 +08:00
|
|
|
new RegExp(`:(${paramsKeys})`, 'g'),
|
|
|
|
(replacement, key) => params[key] || replacement
|
2016-06-11 16:11:08 +08:00
|
|
|
);
|
|
|
|
return <span>{name}</span>;
|
2016-06-10 21:12:52 +08:00
|
|
|
};
|
|
|
|
|
2016-07-14 13:29:50 +08:00
|
|
|
interface BreadcrumbProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
routes?: Array<any>;
|
|
|
|
params?: Object;
|
|
|
|
separator?: string | React.ReactNode;
|
|
|
|
linkRender?: (link, name, paths: Array<any>) => React.ReactNode;
|
|
|
|
nameRender?: (breadcrumbName, route, params) => React.ReactNode;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Breadcrumb extends React.Component<BreadcrumbProps, any> {
|
|
|
|
static Item: any;
|
|
|
|
|
2016-03-31 17:46:35 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-breadcrumb',
|
|
|
|
separator: '/',
|
2016-06-11 18:19:52 +08:00
|
|
|
linkRender: (href, name) => <a href={`#${href}`}>{name}</a>,
|
2016-06-11 16:11:08 +08:00
|
|
|
nameRender: defaultNameRender,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-31 17:46:35 +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,
|
|
|
|
linkRender: React.PropTypes.func,
|
|
|
|
nameRender: React.PropTypes.func,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
render() {
|
|
|
|
let crumbs;
|
2016-06-11 16:11:08 +08:00
|
|
|
const { separator, prefixCls, routes, params, children, linkRender, nameRender } = this.props;
|
2016-03-31 17:46:35 +08:00
|
|
|
if (routes && routes.length > 0) {
|
|
|
|
const paths = [];
|
2016-06-10 22:22:07 +08:00
|
|
|
const lastPath = routes.length - 1;
|
2016-03-31 17:46:35 +08:00
|
|
|
crumbs = routes.map((route, i) => {
|
2016-04-11 11:31:35 +08:00
|
|
|
route.path = route.path || '';
|
2016-03-31 17:46:35 +08:00
|
|
|
let path = route.path.replace(/^\//, '');
|
|
|
|
Object.keys(params).forEach(key => {
|
|
|
|
path = path.replace(`:${key}`, params[key]);
|
|
|
|
});
|
|
|
|
if (path) {
|
|
|
|
paths.push(path);
|
|
|
|
}
|
2016-06-11 16:11:08 +08:00
|
|
|
const name = nameRender(route.breadcrumbName, route, params);
|
2016-06-10 22:22:07 +08:00
|
|
|
if (name) {
|
2016-06-11 18:19:52 +08:00
|
|
|
const link = (i === lastPath) ? name : linkRender(`/${paths.join('/')}`, name, paths);
|
2016-06-11 16:11:08 +08:00
|
|
|
return <BreadcrumbItem separator={separator} key={route.breadcrumbName || i}>{link}</BreadcrumbItem>;
|
2016-03-31 17:46:35 +08:00
|
|
|
}
|
2016-06-10 22:22:07 +08:00
|
|
|
return null;
|
2016-03-31 17:46:35 +08:00
|
|
|
});
|
|
|
|
} else {
|
2016-07-14 13:29:50 +08:00
|
|
|
crumbs = React.Children.map(children, (element: any, index) => {
|
2016-03-31 17:46:35 +08:00
|
|
|
return cloneElement(element, {
|
|
|
|
separator,
|
|
|
|
key: index,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div className={prefixCls}>
|
|
|
|
{crumbs}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|