2016-09-21 11:54:53 +08:00
|
|
|
import React from 'react';
|
2016-07-14 13:29:50 +08:00
|
|
|
import { cloneElement } from 'react';
|
2016-11-01 11:10:11 +08:00
|
|
|
import warning from '../_util/warning';
|
2016-03-31 17:46:35 +08:00
|
|
|
import BreadcrumbItem from './BreadcrumbItem';
|
2016-12-30 21:41:28 +08:00
|
|
|
import classNames from 'classnames';
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2016-08-29 16:52:35 +08:00
|
|
|
export interface BreadcrumbProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
routes?: Array<any>;
|
|
|
|
params?: Object;
|
2017-01-20 20:10:50 +08:00
|
|
|
separator?: React.ReactNode;
|
2016-10-18 11:55:00 +08:00
|
|
|
itemRender?: (route: any, params: any, routes: Array<any>, paths: Array<string>) => React.ReactNode;
|
2016-08-29 16:52:35 +08:00
|
|
|
style?: React.CSSProperties;
|
2016-12-30 21:41:28 +08:00
|
|
|
className?: string;
|
2016-08-29 16:52:35 +08:00
|
|
|
};
|
|
|
|
|
2016-10-20 19:19:16 +08:00
|
|
|
function getBreadcrumbName(route, params) {
|
2016-08-29 16:52:35 +08:00
|
|
|
if (!route.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-08-29 16:52:35 +08:00
|
|
|
const name = route.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
|
|
|
);
|
2016-08-29 16:52:35 +08:00
|
|
|
return name;
|
2016-07-14 13:29:50 +08:00
|
|
|
}
|
|
|
|
|
2016-10-24 12:04:26 +08:00
|
|
|
function defaultItemRender(route, params, routes, paths) {
|
|
|
|
const isLastItem = routes.indexOf(route) === routes.length - 1;
|
|
|
|
const name = getBreadcrumbName(route, params);
|
|
|
|
return isLastItem
|
|
|
|
? <span>{name}</span>
|
|
|
|
: <a href={`#/${paths.join('/')}`}>{name}</a>;
|
|
|
|
}
|
|
|
|
|
2016-07-14 13:29:50 +08:00
|
|
|
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-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
prefixCls: React.PropTypes.string,
|
2017-01-20 20:10:50 +08:00
|
|
|
separator: React.PropTypes.node,
|
2016-03-31 17:46:35 +08:00
|
|
|
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
|
|
|
|
2016-11-01 11:10:11 +08:00
|
|
|
componentDidMount() {
|
|
|
|
const props = this.props;
|
2016-09-09 13:55:46 +08:00
|
|
|
warning(
|
|
|
|
!('linkRender' in props || 'nameRender' in props),
|
2016-12-02 15:07:33 +08:00
|
|
|
'`linkRender` and `nameRender` are removed, please use `itemRender` instead, ' +
|
|
|
|
'see: http://u.ant.design/item-render.'
|
2016-09-09 13:55:46 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:46:35 +08:00
|
|
|
render() {
|
|
|
|
let crumbs;
|
2016-12-30 21:41:28 +08:00
|
|
|
const {
|
|
|
|
separator, prefixCls, style, className, routes, params = {},
|
|
|
|
children, itemRender = defaultItemRender,
|
|
|
|
} = this.props;
|
2016-03-31 17:46:35 +08:00
|
|
|
if (routes && routes.length > 0) {
|
2016-10-24 12:04:26 +08:00
|
|
|
const paths: string[] = [];
|
2016-10-20 19:19:16 +08:00
|
|
|
crumbs = routes.map((route) => {
|
2016-04-11 11:31:35 +08:00
|
|
|
route.path = route.path || '';
|
2016-10-24 12:04:26 +08:00
|
|
|
let path: string = route.path.replace(/^\//, '');
|
2016-03-31 17:46:35 +08:00
|
|
|
Object.keys(params).forEach(key => {
|
|
|
|
path = path.replace(`:${key}`, params[key]);
|
|
|
|
});
|
|
|
|
if (path) {
|
|
|
|
paths.push(path);
|
|
|
|
}
|
2016-08-29 16:52:35 +08:00
|
|
|
if (route.breadcrumbName) {
|
|
|
|
return (
|
|
|
|
<BreadcrumbItem separator={separator} key={route.breadcrumbName}>
|
|
|
|
{itemRender(route, params, routes, paths)}
|
|
|
|
</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
|
|
|
});
|
2016-10-24 12:04:26 +08:00
|
|
|
} else if (children) {
|
2016-07-14 13:29:50 +08:00
|
|
|
crumbs = React.Children.map(children, (element: any, index) => {
|
2017-02-23 13:43:50 +08:00
|
|
|
if (!element) {
|
|
|
|
return element;
|
|
|
|
}
|
2017-01-14 15:25:37 +08:00
|
|
|
warning(
|
2017-02-23 13:43:50 +08:00
|
|
|
element.type && element.type.__ANT_BREADCRUMB_ITEM,
|
2017-03-06 11:06:46 +08:00
|
|
|
'Breadcrumb only accepts Breadcrumb.Item as it\'s children'
|
2017-01-14 15:25:37 +08:00
|
|
|
);
|
2016-03-31 17:46:35 +08:00
|
|
|
return cloneElement(element, {
|
|
|
|
separator,
|
|
|
|
key: index,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return (
|
2016-12-30 21:41:28 +08:00
|
|
|
<div className={classNames(className, prefixCls)} style={style}>
|
2016-03-31 17:46:35 +08:00
|
|
|
{crumbs}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|