feat(combo-box): migrate ComboBox with input-owned focus and filtering

Port the fixed-baseline ComboBox onto the B02a collection protocol: the input
keeps real DOM focus, the list only tracks an active row, and filtering reuses
the owner filter instead of a second collection kernel.

- Five parts: root (field text control, value/input/open models, form value,
  owner wiring), InputGroup (composes the caller's Input into the combobox
  control and anchors the popover), Trigger, Value, Popover.
- Closed-state metadata is planned from the slot tree, so filtering, the initial
  value label and form-value="text" work before the list has mounted.
- Selection text syncs on selection changes only, so allows-custom-value typing
  is not overwritten; read-only and disabled are injected onto the composed
  input because it reads the text-field protocol, not the field context.
- 24 baseline previews, the Chinese doc page, navigation, 19 behaviour tests and
  6 demo/document contracts. Async demo uses a local async source with abort,
  loading and incremental append (documented adaptations).

Not included: independent tarball consumption (B02d).
This commit is contained in:
maoyingjie
2026-10-25 16:59:16 +08:00
parent ff486fb665
commit 46e4df7e85
46 changed files with 4322 additions and 2 deletions
@@ -0,0 +1,53 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Description,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
</script>
<template>
<ComboBox allows-custom-value class="w-[256px]">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索或输入动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="aardvark" text-value="土豚">
土豚
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="kangaroo" text-value="袋鼠">
袋鼠
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="panda" text-value="熊猫">
熊猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="snake" text-value="蛇">
蛇
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
<Description>可输入任意动物名称,即使不在列表中</Description>
</ComboBox>
</template>
@@ -0,0 +1,158 @@
<!-- 适配:源示例请求 SWAPI,这里用本地异步源演示相同状态(取消旧请求、加载中、增量加载)。 -->
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from "vue";
import {
Button,
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
EmptyState,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Spinner,
} from "@heroui-vue/components";
type Character = {
id: string;
name: string;
};
const PAGE_SIZE = 4;
const PAGE_DELAY = 300;
const characters: Character[] = [
{ id: "luke-skywalker", name: "卢克·天行者" },
{ id: "princess-leia", name: "莱娅公主" },
{ id: "han-solo", name: "汉·索罗" },
{ id: "darth-vader", name: "达斯·维达" },
{ id: "yoda", name: "尤达" },
{ id: "obi-wan-kenobi", name: "欧比旺·克诺比" },
{ id: "palpatine", name: "帕尔帕廷" },
{ id: "lando-calrissian", name: "兰多·卡瑞辛" },
{ id: "chewbacca", name: "楚巴卡" },
{ id: "r2-d2", name: "R2-D2" },
{ id: "c-3po", name: "C-3PO" },
{ id: "boba-fett", name: "波巴·费特" },
];
const inputValue = ref("");
const items = ref<Character[]>([]);
const loading = ref(false);
const loadingMore = ref(false);
const hasMore = ref(false);
let controller: AbortController | undefined;
function matches(query: string) {
const text = query.trim().toLocaleLowerCase();
return characters.filter(
(character) => !text || character.name.toLocaleLowerCase().includes(text),
);
}
async function load(query: string, offset: number, append: boolean) {
controller?.abort();
const current = new AbortController();
controller = current;
if (append) loadingMore.value = true;
else loading.value = true;
try {
const page = await new Promise<Character[]>((resolve, reject) => {
const timer = setTimeout(() => {
resolve(matches(query).slice(offset, offset + PAGE_SIZE));
}, PAGE_DELAY);
current.signal.addEventListener(
"abort",
() => {
clearTimeout(timer);
reject(new DOMException("Aborted", "AbortError"));
},
{ once: true },
);
});
if (current.signal.aborted) return;
items.value = append ? [...items.value, ...page] : page;
hasMore.value = offset + page.length < matches(query).length;
} catch {
if (current.signal.aborted) return;
items.value = [];
hasMore.value = false;
} finally {
// A newer request owns the flags once it has taken over the controller.
if (controller === current) {
loading.value = false;
loadingMore.value = false;
}
}
}
function handleInputChange(next: string) {
inputValue.value = next;
void load(next, 0, false);
}
function loadMore() {
if (loading.value || loadingMore.value || !hasMore.value) return;
void load(inputValue.value, items.value.length, true);
}
onMounted(() => {
void load(inputValue.value, 0, false);
});
onBeforeUnmount(() => {
controller?.abort();
});
</script>
<template>
<ComboBox
allows-empty-collection
class="w-[256px]"
:input-value="inputValue"
@input-change="handleInputChange"
>
<Label>选择角色</Label>
<ComboBoxInputGroup>
<Input placeholder="星球大战角色…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox :aria-busy="loading || undefined">
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">
{{ loading ? "正在加载…" : "没有找到角色" }}
</EmptyState>
</template>
</ListBox>
<div v-if="hasMore" class="flex items-center justify-center gap-2 py-2">
<template v-if="loadingMore">
<Spinner size="sm" />
<span class="text-sm text-muted" role="status">加载更多…</span>
</template>
<Button v-else variant="ghost" size="sm" @click="loadMore">
加载更多…
</Button>
</div>
</ComboBoxPopover>
</ComboBox>
</template>
@@ -0,0 +1,61 @@
<script setup lang="ts">
import { ref } from "vue";
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
const inputValue = ref("");
</script>
<template>
<div class="space-y-2">
<ComboBox
class="w-[256px]"
:input-value="inputValue"
@input-change="inputValue = $event"
>
<Label>搜索(受控输入)</Label>
<ComboBoxInputGroup>
<Input placeholder="输入以搜索…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="aardvark" text-value="土豚">
土豚
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="kangaroo" text-value="袋鼠">
袋鼠
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="panda" text-value="熊猫">
熊猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="snake" text-value="蛇">
蛇
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
<p class="text-sm text-muted">输入值:{{ inputValue || "(空)" }}</p>
</div>
</template>
+59
View File
@@ -0,0 +1,59 @@
<script setup lang="ts">
import { computed, ref } from "vue";
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
type ListBoxKey,
} from "@heroui-vue/components";
const animals = [
{ id: "cat", name: "猫" },
{ id: "dog", name: "狗" },
{ id: "bird", name: "鸟" },
{ id: "fish", name: "鱼" },
{ id: "hamster", name: "仓鼠" },
];
const selectedKey = ref<ListBoxKey | null>("cat");
const selectedAnimal = computed(() =>
animals.find((animal) => animal.id === selectedKey.value),
);
</script>
<template>
<div class="space-y-2">
<ComboBox
class="w-[256px]"
:selected-key="selectedKey"
@selection-change="selectedKey = $event"
>
<Label>动物(受控)</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem
v-for="animal in animals"
:id="animal.id"
:key="animal.id"
:text-value="animal.name"
>
{{ animal.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
<p class="text-sm text-muted">已选:{{ selectedAnimal?.name || "无" }}</p>
</div>
</template>
@@ -0,0 +1,50 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
const animals = [
{ id: "cat", name: "猫" },
{ id: "dog", name: "狗" },
{ id: "bird", name: "鸟" },
{ id: "fish", name: "鱼" },
{ id: "hamster", name: "仓鼠" },
];
</script>
<template>
<ComboBox
class="w-[256px]"
:default-filter="
(text, inputValue) =>
!inputValue || text.toLowerCase().includes(inputValue.toLowerCase())
"
>
<Label>动物(自定义筛选)</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem
v-for="animal in animals"
:id="animal.id"
:key="animal.id"
:text-value="animal.name"
>
{{ animal.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
</template>
@@ -0,0 +1,54 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
import { IconSelector } from "@tabler/icons-vue";
</script>
<template>
<ComboBox class="w-[256px]">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger>
<IconSelector class="size-3" aria-hidden="true" />
</ComboBoxTrigger>
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="aardvark" text-value="土豚">
土豚
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="kangaroo" text-value="袋鼠">
袋鼠
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="panda" text-value="熊猫">
熊猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="snake" text-value="蛇">
蛇
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
</template>
@@ -0,0 +1,48 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
const itemClass =
"rounded-lg data-[focused=true]:bg-muted/70 data-[selected=true]:font-medium data-[selected=true]:text-foreground";
</script>
<template>
<ComboBox class="w-64 gap-1.5">
<Label class="font-medium text-neutral-800 dark:text-neutral-100">
框架
</Label>
<ComboBoxInputGroup
class="rounded-xl border border-border/80 bg-surface shadow-sm ring-1 ring-black/5 transition-[box-shadow,border-color] focus-within:ring-2 focus-within:ring-neutral-400/25 dark:ring-white/10 dark:focus-within:ring-neutral-500/30"
>
<Input placeholder="搜索…" />
<ComboBoxTrigger class="text-muted" />
</ComboBoxInputGroup>
<ComboBoxPopover
class="rounded-xl border border-border/80 bg-surface p-1 shadow-lg ring-1 ring-black/5 dark:ring-white/10"
>
<ListBox>
<ListBoxItem :class="itemClass" id="react" text-value="React">
React
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem :class="itemClass" id="vue" text-value="Vue">
Vue
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem :class="itemClass" id="svelte" text-value="Svelte">
Svelte
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
</template>
@@ -0,0 +1,68 @@
<script setup lang="ts">
import {
Button,
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
FieldError,
Form,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
const animals = [
{ id: "aardvark", name: "土豚" },
{ id: "cat", name: "猫" },
{ id: "dog", name: "狗" },
{ id: "kangaroo", name: "袋鼠" },
{ id: "panda", name: "熊猫" },
{ id: "snake", name: "蛇" },
];
function validate(value: string | number | (string | number)[] | null) {
if (value == null) {
return "请选择一种动物";
}
if (value === "snake") {
return "不允许选择蛇";
}
return true;
}
function onSubmit() {
alert("表单提交成功!");
}
</script>
<template>
<Form class="flex w-[256px] flex-col gap-4" @submit.prevent="onSubmit">
<ComboBox is-required class="w-full" name="animal" :validate="validate">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem
v-for="animal in animals"
:id="animal.id"
:key="animal.id"
:text-value="animal.name"
>
{{ animal.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
<FieldError />
</ComboBox>
<Button class="w-fit" type="submit">提交</Button>
</Form>
</template>
@@ -0,0 +1,90 @@
<script setup lang="ts">
import {
Avatar,
AvatarFallback,
AvatarImage,
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Description,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
const users = [
{
id: "1",
name: "Bob",
email: "bob@heroui.com",
fallback: "B",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg",
},
{
id: "2",
name: "Fred",
email: "fred@heroui.com",
fallback: "F",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg",
},
{
id: "3",
name: "Martha",
email: "martha@heroui.com",
fallback: "M",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg",
},
{
id: "4",
name: "John",
email: "john@heroui.com",
fallback: "J",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/red.jpg",
},
{
id: "5",
name: "Jane",
email: "jane@heroui.com",
fallback: "J",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/orange.jpg",
},
];
</script>
<template>
<ComboBox class="w-[256px]">
<Label>用户</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索用户…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem
v-for="user in users"
:id="user.id"
:key="user.id"
:text-value="user.name"
>
<Avatar size="sm">
<AvatarImage :src="user.avatarUrl" />
<AvatarFallback>{{ user.fallback }}</AvatarFallback>
</Avatar>
<div class="flex flex-col">
<Label>{{ user.name }}</Label>
<Description>{{ user.email }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
</template>
@@ -0,0 +1,51 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
</script>
<template>
<ComboBox class="w-[256px]" default-selected-key="cat">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="aardvark" text-value="土豚">
土豚
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="kangaroo" text-value="袋鼠">
袋鼠
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="panda" text-value="熊猫">
熊猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="snake" text-value="蛇">
蛇
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
</template>
+45
View File
@@ -0,0 +1,45 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
const animals = [
{ id: "aardvark", name: "土豚" },
{ id: "cat", name: "猫" },
{ id: "dog", name: "狗" },
{ id: "kangaroo", name: "袋鼠" },
{ id: "panda", name: "熊猫" },
{ id: "snake", name: "蛇" },
];
</script>
<template>
<ComboBox class="w-[256px]">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem
v-for="animal in animals"
:id="animal.id"
:key="animal.id"
:text-value="animal.name"
>
{{ animal.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
</template>
+51
View File
@@ -0,0 +1,51 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
</script>
<template>
<ComboBox is-disabled class="w-[256px]" default-selected-key="cat">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="aardvark" text-value="土豚">
土豚
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="kangaroo" text-value="袋鼠">
袋鼠
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="panda" text-value="熊猫">
熊猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="snake" text-value="蛇">
蛇
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
</template>
+86
View File
@@ -0,0 +1,86 @@
<script setup lang="ts">
import {
Button,
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Description,
FieldError,
Form,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
import { ref } from "vue";
const animals = [
{ id: "aardvark", name: "土豚" },
{ id: "cat", name: "猫" },
{ id: "dog", name: "狗" },
{ id: "kangaroo", name: "袋鼠" },
{ id: "panda", name: "熊猫" },
{ id: "snake", name: "蛇" },
];
const message = ref<string | null>(null);
function onSubmit(event: Event) {
event.preventDefault();
const formData = new FormData(event.currentTarget as HTMLFormElement);
message.value = `key:${formData.get("animal")}。文本:${formData.get("animal-text")}。`;
}
</script>
<template>
<Form class="flex w-[256px] flex-col gap-4" @submit="onSubmit">
<ComboBox is-required class="w-full" form-value="key" name="animal">
<Label>动物</Label>
<ComboBoxInputGroup>
<Input placeholder="选择动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem
v-for="animal in animals"
:id="animal.id"
:key="animal.id"
:text-value="animal.name"
>
{{ animal.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
<Description>提交选中项的 key</Description>
<FieldError />
</ComboBox>
<ComboBox is-required class="w-full" form-value="text" name="animal-text">
<Label>动物(文本)</Label>
<ComboBoxInputGroup>
<Input placeholder="选择动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem
v-for="animal in animals"
:id="animal.id"
:key="animal.id"
:text-value="animal.name"
>
{{ animal.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
<Description>提交选中项的文本</Description>
<FieldError />
</ComboBox>
<Button class="w-fit" type="submit">提交</Button>
<p v-if="message" class="text-sm text-muted">{{ message }}</p>
</Form>
</template>
+41
View File
@@ -0,0 +1,41 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
</script>
<template>
<div class="w-[400px] space-y-4">
<ComboBox full-width>
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="aardvark" text-value="土豚">
土豚
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
</div>
</template>
+138
View File
@@ -0,0 +1,138 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Description,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
</script>
<template>
<div class="flex flex-col gap-8">
<div class="flex flex-col gap-2">
<p class="text-sm font-medium text-muted">聚焦(默认)</p>
<ComboBox class="w-[256px]" menu-trigger="focus">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="aardvark" text-value="土豚">
土豚
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="kangaroo" text-value="袋鼠">
袋鼠
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="panda" text-value="熊猫">
熊猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="snake" text-value="蛇">
蛇
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
<Description>输入框获得焦点时打开弹出层</Description>
</ComboBox>
</div>
<div class="flex flex-col gap-2">
<p class="text-sm font-medium text-muted">输入</p>
<ComboBox class="w-[256px]" menu-trigger="input">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="aardvark" text-value="土豚">
土豚
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="kangaroo" text-value="袋鼠">
袋鼠
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="panda" text-value="熊猫">
熊猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="snake" text-value="蛇">
蛇
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
<Description>用户编辑输入文本时打开弹出层</Description>
</ComboBox>
</div>
<div class="flex flex-col gap-2">
<p class="text-sm font-medium text-muted">手动</p>
<ComboBox class="w-[256px]" menu-trigger="manual">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="aardvark" text-value="土豚">
土豚
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="kangaroo" text-value="袋鼠">
袋鼠
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="panda" text-value="熊猫">
熊猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="snake" text-value="蛇">
蛇
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
<Description>仅在按下触发按钮或使用方向键时打开弹出层</Description>
</ComboBox>
</div>
</div>
</template>
@@ -0,0 +1,47 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
ComboBoxValue,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
const animals = [
{ id: "aardvark", name: "土豚" },
{ id: "cat", name: "猫" },
{ id: "dog", name: "狗" },
{ id: "kangaroo", name: "袋鼠" },
{ id: "panda", name: "熊猫" },
{ id: "snake", name: "蛇" },
];
</script>
<template>
<ComboBox class="w-[256px]" selection-mode="multiple">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxValue placeholder="未选择任何动物" />
<ComboBoxPopover>
<ListBox selection-mode="multiple">
<ListBoxItem
v-for="animal in animals"
:id="animal.id"
:key="animal.id"
:text-value="animal.name"
>
{{ animal.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
</template>
+66
View File
@@ -0,0 +1,66 @@
<script setup lang="ts">
import {
Button,
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
FieldError,
Form,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Surface,
} from "@heroui-vue/components";
function onSubmit(event: Event) {
event.preventDefault();
alert("表单提交成功!");
}
</script>
<template>
<Surface class="w-[320px] rounded-3xl p-6">
<Form class="flex w-full flex-col gap-4" @submit="onSubmit">
<ComboBox is-required class="w-full" name="animal" variant="secondary">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="aardvark" text-value="土豚">
土豚
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="kangaroo" text-value="袋鼠">
袋鼠
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="panda" text-value="熊猫">
熊猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="snake" text-value="蛇">
蛇
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
<FieldError />
</ComboBox>
<Button type="submit">提交</Button>
</Form>
</Surface>
</template>
+45
View File
@@ -0,0 +1,45 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
const animals = [
{ id: "aardvark", name: "土豚" },
{ id: "cat", name: "猫" },
{ id: "dog", name: "狗" },
{ id: "kangaroo", name: "袋鼠" },
{ id: "panda", name: "熊猫" },
{ id: "snake", name: "蛇" },
];
</script>
<template>
<ComboBox is-read-only class="w-[256px]" default-selected-key="cat">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem
v-for="animal in animals"
:id="animal.id"
:key="animal.id"
:text-value="animal.name"
>
{{ animal.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
</template>
@@ -0,0 +1,51 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
</script>
<template>
<ComboBox class="w-[256px]" as="div" data-custom="foo">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="aardvark" text-value="土豚">
土豚
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="kangaroo" text-value="袋鼠">
袋鼠
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="panda" text-value="熊猫">
熊猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="snake" text-value="蛇">
蛇
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
</template>
+75
View File
@@ -0,0 +1,75 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Button,
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
FieldError,
Form,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
const submitted = ref(false);
function submit(event: Event) {
const formData = new FormData(event.currentTarget as HTMLFormElement);
const data: Record<string, string> = {};
formData.forEach((value, key) => {
data[key] = value.toString();
});
submitted.value = "animal" in data;
}
</script>
<template>
<Form class="flex w-[256px] flex-col gap-4" @submit.prevent="submit">
<ComboBox is-required class="w-full" name="animal">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="aardvark" text-value="土豚">
土豚
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="kangaroo" text-value="袋鼠">
袋鼠
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="panda" text-value="熊猫">
熊猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="snake" text-value="蛇">
蛇
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
<FieldError />
</ComboBox>
<Button type="submit">提交</Button>
<p v-if="submitted" role="status" class="text-sm text-success">
表单提交成功!
</p>
</Form>
</template>
@@ -0,0 +1,107 @@
<script setup lang="ts">
import {
Button,
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Description,
FieldError,
Form,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
import { ref } from "vue";
const animals = [
{ id: "aardvark", name: "土豚" },
{ id: "cat", name: "猫" },
{ id: "dog", name: "狗" },
{ id: "kangaroo", name: "袋鼠" },
{ id: "panda", name: "熊猫" },
{ id: "snake", name: "蛇" },
];
const nativeMessage = ref<string | null>(null);
const ariaMessage = ref<string | null>(null);
function submitNative(event: Event) {
event.preventDefault();
nativeMessage.value = "已通过原生校验提交。";
}
function submitAria(event: Event) {
event.preventDefault();
ariaMessage.value = "已通过 ARIA 校验提交。";
}
</script>
<template>
<div class="flex flex-col gap-8">
<Form class="flex w-[256px] flex-col gap-4" @submit="submitNative">
<ComboBox
is-required
class="w-full"
name="animal"
validation-behavior="native"
>
<Label>动物(原生校验)</Label>
<ComboBoxInputGroup>
<Input placeholder="选择动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem
v-for="animal in animals"
:id="animal.id"
:key="animal.id"
:text-value="animal.name"
>
{{ animal.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
<Description>值为空时阻止提交</Description>
<FieldError />
</ComboBox>
<Button class="w-fit" type="submit">提交</Button>
<p v-if="nativeMessage" class="text-sm text-muted">{{ nativeMessage }}</p>
</Form>
<Form class="flex w-[256px] flex-col gap-4" @submit="submitAria">
<ComboBox
is-required
class="w-full"
name="animal-aria"
validation-behavior="aria"
>
<Label>动物(ARIA 校验)</Label>
<ComboBoxInputGroup>
<Input placeholder="选择动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem
v-for="animal in animals"
:id="animal.id"
:key="animal.id"
:text-value="animal.name"
>
{{ animal.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
<Description>实时显示错误,不阻止提交</Description>
<FieldError />
</ComboBox>
<Button class="w-fit" type="submit">提交</Button>
<p v-if="ariaMessage" class="text-sm text-muted">{{ ariaMessage }}</p>
</Form>
</div>
</template>
@@ -0,0 +1,53 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Description,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
</script>
<template>
<ComboBox class="w-[256px]">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="aardvark" text-value="土豚">
土豚
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="kangaroo" text-value="袋鼠">
袋鼠
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="panda" text-value="熊猫">
熊猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="snake" text-value="蛇">
蛇
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
<Description>搜索并选择你最喜欢的动物</Description>
</ComboBox>
</template>
@@ -0,0 +1,53 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
const disabledKeys = ["cat", "kangaroo"];
</script>
<template>
<ComboBox class="w-[256px]" :disabled-keys="disabledKeys">
<Label>动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem id="dog" text-value="狗">
狗
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="cat" text-value="猫">
猫
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="bird" text-value="鸟">
鸟
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="kangaroo" text-value="袋鼠">
袋鼠
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="elephant" text-value="象">
象
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="tiger" text-value="老虎">
老虎
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
</ComboBox>
</template>
@@ -0,0 +1,89 @@
<script setup lang="ts">
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Header,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
ListBoxSection,
Separator,
} from "@heroui-vue/components";
</script>
<template>
<ComboBox class="w-[256px]">
<Label>国家</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索国家…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxSection>
<Header>北美洲</Header>
<ListBoxItem id="usa" text-value="美国">
美国
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="canada" text-value="加拿大">
加拿大
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="mexico" text-value="墨西哥">
墨西哥
<ListBoxItemIndicator />
</ListBoxItem>
</ListBoxSection>
<Separator />
<ListBoxSection>
<Header>欧洲</Header>
<ListBoxItem id="uk" text-value="英国">
英国
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="france" text-value="法国">
法国
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="germany" text-value="德国">
德国
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="spain" text-value="西班牙">
西班牙
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="italy" text-value="意大利">
意大利
<ListBoxItemIndicator />
</ListBoxItem>
</ListBoxSection>
<Separator />
<ListBoxSection>
<Header>亚洲</Header>
<ListBoxItem id="japan" text-value="日本">
日本
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="china" text-value="中国">
中国
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="india" text-value="印度">
印度
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="south-korea" text-value="韩国">
韩国
<ListBoxItemIndicator />
</ListBoxItem>
</ListBoxSection>
</ListBox>
</ComboBoxPopover>
</ComboBox>
</template>