Listbox 列表选择
不弹出的单选或多选列表,支持搜索、分组和虚拟滚动。
引入
ts
import { HuiListbox } from '@nscode/handy-ui'
import type { ListboxOption, ListboxProps, ListboxValue, ListboxExpose } from '@nscode/handy-ui'
示例
单选与详情联动
vue
<script setup lang="ts">
import { computed, ref } from 'vue'
import { HuiListbox, HuiSpace } from '@nscode/handy-ui'
const value = ref<string | number | null>(null)
const options = [
{ value: 'admin', label: '管理员', description: '管理工作区' },
{ value: 'editor', label: '编辑者', description: '编辑内容' },
{ value: 'viewer', label: '观察员', disabled: true }
]
const detail = computed(
() => options.find((option) => option.value === value.value)?.description ?? '请选择角色'
)
</script>
<template>
<HuiSpace wrap align="start"
><HuiListbox v-model="value" :options="options" style="width: 260px" /><span
>{{ value }} · {{ detail }}</span
></HuiSpace
>
</template>
多选、可见全选与复选框
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiListbox, HuiCheckbox, HuiSpace } from '@nscode/handy-ui'
const value = ref<(string | number)[]>([])
const showCheckbox = ref(true)
const event = ref('等待操作')
const options = [
{ value: 1, label: '读取' },
{ value: 2, label: '写入' },
{ value: 3, label: '审批', disabled: true }
]
</script>
<template>
<HuiSpace direction="vertical" fill
><HuiCheckbox v-model="showCheckbox">显示复选框</HuiCheckbox
><HuiListbox
v-model="value"
:options="options"
multiple
select-all
filterable
:show-checkbox="showCheckbox"
@change="event = '已修改:' + JSON.stringify($event)"
><template #header>权限</template
><template #footer>已选:{{ value.length }}</template></HuiListbox
><span>{{ event }}</span></HuiSpace
>
</template>
默认搜索与自定义过滤
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiListbox, HuiSpace } from '@nscode/handy-ui'
import type { ListboxOption } from '@nscode/handy-ui'
const normal = ref<string | number | null>(null)
const custom = ref<string | number | null>(null)
const options = [
{ value: 'sales', label: '销售', description: '客户团队' },
{ value: 'tech', label: '技术', description: '产品团队' }
]
const filter = (query: string, option: ListboxOption) =>
[option.label, option.description, String(option.value)].some((value) =>
value?.toLowerCase().includes(query.toLowerCase())
)
</script>
<template>
<HuiSpace wrap align="start"
><HuiListbox
v-model="normal"
:options="options"
filterable
:height="160"
style="width:240px" /><HuiListbox
v-model="custom"
:options="options"
filterable
:filter-method="filter"
height="12rem"
style="width:240px"
/></HuiSpace>
</template>
分组与说明文字
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiListbox } from '@nscode/handy-ui'
const value = ref<(string | number)[]>([])
const options = [
{ value: 1, label: '运营', group: '业务', description: '检查日常工作' },
{ value: 2, label: '支持', group: '业务', description: '处理请求' },
{ value: 3, label: '研发', group: '产品', description: '交付功能' },
{ value: 4, label: '质量', group: '产品', description: '验证发布' }
]
</script>
<template><HuiListbox v-model="value" :options="options" multiple :height="260" /></template>
一万项虚拟滚动
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiListbox, HuiSpace } from '@nscode/handy-ui'
const value = ref<string | number | null>(null)
const options = Array.from({ length: 10000 }, (_, value) => ({ value, label: '选项 ' + value }))
</script>
<template>
<HuiSpace direction="vertical" fill
><HuiListbox v-model="value" :options="options" virtual filterable :height="240" /><span
>已选:{{ value }}</span
></HuiSpace
>
</template>
三档尺寸、禁用和边框
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiListbox, HuiCheckbox, HuiSpace } from '@nscode/handy-ui'
const disabled = ref(false)
const options = [
{ value: 1, label: '甲项目' },
{ value: 2, label: '乙项目' },
{ value: 3, label: '丙项目' }
]
</script>
<template>
<HuiSpace direction="vertical" fill
><HuiCheckbox v-model="disabled">禁用</HuiCheckbox
><HuiSpace wrap align="start"
><HuiListbox
:options="options"
size="large"
:disabled="disabled"
:height="160"
style="width:200px" /><HuiListbox
:options="options"
size="default"
:disabled="disabled"
:height="160"
style="width:200px" /><HuiListbox
:options="options"
size="small"
:disabled="disabled"
:border="false"
:height="160"
style="width:200px" /></HuiSpace
></HuiSpace>
</template>
选项插槽与两种空态
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiListbox, HuiSpace, HuiTag } from '@nscode/handy-ui'
const value = ref<string | number | null>(null)
const options = [
{ value: 'owner', label: '所有者' },
{ value: 'member', label: '成员' }
]
</script>
<template>
<HuiSpace direction="vertical" fill
><HuiListbox v-model="value" :options="options" :height="120"
><template #option="{ option, selected, active, disabled }"
><HuiTag :type="selected ? 'success' : 'info'">{{ option.label }}</HuiTag
><span>
· {{ active ? '当前' : '非当前' }} · {{ disabled ? '禁用' : '可用' }}</span
></template
></HuiListbox
><HuiSpace wrap
><HuiListbox
:options="[]"
empty-text="暂无选项"
:height="100"
style="width:220px"
/><HuiListbox :options="[]" empty-text="" :height="100" style="width:220px" /><HuiListbox
:options="[]"
:height="100"
style="width:220px"
><template #empty><HuiTag>自定义空态</HuiTag></template></HuiListbox
></HuiSpace
></HuiSpace
>
</template>
表单只读、自动校验与角色权限布局
vue
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { HuiListbox, HuiForm, HuiFormItem, HuiCheckbox, HuiSpace } from '@nscode/handy-ui'
const model = reactive({
roles: [] as (string | number)[],
permissions: [] as (string | number)[]
})
const readonly = ref(false)
const validateEvent = ref(true)
const roles = [
{ value: 'sales', label: '销售' },
{ value: 'tech', label: '技术' }
]
const permissions = [
{ value: 'read', label: '读取' },
{ value: 'write', label: '写入' },
{ value: 'approve', label: '审批' }
]
const rules = { roles: [{ required: true, message: '请选择角色', trigger: 'blur' as const }] }
</script>
<template>
<HuiSpace direction="vertical" fill
><HuiSpace wrap
><HuiCheckbox v-model="readonly">表单只读</HuiCheckbox
><HuiCheckbox v-model="validateEvent">自动校验</HuiCheckbox></HuiSpace
><HuiForm :model="model" :rules="rules" :readonly="readonly"
><HuiSpace wrap align="start"
><HuiFormItem label="角色" prop="roles"
><HuiListbox
v-model="model.roles"
:options="roles"
multiple
:validate-event="validateEvent"
:height="160"
style="width:220px" /></HuiFormItem
><HuiFormItem label="权限" prop="permissions" :readonly="false"
><HuiListbox
v-model="model.permissions"
:options="permissions"
multiple
select-all
:height="160"
style="width:220px" /></HuiFormItem></HuiSpace></HuiForm
><span>{{ JSON.stringify(model) }}</span></HuiSpace
>
</template>
属性存在即受控与显式空值
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiListbox, HuiSpace } from '@nscode/handy-ui'
const event = ref('等待操作')
const options = [
{ value: 1, label: '甲项目' },
{ value: 2, label: '乙项目' }
]
</script>
<template>
<HuiSpace direction="vertical" fill
><HuiSpace wrap
><HuiListbox
:options="options"
:height="120"
style="width:220px"
@change="event = '内部值:' + $event" /><HuiListbox
:options="options"
:model-value="undefined"
:height="120"
style="width:220px"
@update:model-value="event = '拒绝请求:' + $event" /></HuiSpace
><span>{{ event }}</span></HuiSpace
>
</template>
方法、ARIA透传与整组焦点事件
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiListbox, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { ListboxExpose } from '@nscode/handy-ui'
const box = ref<ListboxExpose>()
const event = ref('等待操作')
const options = Array.from({ length: 100 }, (_, value) => ({ value, label: '选项 ' + value }))
</script>
<template>
<HuiSpace direction="vertical" fill
><HuiSpace wrap
><HuiButton @mousedown.prevent @click="box?.focus()">focus()</HuiButton
><HuiButton @mousedown.prevent @click="box?.blur()">blur()</HuiButton
><HuiButton @mousedown.prevent @click="box?.scrollToOption(90)"
>scrollToOption(90)</HuiButton
></HuiSpace
><HuiListbox
ref="box"
id="document-listbox"
:options="options"
virtual
filterable
:height="160"
aria-label="业务选项"
aria-describedby="document-listbox-help"
@focus="event = '进入整个组件'"
@blur="event = '离开整个组件'"
/><span id="document-listbox-help">{{ event }}</span></HuiSpace
>
</template>
Props
filterMethod 函数换了算法时,改变查询、替换选项来源或换 key 重建组件。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| modelValue | ListboxValue | — | 单选null、多选[];属性存在即受控,未传时内部维护 |
| multiple | boolean | false | 多选 |
| options | ListboxOption[] | [] | 选项数据 |
| filterable | boolean | false | 搜索框 |
| filterMethod | (query: string, option: ListboxOption) => boolean | — | 同步过滤;空查询不执行 |
| height | number | string | 280 | 列表区域高度,数字px;不含工具栏和插槽 |
| virtual | boolean | false | 固定行高虚拟滚动 |
| showCheckbox | boolean | true | 多选项显示复选框 |
| selectAll | boolean | false | 多选可见全选入口 |
| disabled | boolean | false | 禁用,继承表单/输入组 |
| size | ComponentSize | — | large/default/small,继承输入组/表单/全局 |
| emptyText | string | 语言包 | 空状态文字,空串保留 |
| border | boolean | true | 外边框 |
| validateEvent | boolean | true | change/整组blur自动表单校验 |
Events
| 事件 | 参数 | 说明 |
|---|---|---|
| update:modelValue | value: ListboxValue | 值更新请求,先于change |
| change | value: ListboxValue | 用户选择值变化 |
| focus | event: FocusEvent | 焦点从外部进入整个组件,仅一次 |
| blur | event: FocusEvent | 焦点离开整个组件,仅一次,并触发blur校验 |
Slots
| 插槽 | 参数 | 说明 |
|---|---|---|
| option | ListboxOptionSlotProps | 自定义选项内容;虚拟时不得超出固定行高 |
| header | — | 搜索/全选上方 |
| footer | — | 列表下方 |
| empty | — | 优先于emptyText |
方法
| 方法 | 签名 | 说明 |
|---|---|---|
| focus | () => void | 聚焦搜索框或列表;禁用时不生效 |
| blur | () => void | 仅让组件内的活动元素失焦 |
| scrollToOption | (value: ListboxOptionValue) => void | 滚到可见选项,不改焦点/当前项/选中值;被过滤或不存在则忽略 |
类型
ts
export type ListboxOptionValue = string | number
export type ListboxValue = ListboxOptionValue | ListboxOptionValue[] | null
export interface ListboxOption {
label: string
value: ListboxOptionValue
disabled?: boolean
description?: string
group?: string
}
export interface ListboxOptionSlotProps {
option: ListboxOption
selected: boolean
active: boolean
disabled: boolean
}
export interface ListboxExpose {
focus: () => void
blur: () => void
scrollToOption: (value: ListboxOptionValue) => void
}
键盘
| 按键 | 行为 |
|---|---|
| ↑/↓ | 移动当前项并在单选时选中;多选仅移动;跳过禁用,到头停住 |
| Home/End | 首/末项;filterable=true时留给搜索输入框 |
| PageUp/PageDown | 按列表高度和行高跳一屏,不循环 |
| Enter / Space | 选择或多选切换;搜索框内Space保持文字输入 |
| Shift+↑/↓ | 多选时将锚点与新当前项之间可选项追加到选中集合 |
| Ctrl+A / ⌘A | 多选可见可选项全选,再按取消这些项;保留隐藏/禁用已选值 |
| 文字输入 | 无搜索框时500ms词首匹配;连按同字母轮换 |