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