import React, { PureComponent, Fragment } from 'react'
import PropTypes from 'prop-types'
import { Breadcrumb } from 'antd'
import { Link, withRouter } from 'umi'
import { Icon as LegacyIcon } from '@ant-design/compatible'
const { pathToRegexp } = require("path-to-regexp")
import { queryAncestors } from 'utils'
import styles from './Bread.less'
@withRouter
class Bread extends PureComponent {
generateBreadcrumbs = paths => {
return paths.map((item, key) => {
const content = item && (
{item.icon && (
)}
{item.name}
)
return (
item && (
{paths.length - 1 !== key ? (
{content}
) : (
content
)}
)
)
})
}
render() {
const { routeList, location, i18n } = this.props
// Find a route that matches the pathname.
const currentRoute = routeList.find(
_ => _.route && pathToRegexp(_.route, location.pathname)
)
// Find the breadcrumb navigation of the current route match and all its ancestors.
const paths = currentRoute
? queryAncestors(routeList, currentRoute, 'breadcrumbParentId').reverse()
: [
routeList[0],
{
id: 404,
name: i18n.t`Not Found`,
},
]
return (
{this.generateBreadcrumbs(paths)}
)
}
}
Bread.propTypes = {
routeList: PropTypes.array,
}
export default Bread