Combobox
可筛选的组合框选择器。基于 Reka UI Combobox。可以单独用,也可以放进 FormField。有 FormField 时会合并 id / aria-* / data-*。选项由 items(useGroupItems)驱动,支持分组、标签、分隔线、描述与多选(multiple)。单选时锚点展示选中项标签;多选时展示各项图标、末项标签与数量。弹出层内含筛选输入框。
Installation
pnpm dlx @weme-ui/weme-ui add comboboxUsage
Vueexamples/usage.vueExpand codeHide code
<script lang="ts" setup>
import type { ComboboxItems } from '../combobox.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 Combobox from '../combobox.vue'
const schema = v.object({
drink: v.pipe(v.string('请选择饮品'), v.minLength(1, '请选择饮品')),
})
const defaultValues = {
drink: '',
}
const items: ComboboxItems = [
{ 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>
<Combobox
:model-value="field.state.value"
:items="items"
prefix-icon="ri:search-line"
placeholder="Search drinks…"
@blur="field.api.handleBlur"
@update:model-value="field.api.handleChange"
/>
</FormField>
<Button type="submit"> Submit </Button>
</Form>
</template>Examples
Variants
Soft
Outline
Vueexamples/variants.vueExpand codeHide code
<script lang="ts" setup>
import type { ComboboxItems } from '../combobox.props'
import Combobox from '../combobox.vue'
const items: ComboboxItems = [
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
]
</script>
<template>
<div class="flex flex-col gap-3 max-w-sm w-full">
<Combobox variant="soft" :items="items" placeholder="Soft" />
<Combobox variant="outline" :items="items" placeholder="Outline" />
</div>
</template>Sizes
Small
Medium
Large
Vueexamples/sizes.vueExpand codeHide code
<script lang="ts" setup>
import type { ComboboxItems } from '../combobox.props'
import Combobox from '../combobox.vue'
const items: ComboboxItems = [
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
]
</script>
<template>
<div class="flex flex-col gap-3 max-w-sm w-full">
<Combobox size="sm" :items="items" placeholder="Small" />
<Combobox size="md" :items="items" placeholder="Medium" />
<Combobox size="lg" :items="items" placeholder="Large" />
</div>
</template>With Affix
Search…
@handle
Vueexamples/with-affix.vueExpand codeHide code
<script lang="ts" setup>
import type { ComboboxItems } from '../combobox.props'
import Combobox from '../combobox.vue'
const items: ComboboxItems = [
{ label: 'Apple', value: 'apple', icon: 'ri:apple-line' },
{ label: 'Search', value: 'search', icon: 'ri:search-line' },
]
</script>
<template>
<div class="flex flex-col gap-3 max-w-sm w-full">
<Combobox
prefix-icon="ri:search-line"
suffix-icon="ri:sparkling-line"
:items="items"
placeholder="Search…"
/>
<Combobox prefix="@" suffix=".com" :items="items" placeholder="handle" />
</div>
</template>With Groups
Search food…
Vueexamples/with-groups.vueExpand codeHide code
<script lang="ts" setup>
import type { ComboboxItems } from '../combobox.props'
import Combobox from '../combobox.vue'
const items: ComboboxItems = [
[
{ 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">
<Combobox :items="items" placeholder="Search food…" />
</div>
</template>Multiple
Pick fruits…
Vueexamples/multiple.vueExpand codeHide code
<script lang="ts" setup>
import type { ComboboxItems } from '../combobox.props'
import Combobox from '../combobox.vue'
const items: ComboboxItems = [
{ 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">
<Combobox multiple :items="items" placeholder="Pick fruits…" />
</div>
</template>Loading
Loading…
Vueexamples/loading.vueExpand codeHide code
<script lang="ts" setup>
import type { ComboboxItems } from '../combobox.props'
import Combobox from '../combobox.vue'
const items: ComboboxItems = [
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
]
</script>
<template>
<div class="max-w-sm w-full">
<Combobox loading :items="items" placeholder="Loading…" />
</div>
</template>Disabled
Disabled
Vueexamples/disabled.vueExpand codeHide code
<script lang="ts" setup>
import type { ComboboxItems } from '../combobox.props'
import Combobox from '../combobox.vue'
const items: ComboboxItems = [
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
]
</script>
<template>
<div class="max-w-sm w-full">
<Combobox disabled :items="items" placeholder="Disabled" />
</div>
</template>Invalid
Default
Invalid
Vueexamples/invalid.vueExpand codeHide code
<script lang="ts" setup>
import type { ComboboxItems } from '../combobox.props'
import Combobox from '../combobox.vue'
const items: ComboboxItems = [
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
]
</script>
<template>
<div class="flex flex-col gap-3 max-w-sm w-full">
<Combobox :items="items" placeholder="Default" />
<Combobox invalid :items="items" placeholder="Invalid" />
</div>
</template>Unstyled
Fully custom…
Vueexamples/unstyled.vueExpand codeHide code
<script lang="ts" setup>
import type { ComboboxItems } from '../combobox.props'
import Combobox from '../combobox.vue'
const items: ComboboxItems = [
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
]
</script>
<template>
<div class="max-w-sm w-full">
<Combobox
unstyled
:items="items"
placeholder="Fully custom…"
class="text-sm py-1 outline-none border-b b-default w-full"
/>
</div>
</template>Props
PropTypeDefault
asany'div'
根节点渲染为的元素或组件(Reka UI
Primitive)any'div'asChildbooleanfalse
是否合并到子元素(Reka UI
Primitive)booleanfalsemodelValueComboboxValue | ComboboxValue[]-
受控选中值;
multiple 为 true 时为数组;可用 v-modelComboboxValue | ComboboxValue[]-
defaultValueComboboxValue | ComboboxValue[]-
非受控初始选中值
ComboboxValue | ComboboxValue[]-
multiplebooleanfalse
是否多选
booleanfalseopenboolean-
受控弹出层开关
boolean-defaultOpenboolean-
非受控初始打开状态
boolean-itemsComboboxItems-
选项列表;支持扁平或二维分组,以及
label / item / separatorComboboxItems-variant'soft' | 'outline''soft'
外观变体
'soft' | 'outline''soft'
size'sm' | 'md' | 'lg''md'
尺寸
'sm' | 'md' | 'lg''md'
prefixstring-
前缀文案(无
prefix 插槽、无选中项图标且无 prefixIcon 时展示)string-suffixstring-
后缀文案(无
suffix 插槽且无 suffixIcon / loading 时展示);位于触发器内string-prefixIconIconifyIconProps['icon']-
前缀图标(Iconify);单选且有选中项
selectedIcon / icon 时优先显示选中项图标IconifyIconProps['icon']-
suffixIconIconifyIconProps['icon']'ri:expand-up-down-line'
后缀 / 触发器图标(Iconify);
loading 时改显示 loadingIconIconifyIconProps['icon']'ri:expand-up-down-line'
loadingIconIconifyIconProps['icon']'ri:loader-line'
加载中图标(Iconify);显示在触发器位置
IconifyIconProps['icon']'ri:loader-line'
clearableboolean-
有值且未禁用时显示清除控件(
ComboboxCancel)boolean-clearIconIconifyIconProps['icon']'ri:close-line'
清除图标(Iconify)
IconifyIconProps['icon']'ri:close-line'
emptyTextstring-
无匹配项时的空状态文案
string-emptyIconIconifyIconProps['icon']-
空状态图标(Iconify)
IconifyIconProps['icon']-
itemIndicatorIconIconifyIconProps['icon']'ri:check-line'
选中项指示图标(Iconify)
IconifyIconProps['icon']'ri:check-line'
placeholderstring-
未选中时的占位文案;筛选输入框未设
filterPlaceholder 时也用它string-filterPlaceholderstring-
弹出层内筛选输入框的占位文案;未传时回退到
placeholderstring-referenceComboboxAnchorProps['reference']-
定位参考元素;透传至
ComboboxAnchorComboboxAnchorProps['reference']-
portalPropsPartial<ComboboxPortalProps>-
透传至
ComboboxPortalPartial<ComboboxPortalProps>-
contentPropsPartial<Omit<ComboboxContentProps, 'as' | 'asChild'>> & Partial<EmitsToProps<ComboboxContentEmits>>-
透传至
ComboboxContent(默认 align: 'start'、side: 'bottom'、position: 'popper' 等)Partial<Omit<ComboboxContentProps, 'as' | 'asChild'>> & Partial<EmitsToProps<ComboboxContentEmits>>-
openOnFocusbooleanfalse
筛选输入框聚焦时是否打开列表
booleanfalseopenOnClickbooleanfalse
点击筛选输入框时是否打开列表
booleanfalseresetSearchTermOnBlurbooleantrue
失焦时是否重置搜索词
booleantrueresetSearchTermOnSelectbooleantrue
选中后是否重置搜索词
booleantrueresetModelValueOnClearbooleanfalse
清除时是否将
modelValue 重置为 null(多选时为 [])booleanfalseignoreFilterboolean-
为
true 时禁用默认过滤boolean-highlightOnHoverbooleantrue
悬停时是否高亮选项
booleantrueloopboolean-
键盘导航是否在首尾循环
boolean-bystring | ((a: ComboboxValue, b: ComboboxValue) => boolean)-
比较选中值的字段名或自定义比较函数
string | ((a: ComboboxValue, b: ComboboxValue) => boolean)-
unmountOnHidebooleantrue
关闭时是否卸载弹出层内容;为
false 时仅用 CSS 隐藏booleantruedir'ltr' | 'rtl'-
阅读方向;未传时继承
ConfigProvider'ltr' | 'rtl'-
unstyledboolean-
移除样式变体类,仅保留结构
boolean-idstring-
控件
id;未传时回退到 FormField Contextstring-namestring-
控件
name;未传时回退到 FormField Contextstring-loadingboolean-
是否加载中;会与 FormField Context 的
loading 合并;为真时触发器显示加载图标boolean-requiredboolean-
是否必填;会与 FormField Context 的
required 合并boolean-disabledboolean-
是否禁用;会与 FormField Context 的
disabled 合并boolean-invalidboolean-
是否无效;会与 FormField Context 的
invalid 合并boolean-classany-
根节点额外 class
any-uiPartial<ComboboxStyleSlots>-
覆盖 slots 样式(
root / anchor / values / value / valueIcon / valueCount / placeholder / prefix / prefixIcon / suffix / suffixIcon / clearBtn / filterInput / content / viewport / empty / emptyIcon / group / label / item / itemIcon / itemContent / itemLabel / itemDescription / itemIndicator / separator)Partial<ComboboxStyleSlots>-
Events
EventType
update:modelValuevalue: ComboboxValue | ComboboxValue[]
选中值变更时触发(含
v-model);multiple 为 true 时值为数组value: ComboboxValue | ComboboxValue[]
changevalue: ComboboxValue | ComboboxValue[]
选中值变更时与
update:modelValue 一并触发(参数为值,非原生 Event)value: ComboboxValue | ComboboxValue[]
highlightpayload: { ref: HTMLElement, value: ComboboxValue | ComboboxValue[] } | undefined
高亮选项变更时触发
payload: { ref: HTMLElement, value: ComboboxValue | ComboboxValue[] } | undefined
focusevent: FocusEvent
筛选输入框获得焦点
event: FocusEvent
blurevent: FocusEvent
筛选输入框失去焦点
event: FocusEvent
Slots
SlotDescription
prefix自定义前缀;未传时回退到选中项图标 / prefixIcon / prefix
自定义前缀;未传时回退到选中项图标 /
prefixIcon / prefixsuffix自定义触发器内后缀;未传时回退到 loading / suffixIcon / suffix
自定义触发器内后缀;未传时回退到 loading /
suffixIcon / suffixempty无匹配项时的空状态;未传时回退到 emptyIcon + emptyText
无匹配项时的空状态;未传时回退到
emptyIcon + emptyTextlabel自定义分组标签(type: 'label');未传时回退到 item.label
自定义分组标签(
type: 'label');未传时回退到 item.labelitem自定义选项内容;作用域参数 { item, selected };未传时为 item-icon / 标签与描述 / 选中指示器
自定义选项内容;作用域参数
{ item, selected };未传时为 item-icon / 标签与描述 / 选中指示器item-icon自定义选项图标(嵌在默认 item 内);作用域参数 { item, selected };未传时按选中态显示 selectedIcon / icon
自定义选项图标(嵌在默认
item 内);作用域参数 { item, selected };未传时按选中态显示 selectedIcon / iconAccessibility
遵循 WAI-ARIA Combobox 设计模式。基于 Reka UI Combobox(ComboboxRoot / ComboboxInput / ComboboxContent / ComboboxItem 等)。通过 useFormFieldControl 合并 FormField Context 的 id、name、disabled、required、aria-labelledby、aria-describedby、aria-invalid 与字段 data-*(绑定在弹出层内筛选输入框)。根节点设置 data-slot="combobox",弹出层内容设置 data-slot="combobox-content"。
Keyboard Interactions
KeyDescription
ArrowDown / ArrowUp打开列表,或在选项间移动高亮
打开列表,或在选项间移动高亮
Enter选中当前高亮项
选中当前高亮项
Escape关闭列表
关闭列表
Files
src/components/combobox/combobox.vuesrc/components/combobox/combobox.props.tssrc/components/combobox/combobox.style.ts