feat(breadcrumb): 支持 onNavigate 接管点击(SPA 路由)

- 可选 prop,不传时保持普通链接
- 文档契约 + props 表 + 变更记录同步
This commit is contained in:
stash
2026-10-29 11:26:20 +08:00
parent 52296b3368
commit 12e3047f1a
3 changed files with 26 additions and 6 deletions
+22 -4
View File
@@ -1,7 +1,12 @@
import type { ReactNode } from 'react'
import styles from './Breadcrumb.module.css'
/** 面包屑:详情页的路径导航,最后一项是当前页(不可点)。 */
/**
* 面包屑:详情页的路径导航,最后一项是当前页(不可点)。
*
* 传了 `onNavigate` 就接管中间项的点击(SPA 路由),
* 不传时保持普通链接行为(整页跳转)。
*/
export type BreadcrumbItem = {
label: ReactNode
href?: string
@@ -11,10 +16,12 @@ export type BreadcrumbProps = {
items: BreadcrumbItem[]
/** 分隔符,默认 `/` */
separator?: ReactNode
/** 点击中间项时回调(用于 SPA 路由);传了就会阻止默认跳转 */
onNavigate?: (href: string) => void
className?: string
}
export function Breadcrumb({ items, separator = '/', className }: BreadcrumbProps) {
export function Breadcrumb({ items, separator = '/', onNavigate, className }: BreadcrumbProps) {
return (
<nav className={[styles.root, className].filter(Boolean).join(' ')} aria-label="面包屑">
<ol className={styles.list}>
@@ -22,9 +29,20 @@ export function Breadcrumb({ items, separator = '/', className }: BreadcrumbProp
const last = index === items.length - 1
return (
<li key={`${index}-${typeof item.label === 'string' ? item.label : ''}`} className={styles.item}>
<li
key={`${index}-${typeof item.label === 'string' ? item.label : ''}`}
className={styles.item}
>
{item.href !== undefined && !last ? (
<a className={styles.link} href={item.href}>
<a
className={styles.link}
href={item.href}
onClick={(event) => {
if (onNavigate === undefined || item.href === undefined) return
event.preventDefault()
onNavigate(item.href)
}}
>
{item.label}
</a>
) : (