TreeSelect 树形选择器
下拉触发器 + 树面板组合的选择器:单选返回 string,多选(勾选框)返回 string[]。
基础用法
单选 / 多选
单选
当前值:(未选择)
多选
当前值:(空)
import { useState } from 'react'
import { LhTreeSelect } from '@lh-ui/react'
import type { LhTreeNode } from '@lh-ui/react'
const data: LhTreeNode[] = [
{
key: 'frontend',
label: '前端',
children: [
{ key: 'vue', label: 'Vue' },
{ key: 'react', label: 'React' },
],
},
{
key: 'backend',
label: '后端',
children: [{ key: 'node', label: 'Node' }],
},
]
export default function Demo() {
const [value, setValue] = useState<string>()
return (
<LhTreeSelect
data={data}
value={value}
placeholder="请选择技术栈"
onChange={(next) => setValue(next as string | undefined)}
/>
)
}
TreeSelect Props
| 属性 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| data | 树数据 | LhTreeNode[] | — | [] |
| value | 受控值(单选 string / 多选 string[]) | string | string[] | — | — |
| defaultValue | 非受控默认值 | string | string[] | — | — |
| multiple | 多选(显示勾选框,值为勾选键 数组) | boolean | — | false |
| disabled | 禁用 | boolean | — | false |
| placeholder | 占位符 | string | — | 语言包兜底 |
| teleported | 面板是否挂到 body(false 就地渲染) | boolean | — | true |
| sx | 快速布局属性 | SxProps | — | — |
LhTreeNode
| 属性 | 说明 | 类型 |
|---|---|---|
| key | 唯一键 | string |
| label | 显示文本 | string |
| children | 子节点 | LhTreeNode[] |
| disabled | 禁用该节点(禁用子树不参与级联勾选) | boolean |
| isLeaf | 叶子标记(不传按 children 推断) | boolean |
TreeSelect 事件
| 事件 | 说明 | 回调签名 | Vue 对照 |
|---|---|---|---|
| onChange | 选中变化(单选 string / 多选 string[];单选清空为 undefined) | (value: string | string[] | undefined) => void | update:modelValue / @change |
键盘交互
触发器为真实 <button>:aria-haspopup="tree" + aria-expanded 标注展开态,Enter / Space 走原生 button 激活。面板为 role="listbox"(多选加 aria-multiselectable),内部为 LhTree 的 APG tree pattern 键盘导航:上下键移动焦点,右键展开 / 进入子级,左键收起 / 回父级,Home / End 跳首尾,Enter 选中,Space 在多选下勾选。点击组件外部或按 Escape 关闭,关闭后焦点归还触发器。
交互约定:单选下非叶子节点首次点击只展开不选中(已展开的父节点再次点击才选中);多选下值只由勾选框驱动,面板保持展开连续勾选,父子勾选级联(禁用子树跳过)。
ref
forwardRef<HTMLDivElement> —— 根元素。LhTreeSelectInstance 暴露 open() / close() / toggle() / clear() / isOpen。
样式
import '@lh-ui/react/style/tree-select'