跳到主要内容

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锚点分组头吸顶booleantrue
stickyOffsetTop吸顶偏移 px(导航栏占位用)number0
highlightColor激活索引高亮色(未传取 indexBar.highlightColor 令牌)string—
sxsx 快速布局属性LhSxProps—
children分组内容(LhIndexBarGroup / LhIndexBarItem 对)ReactNode—
itemSlot自定义索引项渲染(对应 Vue 端 #item 插槽)(index: string) => ReactNode—
onSelect点击 / 触摸侧栏索引项(index: string) => void—

IndexBarGroup API​

属性说明类型默认值
index分组索引键(与 indexList 项对齐)string—
sxsx 快速布局属性LhSxProps—
children组内条目(LhIndexBarItem 对)ReactNode—

IndexBarItem API​

属性说明类型默认值
disabled禁用(降低对比度)booleanfalse
sxsx 快速布局属性LhSxProps—
children条目内容ReactNode—

无障碍​

根元素 role="navigation" + aria-label="索引栏"。