Files
heroui-vue/apps/docs/demos/combo-box-asynchronous-loading.vue
T
maoyingjie 3e9be13b19 fix(pickers): repair data-slot selectors and align demos with the baseline
The CSS port prefixed data-slot attribute values with hv-, so the compiled
selectors never matched the DOM and the ComboBox trigger chevron never
rotated. Only class names get the prefix; slot values stay byte-identical.

Also commit typed text as the value when allowsCustomValue finds no record,
restore the baseline search-field shell in all 24 Autocomplete demos, and
correct both doc pages against the pinned upstream sources.
2026-10-25 17:46:32 +08:00

157 lines
4.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!-- 适配:源示例请求 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: "Luke Skywalker" },
{ id: "Princess Leia", name: "Princess Leia" },
{ id: "Han Solo", name: "Han Solo" },
{ id: "Darth Vader", name: "Darth Vader" },
{ id: "Yoda", name: "Yoda" },
{ id: "Obi-Wan Kenobi", name: "Obi-Wan Kenobi" },
{ id: "Palpatine", name: "Palpatine" },
{ id: "Lando Calrissian", name: "Lando Calrissian" },
{ id: "Chewbacca", name: "Chewbacca" },
{ id: "R2-D2", name: "R2-D2" },
{ id: "C-3PO", name: "C-3PO" },
{ id: "Boba Fett", name: "Boba Fett" },
];
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 />
</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>