import {ReactNode, PropsWithChildren} from 'react' import {cx, isUndefined} from '@befe/brick-utils' import {SvgPlainRight} from '@befe/brick-icon' import {Link, LinkProps} from '@befe/brick-comp-link' import {Icon} from '@befe/brick-comp-icon' import {useTheme} from '@befe/brick-core' import {Crumb} from './crumb' type PropsFromLink = Pick /** * @public */ export interface CrumbItem extends PropsFromLink { /** * 唯一标识 */ id?: string /** * 显示值 */ label?: ReactNode /** * 自定义渲染内容, * 提供时 `label`,`href`,`onClick`, `disabled` 等 link 属性将无效 */ content?: ReactNode } export interface BreadcrumbProps { /** * 自定义 class */ className?: string /** * 尺寸 * use config context `baseSize` as default */ size?: LinkProps['size'] /** * 面包屑节点数据 * 如用 children 自定义面包屑节点则会忽略 */ data?: CrumbItem[] /** * 节点分割符 */ divider?: ReactNode /** * 面包屑节点内容渲染器 * 在使用 data 定义面包屑节点时,用以自定义节点的内容,比如 ReactRouter Link 作为节点内容 */ renderCrumbItem?: (crumbItem: CrumbItem, crumbIndex: number) => ReactNode } function BreadcrumbDivider(props: Pick) { const {divider} = props const className = 'brick-breadcrumb-divider' return isUndefined(divider) ? : {divider} } interface CrumbNodeProps { item: CrumbItem index: number data?: CrumbItem[] divider?: BreadcrumbProps['divider'] size: LinkProps['size'] renderCrumbItem?: BreadcrumbProps['renderCrumbItem'] } function resolveCrumbContent(props: CrumbNodeProps): ReactNode { const {item, index, data, size, renderCrumbItem} = props const {label, href, disabled, content} = item // 自定义 if (typeof renderCrumbItem === 'function') { return renderCrumbItem(item, index) } if (!isUndefined(content)) { return content } // 非链接,最后一个认为是当前页面,认为是非链接 if (data && index === data.length - 1) { return {label} } return ( {label} ) } function CrumbNode(props: CrumbNodeProps) { const {index, divider} = props return ( <> {index ? : null} {resolveCrumbContent(props)} ) } export function Breadcrumb(props: PropsWithChildren) { const {className: customClassName, divider, data, renderCrumbItem, children} = props const {baseSize} = useTheme() const size = props.size ?? baseSize const className = cx( 'brick-breadcrumb', { [`brick-breadcrumb-size-${size}`]: size, }, customClassName ) return ( ) } Breadcrumb.displayName = 'Breadcrumb'