IndexBar
通讯录式索引栏:右侧字母索引 + 分组锚点定位。scroll-spy 激活换算
(resolveActiveIndex)与触摸换算(hitIndexBar)全部消费
@lh-ui/share/core/index-bar 纯逻辑;本端为 DOM 测量 + scrollTo 胶水。
基础用法
索引栏(点击 / 触摸定位分组)
当前索引:—
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>
)
}
吸顶与自定义高亮
sticky 开启分组头吸顶(默认开),stickyOffsetTop 为导航栏占位偏移,
highlightColor 自定义激活索引高亮色:
<LhIndexBar indexList={['甲', '乙', '丙']} stickyOffsetTop={0} highlightColor="#c0392b">
<LhIndexBarGroup index="甲"><LhIndexBarItem>甲一</LhIndexBarItem></LhIndexBarGroup>
<LhIndexBarGroup index="乙"><LhIndexBarItem>乙一</LhIndexBarItem></LhIndexBarGroup>
<LhIndexBarGroup index="丙"><LhIndexBarItem>丙一</LhIndexBarItem></LhIndexBarGroup>
</LhIndexBar>
item 自定义渲染
itemSlot 替换默认索引项渲染(对应 Vue 端的 #item 插槽):
<LhIndexBar indexList={['A', 'B']} itemSlot={(index) => <b>项{index}</b>} />
IndexBar API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| indexList | 索引项数组(未传默认 A-Z 26 项;空数组渲染无侧栏) | string[] | A-Z |
| sticky | 锚点分组头吸顶 | boolean | true |
| stickyOffsetTop | 吸顶偏移 px(导航栏占位用) | number | 0 |
| highlightColor | 激活索引高亮色(未传取 indexBar.highlightColor 令牌) | string | — |
| sx | sx 快速布局属性 | LhSxProps | — |
| children | 分组内容(LhIndexBarGroup / LhIndexBarItem 对) | ReactNode | — |
| itemSlot | 自定义索引项渲染(对应 Vue 端 #item 插槽) | (index: string) => ReactNode | — |
| onSelect | 点击 / 触摸侧栏索引项 | (index: string) => void | — |
IndexBarGroup API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| index | 分组索引键(与 indexList 项对齐) | string | — |
| sx | sx 快速布局属性 | LhSxProps | — |
| children | 组内条目(LhIndexBarItem 对) | ReactNode | — |
IndexBarItem API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| disabled | 禁用(降低对比度) | boolean | false |
| sx | sx 快速布局属性 | LhSxProps | — |
| children | 条目内容 | ReactNode | — |
无障碍
根元素 role="navigation" + aria-label="索引栏"。