IndexBar
Barra de índice estilo contactos: índice alfabético lateral + grupos ancla. La activación scroll-spy (resolveActiveIndex) y la detección táctil (hitIndexBar) son funciones puras consumidas de @lh-ui/share/core/index-bar.
Uso básico
Barra de índice (clic / toque para ir al grupo)
Índice actual: —
import { useState } from 'react'
import { LhIndexBar, LhIndexBarGroup, LhIndexBarItem } from '@lh-ui/react'
export default function Demo() {
const [current, setCurrent] = useState('')
const groups: Record<string, string[]> = {
A: ['阿白', '阿黄'],
B: ['柏舟', '薄荷'],
C: ['春日', '椿'],
}
return (
<LhIndexBar indexList={['A', 'B', 'C']} onSelect={setCurrent}>
{(['A', 'B', 'C'] as const).map((key) => (
<LhIndexBarGroup key={key} index={key}>
{groups[key].map((name) => (
<LhIndexBarItem key={name}>{name}</LhIndexBarItem>
))}
</LhIndexBarGroup>
))}
</LhIndexBar>
)
}
Encabezado fijo y resaltado personalizado
sticky fija el encabezado de grupo (activado por defecto), stickyOffsetTop reserva espacio para la barra de navegación y highlightColor anula el color activo:
<LhIndexBar indexList={['甲', '乙', '丙']} stickyOffsetTop={0} highlightColor="#c0392b">
<LhIndexBarGroup index="甲"><LhIndexBarItem>甲一</LhIndexBarItem></LhIndexBarGroup>
<LhIndexBarGroup index="乙"><LhIndexBarItem>乙一</LhIndexBarItem></LhIndexBarGroup>
<LhIndexBarGroup index="丙"><LhIndexBarItem>丙一</LhIndexBarItem></LhIndexBarGroup>
</LhIndexBar>
Renderización personalizada de item
itemSlot reemplaza el elemento de índice por defecto (Vue: slot #item):
<LhIndexBar indexList={['A', 'B']} itemSlot={(index) => <b>项{index}</b>} />
IndexBar API
Propiedades
| Propiedades | Descripción | Tipo | Por defecto |
|---|---|---|---|
| indexList | string[] | string[] | A-Z |
| sticky | boolean | boolean | true |
| stickyOffsetTop | number | number | 0 |
| highlightColor | string | string | — |
| sx | LhSxProps | LhSxProps | — |
| children | ReactNode | ReactNode | — |
| itemSlot | (index) => ReactNode | (index: string) => ReactNode | — |
| onSelect | (index) => void | (index: string) => void | — |
IndexBarGroup
| Propiedades | Descripción | Tipo |
|---|---|---|
| index | Clave de grupo (alineada con indexList) | string |
| sx | LhSxProps | LhSxProps |
| children | LhIndexBarItem | ReactNode |
IndexBarItem
| Propiedades | Descripción | Tipo | Por defecto |
|---|---|---|---|
| disabled | Desactivado (contraste reducido) | boolean | false |
| sx | LhSxProps | LhSxProps | — |
| children | ReactNode | ReactNode | — |
Accesibilidad
- El elemento raíz lleva role="navigation" + aria-label="Barra de índice".