feat: migrate Toast and Table with reviewed state focus and SSR contracts
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
<script setup lang="ts">
|
||||
import { onBeforeUnmount } from "vue";
|
||||
import {
|
||||
Button,
|
||||
ToastProvider,
|
||||
ToastQueue,
|
||||
createToastFunction,
|
||||
} from "@heroui-vue/components";
|
||||
const queue = new ToastQueue();
|
||||
const toast = createToastFunction(queue);
|
||||
const timers = new Set<ReturnType<typeof setTimeout>>();
|
||||
function later(run: () => void, delay = 2000) {
|
||||
const timer = setTimeout(() => {
|
||||
timers.delete(timer);
|
||||
run();
|
||||
}, delay);
|
||||
timers.add(timer);
|
||||
}
|
||||
onBeforeUnmount(() => {
|
||||
timers.forEach(clearTimeout);
|
||||
queue.destroy();
|
||||
});
|
||||
function upload() {
|
||||
toast.promise(
|
||||
new Promise<{ filename: string; size: number }>((resolve) =>
|
||||
later(() => resolve({ filename: "document.pdf", size: 1024 })),
|
||||
),
|
||||
{
|
||||
loading: "正在上传文件…",
|
||||
success: (data) => `文件 ${data.filename} 已上传(${data.size}KB)`,
|
||||
error: "上传文件失败",
|
||||
},
|
||||
);
|
||||
}
|
||||
function createEvent() {
|
||||
toast.promise(
|
||||
new Promise<never>((_, reject) =>
|
||||
later(() => reject(new Error("网络错误,请重试。"))),
|
||||
),
|
||||
{
|
||||
loading: "正在创建活动…",
|
||||
success: "活动已创建",
|
||||
error: (err) => err.message,
|
||||
},
|
||||
);
|
||||
}
|
||||
function save() {
|
||||
toast.promise(
|
||||
new Promise<{ count: number }>((resolve, reject) =>
|
||||
later(() =>
|
||||
Math.random() > 0.5
|
||||
? resolve({ count: 42 })
|
||||
: reject(new Error("保存数据失败")),
|
||||
),
|
||||
),
|
||||
{
|
||||
loading: "正在保存更改…",
|
||||
success: (data) => `已保存 ${data.count} 项`,
|
||||
error: (err) => err.message,
|
||||
},
|
||||
);
|
||||
}
|
||||
function fetchUser() {
|
||||
toast.promise(
|
||||
new Promise<{ name: string; email: string }>((resolve) =>
|
||||
later(() => resolve({ name: "John Doe", email: "john@example.com" })),
|
||||
),
|
||||
{
|
||||
loading: "正在加载用户…",
|
||||
success: (data) => `欢迎回来,${data.name}!`,
|
||||
error: "获取用户失败",
|
||||
},
|
||||
);
|
||||
}
|
||||
function manual(kind: "upload" | "payment" | "error") {
|
||||
const messages = {
|
||||
upload: ["正在上传文件…", "文件已上传", "您的文件已成功上传", 3000],
|
||||
payment: ["正在处理付款…", "付款已处理", "您的付款已成功处理", 2500],
|
||||
error: ["正在保存更改…", "保存失败", "请重试", 2000],
|
||||
} as const;
|
||||
const [loading, title, description, delay] = messages[kind];
|
||||
const id = toast(loading, {
|
||||
isLoading: true,
|
||||
timeout: 0,
|
||||
description: kind === "upload" ? "请稍候,正在上传您的文件" : undefined,
|
||||
});
|
||||
later(() => {
|
||||
if (
|
||||
queue.visibleToasts.some((item) => item.key === id) &&
|
||||
!queue.getExitingKeys().has(id)
|
||||
)
|
||||
toast.update(id, title, {
|
||||
description,
|
||||
variant: kind === "error" ? "danger" : "success",
|
||||
timeout: 4000,
|
||||
});
|
||||
}, delay);
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
<div class="flex h-full max-w-2xl flex-col items-center justify-center gap-8">
|
||||
<div class="w-full space-y-3">
|
||||
<div class="text-center">
|
||||
<h3 class="text-sm font-medium">使用 toast.promise()</h3>
|
||||
<p class="text-xs text-muted">自动处理加载、成功和错误状态</p>
|
||||
</div>
|
||||
<div class="flex w-full flex-wrap items-center justify-center gap-4">
|
||||
<Button size="sm" variant="secondary" @click="upload">上传文件</Button>
|
||||
<Button size="sm" variant="secondary" @click="createEvent">
|
||||
创建活动(错误)
|
||||
</Button>
|
||||
<Button size="sm" variant="secondary" @click="save">
|
||||
保存数据(随机)
|
||||
</Button>
|
||||
<Button size="sm" variant="secondary" @click="fetchUser">
|
||||
获取用户
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="w-full space-y-3">
|
||||
<div class="text-center">
|
||||
<h3 class="text-sm font-medium">手动加载状态</h3>
|
||||
<p class="text-xs text-muted">使用 isLoading 属性手动控制加载状态</p>
|
||||
</div>
|
||||
<div class="flex w-full flex-wrap items-center justify-center gap-4">
|
||||
<Button size="sm" variant="secondary" @click="manual('upload')">
|
||||
上传(含加载)
|
||||
</Button>
|
||||
<Button size="sm" variant="secondary" @click="manual('payment')">
|
||||
付款处理
|
||||
</Button>
|
||||
<Button size="sm" variant="secondary" @click="manual('error')">
|
||||
加载后显示错误
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<ToastProvider :queue="queue" to="body" />
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user