Combobox
通过输入框筛选分组选项,键盘焦点保持在输入区域。
适用场景
- 当界面需要以下行为时使用 Combobox:通过输入框筛选分组选项,键盘焦点保持在输入区域。
安装
选择安装方式,用法示例将同步更新导入路径。
$ bunx --bun shadcn-vue@2.8.2 add https://orvui.efu.me/r/combobox.json
首次使用时,将安装的 src/components/orvui/styles.css 导入项目样式入口。
示例
预览
Workspace
packages/vue/src/motion/combobox/demo.vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { Blocks, Box, Component, Layers3 } from "lucide-vue-next";
import {
Combobox,
ComboboxTrigger,
ComboboxInput,
ComboboxContent,
ComboboxList,
ComboboxEmpty,
ComboboxGroup,
ComboboxSeparator,
ComboboxLabel,
ComboboxItem,
} from "@/components/orvui/motion/combobox";
const selected = ref("studio");
const workspaces = [
{
value: "studio",
label: "Design studio",
detail: "12 projects",
group: "Recent",
icon: Component,
color: "bg-amber-400/20 text-amber-700 dark:text-amber-300",
},
{
value: "product",
label: "Product team",
detail: "8 projects",
group: "Recent",
icon: Layers3,
color: "bg-sky-400/20 text-sky-700 dark:text-sky-300",
},
{
value: "playground",
label: "Playground",
detail: "24 experiments",
group: "Workspaces",
icon: Blocks,
color: "bg-emerald-400/20 text-emerald-700 dark:text-emerald-300",
},
{
value: "archive",
label: "Component archive",
detail: "41 components",
group: "Workspaces",
icon: Box,
color: "bg-rose-400/20 text-rose-700 dark:text-rose-300",
},
];
const groups = computed(() =>
["Recent", "Workspaces"].map((label) => ({
label,
items: workspaces.filter((item) => item.group === label),
})),
);
</script>
<template>
<div class="w-full max-w-72">
<p class="mb-2 text-xs font-medium text-muted-foreground">Workspace</p>
<Combobox v-model="selected"
><ComboboxTrigger class="h-12 rounded-2xl px-2.5"
><ComboboxInput
aria-label="Search workspaces"
placeholder="Search workspaces…" /></ComboboxTrigger
><ComboboxContent class="w-72 rounded-2xl"
><ComboboxList aria-label="Workspaces" class="p-2"
><ComboboxEmpty>No workspaces found.</ComboboxEmpty
><ComboboxGroup v-for="(group, index) in groups" :key="group.label"
><ComboboxSeparator v-if="index" /><ComboboxLabel>{{
group.label
}}</ComboboxLabel
><ComboboxItem
v-for="workspace in group.items"
:key="workspace.value"
:value="workspace.value"
:text-value="workspace.label"
:keywords="[workspace.detail, workspace.group]"
class="py-2"
><span class="flex min-w-0 items-center gap-2.5"
><span
:class="[
'grid size-7 shrink-0 place-items-center rounded-lg',
workspace.color,
]"
><component :is="workspace.icon" class="size-3.5" /></span
><span class="min-w-0"
><span class="block truncate font-medium text-foreground">{{
workspace.label
}}</span
><span class="block truncate text-xs text-muted-foreground">{{
workspace.detail
}}</span></span
></span
></ComboboxItem
></ComboboxGroup
></ComboboxList
></ComboboxContent
></Combobox
>
</div>
</template>
packages/vue/src/motion/combobox/src/Combobox.vue
组合结构
使用公开部件 Combobox, ComboboxTrigger, ComboboxInput, ComboboxContent, ComboboxList, ComboboxEmpty, ComboboxGroup, ComboboxSeparator, ComboboxLabel, ComboboxItem 组合 Combobox,并从该组件的公开入口导入。
Combobox
├── ComboboxTrigger
│ └── ComboboxInput
└── ComboboxContent
└── ComboboxList
├── ComboboxEmpty
├── ComboboxGroup
│ ├── ComboboxLabel
│ └── ComboboxItem
└── ComboboxSeparator工作方式
通过输入框筛选分组选项,键盘焦点保持在输入区域。
状态与事件
需要由表单、路由或父组件管理时,绑定下列状态并接收对应更新。v-model 是属性与 update 事件的简写;值的类型应与 API 一致。
| 组件 | Vue 绑定 | 更新事件 |
|---|---|---|
Combobox | v-modelstring | update:modelValue |
Combobox | v-model:openboolean | update:open |
Combobox | v-model:querystring | update:query |
可选受控属性未传入时,按组件文档的默认行为工作;不要用固定的 false 或空值代替未传入。异步请求和持久化在应用中处理,再把结果传回组件。
API 参考
Combobox
modelValue?string—defaultValue?string(multiple ? [] : undefined)open?booleanundefineddefaultOpen?booleanfalsequery?string—defaultQuery?string""disabled?booleanfalsefilter?(value: string, query: string, keywords: string[]) => boolean—事件
update:modelValue—@update:modelValuechange—@changeupdate:open—@update:openupdate:query—@update:query插槽
default—作用域参数: value, open, query
#defaultComboboxTrigger
插槽
default() => VNodeChild#defaultComboboxInput
placeholder?string"Search…"wrapperClassName?string—showIcon?booleantrueComboboxContent
side?"top" | "bottom""bottom"align?"start" | "center" | "end""start"sideOffset?number6avoidCollisions?booleantrue插槽
default() => VNodeChild#defaultComboboxList
ariaLabel?string"Options"插槽
default() => VNodeChild#defaultComboboxEmpty
插槽
default() => VNodeChild#defaultComboboxGroup
插槽
default() => VNodeChild#defaultComboboxLabel
插槽
default() => VNodeChild#defaultComboboxItem
valuestring—textValue?string—label?string—keywords?string[]() => []disabled?booleanfalse事件
select[value: string]@select插槽
default() => VNodeChild#default无障碍
为交互控件提供明确的无障碍名称。保留文档中的键盘路径、可见焦点与减少动态效果状态;自定义插槽应继续保留所替代部件的语义。
常见问题
- 如何配置 Combobox?
- 通过 modelValue、defaultValue、open、defaultOpen、query、defaultQuery、disabled、filter 配置 Combobox;API 表列出其类型与默认值。
- 如何连接到应用状态?
- 监听 update:modelValue、change、update:open、update:query,将组件连接到应用状态。
- 可以在服务端渲染吗?
- 可以在 Vue SSR 应用中正常渲染。服务端与客户端的初始 props 应保持一致;浏览器测量与交互资源在挂载后启动。
更新于
来源与许可 ↗MIT · 0.1.1