import classNames from 'classnames'; import pickAttrs from 'rc-util/lib/pickAttrs'; import * as React from 'react'; import type { BreadcrumbProps, InternalRouteType, ItemType } from './Breadcrumb'; type AddParameters any, TParameters extends [...args: any]> = ( ...args: [...Parameters, ...TParameters] ) => ReturnType; type ItemRender = NonNullable; type InternalItemRenderParams = AddParameters; function getBreadcrumbName(route: InternalRouteType, params: any) { if (route.title === undefined) { return null; } const paramsKeys = Object.keys(params).join('|'); return typeof route.title === 'object' ? route.title : String(route.title).replace( new RegExp(`:(${paramsKeys})`, 'g'), (replacement, key) => params[key] || replacement, ); } export function renderItem( prefixCls: string, item: ItemType, children: React.ReactNode, href?: string, ) { if (children === null || children === undefined) { return null; } const { className, onClick, ...restItem } = item; const passedProps = { ...pickAttrs(restItem, { data: true, aria: true, }), onClick, }; if (href !== undefined) { return ( {children} ); } return ( {children} ); } export default function useItemRender(prefixCls: string, itemRender?: ItemRender) { const mergedItemRender: InternalItemRenderParams = (item, params, routes, path, href) => { if (itemRender) { return itemRender(item, params, routes, path); } const name = getBreadcrumbName(item, params); return renderItem(prefixCls, item, name, href); }; return mergedItemRender; }