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,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>