import { inject, cloneVNode } from 'vue'; import PropTypes from '../_util/vue-types'; import { filterEmpty, getComponent, getSlot } from '../_util/props-util'; import warning from '../_util/warning'; import { defaultConfigProvider } from '../config-provider'; import BreadcrumbItem from './BreadcrumbItem'; import Menu from '../menu'; const Route = PropTypes.shape({ path: PropTypes.string, breadcrumbName: PropTypes.string, children: PropTypes.array, }).loose; const BreadcrumbProps = { prefixCls: PropTypes.string, routes: PropTypes.arrayOf(Route), params: PropTypes.any, separator: PropTypes.any, itemRender: PropTypes.func, }; function getBreadcrumbName(route, params) { if (!route.breadcrumbName) { return null; } const paramsKeys = Object.keys(params).join('|'); const name = route.breadcrumbName.replace( new RegExp(`:(${paramsKeys})`, 'g'), (replacement, key) => params[key] || replacement, ); return name; } export default { name: 'ABreadcrumb', props: BreadcrumbProps, setup() { return { configProvider: inject('configProvider', defaultConfigProvider), }; }, methods: { defaultItemRender({ route, params, routes, paths }) { const isLastItem = routes.indexOf(route) === routes.length - 1; const name = getBreadcrumbName(route, params); return isLastItem ? {name} : {name}; }, getPath(path, params) { path = (path || '').replace(/^\//, ''); Object.keys(params).forEach(key => { path = path.replace(`:${key}`, params[key]); }); return path; }, addChildPath(paths, childPath, params) { const originalPaths = [...paths]; const path = this.getPath(childPath, params); if (path) { originalPaths.push(path); } return originalPaths; }, genForRoutes({ routes = [], params = {}, separator, itemRender = this.defaultItemRender }) { const paths = []; return routes.map(route => { const path = this.getPath(route.path, params); if (path) { paths.push(path); } const tempPaths = [...paths]; // generated overlay by route.children let overlay = null; if (route.children && route.children.length) { overlay = (
); } return (