Skip to content

Listbox

可筛选的列表选择器。基于 Reka UI Listbox。可以单独用,也可以放进 FormField。有 FormField 时会合并 id / aria-* / data-*。选项由 items(useGroupItems)驱动,支持分组、标签、分隔线、描述与多选(multiple)。顶部含筛选输入框;按 label / description(或 value)过滤,并保留分组结构。

Installation

pnpm dlx @weme-ui/weme-ui add listbox

Usage

*
Beer
Coffee
Juice
Wine
Vueexamples/usage.vueExpand codeHide code
<script lang="ts" setup>
import type { ListboxItems } from '../listbox.props'
import * as v from 'valibot'
import Button from '../../button/button.vue'
import FormField from '../../form-field/form-field.vue'
import Form from '../../form/form.vue'
import Listbox from '../listbox.vue'

const schema = v.object({
  drink: v.pipe(v.string('请选择饮品'), v.minLength(1, '请选择饮品')),
})

const defaultValues = {
  drink: '',
}

const items: ListboxItems = [
  { label: 'Beer', value: 'beer', icon: 'ri:beer-line' },
  { label: 'Coffee', value: 'coffee', icon: 'ri:drinks-line' },
  { label: 'Juice', value: 'juice', icon: 'ri:goblet-line' },
  { label: 'Wine', value: 'wine', icon: 'ri:goblet-2-line' },
]

function onFormSubmit(values: { drink: string }) {
  // eslint-disable-next-line no-alert
  alert(JSON.stringify(values))
}
</script>

<template>
  <Form
    class="max-w-sm w-full"
    :default-values="defaultValues"
    :schema="schema"
    @form-submit="onFormSubmit"
  >
    <FormField v-slot="{ field }" name="drink" label="Drink" required>
      <Listbox
        :model-value="field.state.value"
        :items="items"
        placeholder="Search drinks…"
        @update:model-value="field.api.handleChange"
      />
    </FormField>
    <Button type="submit"> Submit </Button>
  </Form>
</template>

Examples

Variants

Apple
Banana
Apple
Banana
Vueexamples/variants.vueExpand codeHide code
<script lang="ts" setup>
import type { ListboxItems } from '../listbox.props'
import Listbox from '../listbox.vue'

const items: ListboxItems = [
  { label: 'Apple', value: 'apple' },
  { label: 'Banana', value: 'banana' },
]
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <Listbox variant="soft" :items="items" placeholder="Soft" />

    <Listbox variant="outline" :items="items" placeholder="Outline" />
  </div>
</template>

Sizes

Apple
Banana
Apple
Banana
Apple
Banana
Vueexamples/sizes.vueExpand codeHide code
<script lang="ts" setup>
import type { ListboxItems } from '../listbox.props'
import Listbox from '../listbox.vue'

const items: ListboxItems = [
  { label: 'Apple', value: 'apple' },
  { label: 'Banana', value: 'banana' },
]
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <Listbox size="sm" :items="items" placeholder="Small" />

    <Listbox size="md" :items="items" placeholder="Medium" />

    <Listbox size="lg" :items="items" placeholder="Large" />
  </div>
</template>

With Groups

Fruits
Apple
Banana
Vegetables
Carrot
Broccoli
Vueexamples/with-groups.vueExpand codeHide code
<script lang="ts" setup>
import type { ListboxItems } from '../listbox.props'
import Listbox from '../listbox.vue'

const items: ListboxItems = [
  [
    { type: 'label', label: 'Fruits', value: 'fruits' },
    { label: 'Apple', value: 'apple', icon: 'ri:apple-line' },
    { label: 'Banana', value: 'banana' },
    { type: 'separator', value: 'sep-1' },
  ],
  [
    { type: 'label', label: 'Vegetables', value: 'vegetables' },
    { label: 'Carrot', value: 'carrot' },
    { label: 'Broccoli', value: 'broccoli' },
  ],
]
</script>

<template>
  <div class="max-w-sm w-full">
    <Listbox :items="items" placeholder="Search food…" />
  </div>
</template>

Multiple

Apple
Banana
Cherry
Durian
Vueexamples/multiple.vueExpand codeHide code
<script lang="ts" setup>
import type { ListboxItems } from '../listbox.props'
import Listbox from '../listbox.vue'

const items: ListboxItems = [
  { label: 'Apple', value: 'apple' },
  { label: 'Banana', value: 'banana' },
  { label: 'Cherry', value: 'cherry' },
  { label: 'Durian', value: 'durian' },
]
</script>

<template>
  <div class="max-w-sm w-full">
    <Listbox multiple :items="items" placeholder="Pick fruits…" />
  </div>
</template>

Horizontal

Beer
Coffee
Juice
Milk Tea
Wine

A type of alcoholic beverage made from fermented grapes

Vueexamples/horizontal.vueExpand codeHide code
<script lang="ts" setup>
import type { ListboxItems } from '../listbox.props'
import Listbox from '../listbox.vue'

const items: ListboxItems = [
  {
    label: 'Beer',
    value: 'beer',
    icon: 'ri:beer-line',
    selectedIcon: 'ri:beer-fill',
    iconClass: 'text-accent',
  },
  {
    label: 'Coffee',
    value: 'coffee',
    icon: 'ri:drinks-line',
    selectedIcon: 'ri:drinks-fill',
    iconClass: 'text-neutral',
  },
  {
    label: 'Juice',
    value: 'juice',
    icon: 'ri:goblet-line',
    selectedIcon: 'ri:goblet-fill',
    iconClass: 'text-info',
  },
  {
    label: 'Milk Tea',
    value: 'milk tea',
    icon: 'ri:drinks-2-line',
    selectedIcon: 'ri:drinks-2-fill',
    iconClass: 'text-warning',
  },
  {
    label: 'Wine',
    value: 'wine',
    description: 'A type of alcoholic beverage made from fermented grapes',
    icon: 'ri:goblet-2-line',
    selectedIcon: 'ri:goblet-2-fill',
    iconClass: 'text-error',
  },
]
</script>

<template>
  <div class="w-full">
    <Listbox orientation="horizontal" :items="items" placeholder="Search drinks…" />
  </div>
</template>

Disabled

Apple
Banana
Vueexamples/disabled.vueExpand codeHide code
<script lang="ts" setup>
import type { ListboxItems } from '../listbox.props'
import Listbox from '../listbox.vue'

const items: ListboxItems = [
  { label: 'Apple', value: 'apple' },
  { label: 'Banana', value: 'banana' },
]
</script>

<template>
  <div class="max-w-sm w-full">
    <Listbox disabled :items="items" placeholder="Disabled" />
  </div>
</template>

Invalid

Apple
Banana
Apple
Banana
Vueexamples/invalid.vueExpand codeHide code
<script lang="ts" setup>
import type { ListboxItems } from '../listbox.props'
import Listbox from '../listbox.vue'

const items: ListboxItems = [
  { label: 'Apple', value: 'apple' },
  { label: 'Banana', value: 'banana' },
]
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <Listbox :items="items" placeholder="Default" />
    <Listbox invalid :items="items" placeholder="Invalid" />
  </div>
</template>

Unstyled

Apple
Banana
Vueexamples/unstyled.vueExpand codeHide code
<script lang="ts" setup>
import type { ListboxItems } from '../listbox.props'
import Listbox from '../listbox.vue'

const items: ListboxItems = [
  { label: 'Apple', value: 'apple' },
  { label: 'Banana', value: 'banana' },
]
</script>

<template>
  <div class="max-w-sm w-full">
    <Listbox unstyled :items="items" placeholder="Fully custom…" class="text-sm w-full" />
  </div>
</template>

Props

PropTypeDefault
asany'div'
根节点渲染为的元素或组件(Reka UI Primitive)
any
'div'
asChildbooleanfalse
是否合并到子元素(Reka UI Primitive)
boolean
false
modelValueListboxValue | ListboxValue[]-
受控选中值;multiple 为 true 时为数组;可用 v-model
ListboxValue | ListboxValue[]
-
defaultValueListboxValue | ListboxValue[]-
非受控初始选中值
ListboxValue | ListboxValue[]
-
multiplebooleanfalse
是否多选
boolean
false
itemsListboxItems-
选项列表;支持扁平或二维分组,以及 label / item / separator
ListboxItems
-
filterstring''
筛选关键词;可用 v-model:filter;空字符串时不过滤
string
''
variant'soft' | 'outline''soft'
外观变体
'soft' | 'outline'
'soft'
size'sm' | 'md' | 'lg''md'
尺寸
'sm' | 'md' | 'lg'
'md'
placeholderstring-
筛选输入框占位文案
string
-
emptyTextstring'无数据'
空状态标题;传给默认 Empty 的 title
string
'无数据'
emptyIconIconifyIconProps['icon']'ri:inbox-2-line'
空状态图标(Iconify);传给默认 Empty 的 icon
IconifyIconProps['icon']
'ri:inbox-2-line'
itemIndicatorIconIconifyIconProps['icon']'ri:check-line'
选中项指示图标(Iconify)
IconifyIconProps['icon']
'ri:check-line'
orientation'vertical' | 'horizontal''vertical'
列表方向;影响方向键导航
'vertical' | 'horizontal'
'vertical'
selectionBehavior'toggle' | 'replace''toggle'
多选时的选择行为
'toggle' | 'replace'
'toggle'
highlightOnHoverboolean-
悬停时是否高亮选项
boolean
-
loopboolean-
键盘导航是否在首尾循环
boolean
-
bystring | ((a: ListboxValue, b: ListboxValue) => boolean)-
比较选中值的字段名或自定义比较函数
string | ((a: ListboxValue, b: ListboxValue) => boolean)
-
dir'ltr' | 'rtl'-
阅读方向;未传时继承 ConfigProvider
'ltr' | 'rtl'
-
unstyledboolean-
移除样式变体类,仅保留结构
boolean
-
idstring-
控件 id;未传时回退到 FormField Context
string
-
namestring-
控件 name;未传时回退到 FormField Context
string
-
requiredboolean-
是否必填;会与 FormField Context 的 required 合并
boolean
-
disabledboolean-
是否禁用;会与 FormField Context 的 disabled 合并
boolean
-
invalidboolean-
是否无效;会与 FormField Context 的 invalid 合并
boolean
-
classany-
根节点额外 class
any
-
uiPartial<ListboxStyleSlots>-
覆盖 slots 样式(root / filter / filterIcon / filterInput / content / group / groupLabel / item / itemIcon / itemContent / itemLabel / itemDescription / itemIndicator / separator / empty)
Partial<ListboxStyleSlots>
-

Events

EventType
update:modelValuevalue: ListboxValue | ListboxValue[]
选中值变更时触发(含 v-model);multiple 为 true 时值为数组
value: ListboxValue | ListboxValue[]
update:filtervalue: string
筛选关键词变更时触发(含 v-model:filter)
value: string
changevalue: ListboxValue | ListboxValue[]
选中值变更时与 update:modelValue 一并触发(参数为值,非原生 Event)
value: ListboxValue | ListboxValue[]
focusevent: FocusEvent
筛选输入框获得焦点
event: FocusEvent
blurevent: FocusEvent
筛选输入框失去焦点
event: FocusEvent

Slots

SlotDescription
empty无可见选项时的空状态(含筛选后无匹配);未传时渲染 Empty(emptyIcon + emptyText)
无可见选项时的空状态(含筛选后无匹配);未传时渲染 Empty(emptyIcon + emptyText)
group-label自定义分组标签(type: 'label');未传时回退到 item.label
自定义分组标签(type: 'label');未传时回退到 item.label
item自定义选项内容;作用域参数 { item, selected };未传时为 item-icon / item-content(选中指示器在插槽外)
自定义选项内容;作用域参数 { item, selected };未传时为 item-icon / item-content(选中指示器在插槽外)
item-icon自定义选项图标(嵌在默认 item 内);作用域参数 { item, selected };未传时按选中态显示 selectedIcon / icon
自定义选项图标(嵌在默认 item 内);作用域参数 { item, selected };未传时按选中态显示 selectedIcon / icon
item-content自定义选项文案区(嵌在默认 item 内);作用域参数 { item, selected };未传时渲染 label / description
自定义选项文案区(嵌在默认 item 内);作用域参数 { item, selected };未传时渲染 label / description

Accessibility

遵循 WAI-ARIA Listbox 设计模式。基于 Reka UI Listbox(ListboxRoot / ListboxFilter / ListboxContent / ListboxItem 等)。通过 useFormFieldControl 合并 FormField Context 的 id、name、disabled、required、aria-labelledby、aria-describedby、aria-invalid 与字段 data-*(绑定在筛选输入框;筛选容器同步字段 data-*)。根节点设置 data-slot="listbox"。列表 role="listbox"(aria-orientation / aria-multiselectable),选项 role="option"(aria-selected),并以 data-state / data-highlighted 反映选中与高亮;筛选框带 aria-activedescendant。

Keyboard Interactions

KeyDescription
ArrowDown / ArrowUp在选项间移动高亮(筛选框内;竖向 orientation)
在选项间移动高亮(筛选框内;竖向 orientation)
Home / End将高亮移到首个 / 末个选项
将高亮移到首个 / 末个选项
Enter选中当前高亮项
选中当前高亮项

Files

  • src/components/listbox/listbox.vue
  • src/components/listbox/listbox.props.ts
  • src/components/listbox/listbox.style.ts