Error State
WhereUIส่วนแสดงข้อผิดพลาดกลางหน้า พร้อมไอคอน ข้อความอธิบาย และปุ่มลองใหม่ — ใช้แทนหน้าว่างเปล่าเมื่อโหลดข้อมูลไม่สำเร็จ
โหลดรายชื่อพนักงานไม่สำเร็จ
มีปัญหาการเชื่อมต่อกับเซิร์ฟเวอร์ (500) โปรดลองอีกครั้ง
ติดตั้ง
Terminal
pnpm dlx shadcn@latest add @whereui/error-stateต้องผูก registry และติดตั้ง @whereui/theme นำหน้าเสมอเมื่อติดตั้งครั้งแรก
การใช้งาน
import { ErrorState } from "@/components/ui/error-state"<ErrorState description="โหลดรายชื่อพนักงานไม่สำเร็จ" onRetry={() => refetch()}/>ตัวอย่างเพิ่มเติม
ไม่มีปุ่มลองใหม่
เกิดข้อผิดพลาด
ไม่สามารถโหลดข้อมูลได้ โปรดลองอีกครั้ง
Props
| prop | type | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| title | React.ReactNode | "เกิดข้อผิดพลาด" | หัวข้อความผิดพลาด |
| description | React.ReactNode | "ไม่สามารถโหลดข้อมูลได้ โปรดลองอีกครั้ง" | คำอธิบายสิ่งที่ผิดพลาด |
| onRetry | () => void | — | ถ้าส่งมาจะแสดงปุ่มลองใหม่ |
| retryLabel | string | "ลองอีกครั้ง" | ข้อความปุ่มลองใหม่ |
ข้อควรรู้
- รากมี role="alert" โปรแกรมอ่านหน้าจอจะประกาศทันทีเมื่อเกิดข้อผิดพลาด
- โครงเลียนแบบ empty.tsx (ไอคอน หัวข้อ คำอธิบายเรียงกลาง) แต่ไม่มีกรอบเส้นประ เพราะมักใช้เต็มพื้นที่หน้า
ซอร์สโค้ด
components/ui/error-state.tsx
import * as React from "react"import { CircleAlertIcon } from "lucide-react"import { cn } from "cn"import { Button } from "@/components/ui/button"// หน้า/ส่วนผิดพลาด — โครงตาม empty.tsx (กลางหน้า ข้อความสมดุล) พร้อมปุ่มลองใหม่function ErrorState({ className, title = "เกิดข้อผิดพลาด", description = "ไม่สามารถโหลดข้อมูลได้ โปรดลองอีกครั้ง", onRetry, retryLabel = "ลองอีกครั้ง", ...props}: React.ComponentProps<"div"> & { title?: React.ReactNode description?: React.ReactNode /** ถ้าส่งมาจะแสดงปุ่มลองใหม่ */ onRetry?: () => void retryLabel?: string}) { return ( <div data-slot="error-state" role="alert" className={cn( "flex min-h-64 w-full min-w-0 flex-col items-center justify-center gap-4 p-6 text-center", className )} {...props} > <CircleAlertIcon aria-hidden="true" className="size-10 shrink-0 text-destructive" /> <div className="flex max-w-sm flex-col gap-1 text-balance"> <div className="font-heading text-base font-medium">{title}</div> <div className="text-sm text-muted-foreground">{description}</div> </div> {onRetry ? ( <Button variant="outline" onClick={onRetry}> {retryLabel} </Button> ) : null} </div> )}export { ErrorState }