Aller au contenu principal

IndexBar

Barre d'index style contacts : index alphabétique latéral + groupes ancrés. L'activation scroll-spy (resolveActiveIndex) et la détection tactile (hitIndexBar) sont des fonctions pures consommées depuis @lh-ui/share/core/index-bar.

Utilisation de base​

Barre d'index (clic / tactile pour aller au groupe)

Index actuel : —

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>
)
}

En-tête collé et surbrillance personnalisée​

sticky fixe l'en-tête de groupe (activé par défaut), stickyOffsetTop réserve l'espace d'une barre de navigation et highlightColor survole la couleur active :

<LhIndexBar indexList={['甲', '乙', '丙']} stickyOffsetTop={0} highlightColor="#c0392b">
<LhIndexBarGroup index="甲"><LhIndexBarItem>甲一</LhIndexBarItem></LhIndexBarGroup>
<LhIndexBarGroup index="乙"><LhIndexBarItem>乙一</LhIndexBarItem></LhIndexBarGroup>
<LhIndexBarGroup index="丙"><LhIndexBarItem>丙一</LhIndexBarItem></LhIndexBarGroup>
</LhIndexBar>

Rendu personnalisé des items​

itemSlot remplace l'élément d'index par défaut (côté Vue : slot #item) :

<LhIndexBar indexList={['A', 'B']} itemSlot={(index) => <b>项{index}</b>} />

IndexBar API​

Propriétés​

PropriétésDescriptionTypePar défaut
indexListstring[]string[]A-Z
stickybooleanbooleantrue
stickyOffsetTopnumbernumber0
highlightColorstringstring—
sxLhSxPropsLhSxProps—
childrenReactNodeReactNode—
itemSlot(index) => ReactNode(index: string) => ReactNode—
onSelect(index) => void(index: string) => void—

IndexBarGroup​

PropriétésDescriptionType
indexClé de groupe (alignée avec indexList)string
sxLhSxPropsLhSxProps
childrenLhIndexBarItemReactNode

IndexBarItem​

PropriétésDescriptionTypePar défaut
disabledDésactivé (contraste réduit)booleanfalse
sxLhSxPropsLhSxProps—
childrenReactNodeReactNode—

Accessibilité​

  • L'élément racine porte role="navigation" + aria-label="Barre d'index".