From f6da6ef38bcedeec16bc5f4e445b277ab7a80027 Mon Sep 17 00:00:00 2001 From: plandem Date: Fri, 10 Jun 2016 16:12:52 +0300 Subject: [PATCH 1/2] added nameFormatter for routes --- components/breadcrumb/Breadcrumb.jsx | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/components/breadcrumb/Breadcrumb.jsx b/components/breadcrumb/Breadcrumb.jsx index fe74ad494b..399a32748a 100644 --- a/components/breadcrumb/Breadcrumb.jsx +++ b/components/breadcrumb/Breadcrumb.jsx @@ -1,12 +1,24 @@ import React, { cloneElement } from 'react'; import BreadcrumbItem from './BreadcrumbItem'; +const defaultNameFormatter = (route, params) => { + if (!route.breadcrumbName) { + return null; + } + const name = route.breadcrumbName.replace(/:(.*)/g, (replacement, key) => { + return params[key] || replacement; + }); + + return name; +}; + export default class Breadcrumb extends React.Component { static defaultProps = { prefixCls: 'ant-breadcrumb', separator: '/', linkRender: (href, name) => {name}, nameRender: (name) => {name}, + nameFormatter: defaultNameFormatter, } static propTypes = { @@ -19,11 +31,12 @@ export default class Breadcrumb extends React.Component { params: React.PropTypes.object, linkRender: React.PropTypes.func, nameRender: React.PropTypes.func, + nameFormatter: React.PropTypes.func, } render() { let crumbs; - const { separator, prefixCls, routes, params, children, linkRender, nameRender } = this.props; + const { separator, prefixCls, routes, params, children, linkRender, nameRender, nameFormatter } = this.props; if (routes && routes.length > 0) { const paths = []; crumbs = routes.map((route, i) => { @@ -36,12 +49,10 @@ export default class Breadcrumb extends React.Component { paths.push(path); } - if (!route.breadcrumbName) { + const name = nameFormatter(route, params); + if (!name) { return null; } - const name = route.breadcrumbName.replace(/:(.*)/g, (replacement, key) => { - return params[key] || replacement; - }); let link; if (i === routes.length - 1) { From 9d4a6848c12178f4dccc46a49dd0f5688e6fb004 Mon Sep 17 00:00:00 2001 From: plandem Date: Fri, 10 Jun 2016 17:22:07 +0300 Subject: [PATCH 2/2] fixed case when linkRender must accept raw paths --- components/breadcrumb/Breadcrumb.jsx | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/components/breadcrumb/Breadcrumb.jsx b/components/breadcrumb/Breadcrumb.jsx index 399a32748a..c59582981a 100644 --- a/components/breadcrumb/Breadcrumb.jsx +++ b/components/breadcrumb/Breadcrumb.jsx @@ -16,7 +16,7 @@ export default class Breadcrumb extends React.Component { static defaultProps = { prefixCls: 'ant-breadcrumb', separator: '/', - linkRender: (href, name) => {name}, + linkRender: (paths, name) => {name}, nameRender: (name) => {name}, nameFormatter: defaultNameFormatter, } @@ -39,6 +39,7 @@ export default class Breadcrumb extends React.Component { const { separator, prefixCls, routes, params, children, linkRender, nameRender, nameFormatter } = this.props; if (routes && routes.length > 0) { const paths = []; + const lastPath = routes.length - 1; crumbs = routes.map((route, i) => { route.path = route.path || ''; let path = route.path.replace(/^\//, ''); @@ -50,17 +51,12 @@ export default class Breadcrumb extends React.Component { } const name = nameFormatter(route, params); - if (!name) { - return null; + if (name) { + const link = (i === lastPath) ? nameRender(name) : linkRender(paths, name); + return {link}; } - let link; - if (i === routes.length - 1) { - link = nameRender(name); - } else { - link = linkRender(`/${paths.join('/')}`, name); - } - return {link}; + return null; }); } else { crumbs = React.Children.map(children, (element, index) => {