You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.1 KiB
73 lines
2.1 KiB
9 years ago
|
import React, { cloneElement } from 'react';
|
||
|
import BreadcrumbItem from './BreadcrumbItem';
|
||
|
|
||
9 years ago
|
const defaultNameRender = (breadcrumbName, route, params) => {
|
||
|
if (!breadcrumbName) {
|
||
9 years ago
|
return null;
|
||
|
}
|
||
9 years ago
|
const paramsKeys = Object.keys(params).join('|');
|
||
9 years ago
|
const name = breadcrumbName.replace(
|
||
9 years ago
|
new RegExp(`:(${paramsKeys})`, 'g'),
|
||
|
(replacement, key) => params[key] || replacement
|
||
9 years ago
|
);
|
||
|
return <span>{name}</span>;
|
||
9 years ago
|
};
|
||
|
|
||
9 years ago
|
export default class Breadcrumb extends React.Component {
|
||
|
static defaultProps = {
|
||
|
prefixCls: 'ant-breadcrumb',
|
||
|
separator: '/',
|
||
9 years ago
|
linkRender: (href, name) => <a href={`#${href}`}>{name}</a>,
|
||
9 years ago
|
nameRender: defaultNameRender,
|
||
9 years ago
|
}
|
||
|
|
||
|
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,
|
||
|
}
|
||
|
|
||
|
render() {
|
||
|
let crumbs;
|
||
9 years ago
|
const { separator, prefixCls, routes, params, children, linkRender, nameRender } = this.props;
|
||
9 years ago
|
if (routes && routes.length > 0) {
|
||
|
const paths = [];
|
||
9 years ago
|
const lastPath = routes.length - 1;
|
||
9 years ago
|
crumbs = routes.map((route, i) => {
|
||
9 years ago
|
route.path = route.path || '';
|
||
9 years ago
|
let path = route.path.replace(/^\//, '');
|
||
|
Object.keys(params).forEach(key => {
|
||
|
path = path.replace(`:${key}`, params[key]);
|
||
|
});
|
||
|
if (path) {
|
||
|
paths.push(path);
|
||
|
}
|
||
9 years ago
|
const name = nameRender(route.breadcrumbName, route, params);
|
||
9 years ago
|
if (name) {
|
||
9 years ago
|
const link = (i === lastPath) ? name : linkRender(`/${paths.join('/')}`, name, paths);
|
||
9 years ago
|
return <BreadcrumbItem separator={separator} key={route.breadcrumbName || i}>{link}</BreadcrumbItem>;
|
||
9 years ago
|
}
|
||
9 years ago
|
return null;
|
||
9 years ago
|
});
|
||
|
} else {
|
||
|
crumbs = React.Children.map(children, (element, index) => {
|
||
|
return cloneElement(element, {
|
||
|
separator,
|
||
|
key: index,
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
return (
|
||
|
<div className={prefixCls}>
|
||
|
{crumbs}
|
||
|
</div>
|
||
|
);
|
||
|
}
|
||
|
}
|