"use client"; import { Progress as ProgressPrimitive } from "@base-ui/react/progress"; import { ProgressIndicator, ProgressLabel, ProgressTrack, ProgressValue, } from "@suphark/ui/components/ui/progress"; import { cn } from "@suphark/ui/lib/utils"; import type { CSSProperties, ReactNode } from "react"; /** `danger`/`neutral` = ชื่อเดียวกับ `destructive`/`default` (ให้ตรงกับ StatusBadge/Notice/Banner) */ export type ProgressTone = "default" | "success" | "warning" | "destructive" | "danger" | "neutral"; const TONE: Record = { default: "bg-primary", success: "bg-success", warning: "bg-warning", destructive: "bg-destructive", neutral: "bg-primary", danger: "bg-destructive", }; /** * แถบความคืบหน้าที่ตั้งสี/ความสูง/ป้ายได้จาก props — ใช้แทนการประกอบ `Progress` ของ shadcn เอง * (`Progress` ต่อท้าย Track+Indicator ให้เสมอ ส่ง children เข้าไปจะได้ 2 แถบ) — ยกจาก DueBell * * ไม่มี `label` = ต้องส่ง `aria-label` (เช่น "อัปโหลด รายงาน.pdf") ไม่งั้นโปรแกรมอ่านจอไม่รู้ว่าแถบนี้คืออะไร * `value={null}` = ไม่ทราบความคืบหน้า (แถบวิ่ง) · NaN/Infinity (คำนวณจากข้อมูลที่ยังไม่มา) = 0 */ export function ProgressBar({ value, max = 100, label, showValue = false, format, tone = "default", size = "sm", trackClassName, indicatorClassName, indicatorStyle, className, ...props }: Omit & { value: number | null; max?: number; label?: ReactNode; /** แสดงตัวเลข (ค่าเริ่มต้น "N%") */ showValue?: boolean; /** ปรับข้อความตัวเลข เช่น (v) => `${v} / ${max}` */ format?: (value: number | null, max: number) => ReactNode; /** `auto` = เขียวเมื่อครบ (value ≥ max) นอกนั้นสีหลัก — แถบงานย่อยที่เต็มแล้วเปลี่ยนสีเอง */ tone?: ProgressTone | "auto"; /** sm = h-1 (ค่าเริ่มต้น), md = h-2, lg = h-3 */ size?: "sm" | "md" | "lg"; trackClassName?: string; /** ใส่ class สีเอง (เช่นสี dynamic ของผู้ใช้ผ่าน indicatorStyle) */ indicatorClassName?: string; indicatorStyle?: CSSProperties; }) { // NaN เดิมกลายเป็นแถบวิ่ง (ไม่ทราบค่า) เงียบ ๆ — 0/0 จากข้อมูลที่ยังไม่โหลดควรเป็น 0% const safeValue = value == null ? null : Number.isFinite(value) ? value : 0; return ( {(label || showValue) && (
{label && {label}} {showValue && (format ? ( {format(safeValue, max)} ) : ( ))}
)} = max ? "success" : "default" : tone ], indicatorClassName, )} style={indicatorStyle} />
); }