ข้ามไปเนื้อหา
WhereUI

ค้นหาในเอกสาร

ค้นหาชื่อ component หรือหน้าเอกสาร

Error State

WhereUI

ส่วนแสดงข้อผิดพลาดกลางหน้า พร้อมไอคอน ข้อความอธิบาย และปุ่มลองใหม่ — ใช้แทนหน้าว่างเปล่าเมื่อโหลดข้อมูลไม่สำเร็จ

ติดตั้ง

Terminal
pnpm dlx shadcn@latest add @whereui/error-state

ต้องผูก registry และติดตั้ง @whereui/theme นำหน้าเสมอเมื่อติดตั้งครั้งแรก

การใช้งาน

import { ErrorState } from "@/components/ui/error-state"<ErrorState  description="โหลดรายชื่อพนักงานไม่สำเร็จ"  onRetry={() => refetch()}/>

ตัวอย่างเพิ่มเติม

ไม่มีปุ่มลองใหม่

Props

proptypeค่าเริ่มต้นคำอธิบาย
titleReact.ReactNode"เกิดข้อผิดพลาด"หัวข้อความผิดพลาด
descriptionReact.ReactNode"ไม่สามารถโหลดข้อมูลได้ โปรดลองอีกครั้ง"คำอธิบายสิ่งที่ผิดพลาด
onRetry() => void—ถ้าส่งมาจะแสดงปุ่มลองใหม่
retryLabelstring"ลองอีกครั้ง"ข้อความปุ่มลองใหม่

ข้อควรรู้

  • รากมี 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 }