Listbox
A persistent single or multiple selection list with search, groups and virtual scrolling.
Import
ts
import { HuiListbox } from '@nscode/handy-ui'
import type { ListboxOption, ListboxProps, ListboxValue, ListboxExpose } from '@nscode/handy-ui'
Examples
Single selection and details
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: 'Administrator', description: 'Manage the workspace' },
{ value: 'editor', label: 'Editor', description: 'Edit content' },
{ value: 'viewer', label: 'Viewer', disabled: true }
]
const detail = computed(
() => options.find((option) => option.value === value.value)?.description ?? 'Choose a role'
)
</script>
<template>
<HuiSpace wrap align="start"
><HuiListbox v-model="value" :options="options" style="width: 260px" /><span
>{{ value }} · {{ detail }}</span
></HuiSpace
>
</template>
Multiple selection and visible select all
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('Waiting')
const options = [
{ value: 1, label: 'Read' },
{ value: 2, label: 'Write' },
{ value: 3, label: 'Approve', disabled: true }
]
</script>
<template>
<HuiSpace direction="vertical" fill
><HuiCheckbox v-model="showCheckbox">Show checkboxes</HuiCheckbox
><HuiListbox
v-model="value"
:options="options"
multiple
select-all
filterable
:show-checkbox="showCheckbox"
@change="event = 'Changed: ' + JSON.stringify($event)"
><template #header>Permissions</template
><template #footer>Selected: {{ value.length }}</template></HuiListbox
><span>{{ event }}</span></HuiSpace
>
</template>
Default search and custom filtering
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: 'Sales', description: 'Customer team' },
{ value: 'tech', label: 'Technology', description: 'Product team' }
]
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>
Groups and descriptions
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: 'Operations', group: 'Business', description: 'Review daily work' },
{ value: 2, label: 'Support', group: 'Business', description: 'Answer requests' },
{ value: 3, label: 'Development', group: 'Product', description: 'Deliver features' },
{ value: 4, label: 'Quality', group: 'Product', description: 'Verify releases' }
]
</script>
<template><HuiListbox v-model="value" :options="options" multiple :height="260" /></template>
Ten thousand virtual options
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: 'Option ' + value }))
</script>
<template>
<HuiSpace direction="vertical" fill
><HuiListbox v-model="value" :options="options" virtual filterable :height="240" /><span
>Selected: {{ value }}</span
></HuiSpace
>
</template>
Sizes, disabled state and borders
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: 'Alpha' },
{ value: 2, label: 'Bravo' },
{ value: 3, label: 'Charlie' }
]
</script>
<template>
<HuiSpace direction="vertical" fill
><HuiCheckbox v-model="disabled">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>
Option slot and empty states
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: 'Owner' },
{ value: 'member', label: 'Member' }
]
</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 ? 'Active' : 'Idle' }} · {{ disabled ? 'Disabled' : 'Available' }}</span
></template
></HuiListbox
><HuiSpace wrap
><HuiListbox
:options="[]"
empty-text="No choices yet"
:height="100"
style="width:220px"
/><HuiListbox :options="[]" empty-text="" :height="100" style="width:220px" /><HuiListbox
:options="[]"
:height="100"
style="width:220px"
><template #empty><HuiTag>Custom empty slot</HuiTag></template></HuiListbox
></HuiSpace
></HuiSpace
>
</template>
Readonly forms, validation and role permissions
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: 'Sales' },
{ value: 'tech', label: 'Technology' }
]
const permissions = [
{ value: 'read', label: 'Read' },
{ value: 'write', label: 'Write' },
{ value: 'approve', label: 'Approve' }
]
const rules = { roles: [{ required: true, message: 'Choose a role', trigger: 'blur' as const }] }
</script>
<template>
<HuiSpace direction="vertical" fill
><HuiSpace wrap
><HuiCheckbox v-model="readonly">Readonly form</HuiCheckbox
><HuiCheckbox v-model="validateEvent">Automatic validation</HuiCheckbox></HuiSpace
><HuiForm :model="model" :rules="rules" :readonly="readonly"
><HuiSpace wrap align="start"
><HuiFormItem label="Roles" prop="roles"
><HuiListbox
v-model="model.roles"
:options="roles"
multiple
:validate-event="validateEvent"
:height="160"
style="width:220px" /></HuiFormItem
><HuiFormItem label="Permissions" 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>
Controlled attribute presence and empty values
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiListbox, HuiSpace } from '@nscode/handy-ui'
const event = ref('Waiting')
const options = [
{ value: 1, label: 'Alpha' },
{ value: 2, label: 'Bravo' }
]
</script>
<template>
<HuiSpace direction="vertical" fill
><HuiSpace wrap
><HuiListbox
:options="options"
:height="120"
style="width:220px"
@change="event = 'Internal: ' + $event" /><HuiListbox
:options="options"
:model-value="undefined"
:height="120"
style="width:220px"
@update:model-value="event = 'Request refused: ' + $event" /></HuiSpace
><span>{{ event }}</span></HuiSpace
>
</template>
Methods, ARIA attributes and group focus events
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('Waiting')
const options = Array.from({ length: 100 }, (_, value) => ({ value, label: 'Option ' + 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="Business options"
aria-describedby="document-listbox-help"
@focus="event = 'Entered the component'"
@blur="event = 'Left the component'"
/><span id="document-listbox-help">{{ event }}</span></HuiSpace
>
</template>
Props
After changing the algorithm of filterMethod, change the query, replace the options source, or recreate the component with a new key.
| Name | Type | Default | Description |
|---|---|---|---|
| modelValue | ListboxValue | — | Single null / multiple []; controlled by attribute presence, internal state when omitted |
| multiple | boolean | false | Multiple selection |
| options | ListboxOption[] | [] | Option data |
| filterable | boolean | false | Show search input |
| filterMethod | (query: string, option: ListboxOption) => boolean | — | Synchronous predicate; skipped for an empty query |
| height | number | string | 280 | List viewport height, numbers in px; excludes toolbars and slots |
| virtual | boolean | false | Fixed-height virtualization |
| showCheckbox | boolean | true | Show decorative option checkboxes in multiple mode |
| selectAll | boolean | false | Show visible selectable options select-all |
| disabled | boolean | false | Disabled; inherits Form/InputGroup |
| size | ComponentSize | — | large/default/small; inherits InputGroup/Form/global size |
| emptyText | string | Locale | Empty text; explicit empty string preserved |
| border | boolean | true | Outer border |
| validateEvent | boolean | true | Automatic change and component-leave blur validation |
Events
| Name | Arguments | Description |
|---|---|---|
| update:modelValue | value: ListboxValue | Value update request; emitted before change |
| change | value: ListboxValue | User selection changed |
| focus | event: FocusEvent | Focus enters the whole component once |
| blur | event: FocusEvent | Focus leaves the whole component once; triggers blur validation |
Slots
| Name | Arguments | Description |
|---|---|---|
| option | ListboxOptionSlotProps | Option contents; keep virtual rows within the fixed height |
| header | — | Above search and selection tools |
| footer | — | Below the list |
| empty | — | Overrides emptyText |
Methods
| Name | Signature | Description |
|---|---|---|
| focus | () => void | Focus search input or list; ignored when disabled |
| blur | () => void | Blur the active element only if inside this component |
| scrollToOption | (value: ListboxOptionValue) => void | Scroll visible option into view without changing focus, highlight or selection |
Types
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
}
Keyboard
| Key | Behavior |
|---|---|
| ↑/↓ | Move highlight and select in single mode; highlight only in multiple mode. Skip disabled options and stop at boundaries. |
| Home/End | First/last option; reserved for the input when filterable=true. |
| PageUp/PageDown | Move one viewport based on row height; no wrapping. |
| Enter / Space | Select/toggle; Space in a search input remains text input. |
| Shift+↑/↓ | Add eligible options between the anchor and new highlight to the selection. |
| Ctrl+A / ⌘A | Select visible eligible options, then deselect them on the next press. Hidden/disabled selected values remain. |
| Text input | Without a search input, match prefixes within 500ms; repeated letters cycle matches. |