Merge branch 'plandem-master'

This commit is contained in:
afc163 2016-06-11 16:12:09 +08:00
commit 203b1ef388
2 changed files with 20 additions and 18 deletions

View File

@ -1,12 +1,22 @@
import React, { cloneElement } from 'react'; import React, { cloneElement } from 'react';
import BreadcrumbItem from './BreadcrumbItem'; import BreadcrumbItem from './BreadcrumbItem';
const defaultNameRender = (breadcrumbName, route, params) => {
if (!breadcrumbName) {
return null;
}
const name = breadcrumbName.replace(
/:(.*)/g, (replacement, key) => params[key] || replacement
);
return <span>{name}</span>;
};
export default class Breadcrumb extends React.Component { export default class Breadcrumb extends React.Component {
static defaultProps = { static defaultProps = {
prefixCls: 'ant-breadcrumb', prefixCls: 'ant-breadcrumb',
separator: '/', separator: '/',
linkRender: (href, name) => <a href={`#${href}`}>{name}</a>, linkRender: (paths, name) => <a href={`#/${paths.join('/')}`}>{name}</a>,
nameRender: (name) => <span>{name}</span>, nameRender: defaultNameRender,
} }
static propTypes = { static propTypes = {
@ -26,6 +36,7 @@ export default class Breadcrumb extends React.Component {
const { separator, prefixCls, routes, params, children, linkRender, nameRender } = this.props; const { separator, prefixCls, routes, params, children, linkRender, nameRender } = this.props;
if (routes && routes.length > 0) { if (routes && routes.length > 0) {
const paths = []; const paths = [];
const lastPath = routes.length - 1;
crumbs = routes.map((route, i) => { crumbs = routes.map((route, i) => {
route.path = route.path || ''; route.path = route.path || '';
let path = route.path.replace(/^\//, ''); let path = route.path.replace(/^\//, '');
@ -35,21 +46,12 @@ export default class Breadcrumb extends React.Component {
if (path) { if (path) {
paths.push(path); paths.push(path);
} }
const name = nameRender(route.breadcrumbName, route, params);
if (!route.breadcrumbName) { if (name) {
return null; const link = (i === lastPath) ? name : linkRender(paths, name);
return <BreadcrumbItem separator={separator} key={route.breadcrumbName || i}>{link}</BreadcrumbItem>;
} }
const name = route.breadcrumbName.replace(/:(.*)/g, (replacement, key) => { return null;
return params[key] || replacement;
});
let link;
if (i === routes.length - 1) {
link = nameRender(name);
} else {
link = linkRender(`/${paths.join('/')}`, name);
}
return <BreadcrumbItem separator={separator} key={name}>{link}</BreadcrumbItem>;
}); });
} else { } else {
crumbs = React.Children.map(children, (element, index) => { crumbs = React.Children.map(children, (element, index) => {

View File

@ -29,5 +29,5 @@ english: Breadcrumb
| routes | router 的路由栈信息 | Array | | - | | routes | router 的路由栈信息 | Array | | - |
| params | 路由的参数 | Object | | - | | params | 路由的参数 | Object | | - |
| separator | 分隔符自定义 | String or Element | | '/' | | separator | 分隔符自定义 | String or Element | | '/' |
| linkRender | 自定义链接函数,和 react-router 配置使用 | Function(href, name) | | - | | linkRender | 自定义链接函数,和 react-router 配置使用 | Function(href, breadcrumbName) | | - |
| nameRender | 自定义链接函数,和 react-router 配置使用 | Function(name) | | - | | nameRender | 自定义文字函数,和 react-router 配置使用 | Function(breadcrumbName, route, params) | | - |