feat: migrate Select with field, collection and SSR contracts
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
<script setup lang="ts">
|
||||
import { onBeforeUnmount, onMounted, ref } from "vue";
|
||||
import { useIntersectionObserver } from "@vueuse/core";
|
||||
import {
|
||||
Select,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
SelectIndicator,
|
||||
SelectPopover,
|
||||
ListBox,
|
||||
ListBoxItem,
|
||||
ListBoxItemIndicator,
|
||||
Label,
|
||||
Spinner,
|
||||
Button,
|
||||
} from "@heroui-vue/components";
|
||||
const items = ref<{ id: string; textValue: string }[]>([]);
|
||||
const cursor = ref<string | null>("https://pokeapi.co/api/v2/pokemon");
|
||||
const loading = ref(false),
|
||||
error = ref(false),
|
||||
sentinel = ref<HTMLElement>();
|
||||
let controller: AbortController | undefined;
|
||||
async function loadMore() {
|
||||
if (loading.value || !cursor.value) return;
|
||||
controller = new AbortController();
|
||||
loading.value = true;
|
||||
error.value = false;
|
||||
try {
|
||||
const response = await fetch(cursor.value, { signal: controller.signal });
|
||||
if (!response.ok) throw new Error("Unable to load Pokémon");
|
||||
const page = (await response.json()) as {
|
||||
next: string | null;
|
||||
results: { name: string }[];
|
||||
};
|
||||
const seen = new Set(items.value.map((item) => item.id));
|
||||
items.value.push(
|
||||
...page.results
|
||||
.filter((item) => !seen.has(item.name))
|
||||
.map((item) => ({ id: item.name, textValue: item.name })),
|
||||
);
|
||||
cursor.value = page.next;
|
||||
} catch (cause) {
|
||||
if (!controller.signal.aborted) error.value = true;
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
useIntersectionObserver(sentinel, ([entry]) => {
|
||||
if (entry?.isIntersecting && !error.value) void loadMore();
|
||||
});
|
||||
onMounted(loadMore);
|
||||
onBeforeUnmount(() => controller?.abort());
|
||||
</script>
|
||||
<template>
|
||||
<Select class="w-[256px]" placeholder="请选择宝可梦" :items="items">
|
||||
<Label>选择宝可梦</Label>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
<SelectIndicator />
|
||||
</SelectTrigger>
|
||||
<SelectPopover class="max-h-80">
|
||||
<ListBox :items="items" :aria-busy="loading || undefined">
|
||||
<template #default="{ item }">
|
||||
<ListBoxItem :id="item.id" :text-value="item.textValue">
|
||||
{{ item.textValue }}
|
||||
<ListBoxItemIndicator />
|
||||
</ListBoxItem>
|
||||
</template>
|
||||
<template #empty>
|
||||
<p class="px-4 py-2 text-sm text-muted">
|
||||
{{ error ? "加载失败,请重试" : "正在加载…" }}
|
||||
</p>
|
||||
</template>
|
||||
</ListBox>
|
||||
<div
|
||||
v-if="cursor"
|
||||
ref="sentinel"
|
||||
class="flex items-center justify-center gap-2 py-2"
|
||||
role="status"
|
||||
>
|
||||
<template v-if="loading">
|
||||
<Spinner size="sm" />
|
||||
<span class="text-sm text-muted">加载更多…</span>
|
||||
</template>
|
||||
<Button v-else variant="ghost" size="sm" @click="loadMore">
|
||||
{{ error ? "重试" : "加载更多…" }}
|
||||
</Button>
|
||||
</div>
|
||||
</SelectPopover>
|
||||
</Select>
|
||||
</template>
|
||||
Reference in New Issue
Block a user