"use client"; import { ToggleGroup, ToggleGroupItem } from "@suphark/ui/components/ui/toggle-group"; import { cn } from "@suphark/ui/lib/utils"; import type { ReactNode } from "react"; import { CountBadge } from "./count-badge"; /** `danger`/`neutral` = ชื่อเดียวกับ `destructive`/`default` (ให้ตรงกับ StatusBadge/Notice/Banner) */ export type SegmentedTone = | "default" | "success" | "warning" | "destructive" | "info" | "danger" | "neutral"; /** สีตอนถูกเลือกเท่านั้น — ตัวที่ไม่ได้เลือกคงหน้าตากลาง ๆ ไม่ให้แถบกลายเป็นรุ้ง */ const TONE: Record = { default: "", success: "aria-pressed:bg-success-subtle aria-pressed:text-success-subtle-foreground", warning: "aria-pressed:bg-warning-subtle aria-pressed:text-warning-subtle-foreground", destructive: "aria-pressed:bg-destructive-subtle aria-pressed:text-destructive-subtle-foreground", info: "aria-pressed:bg-info-subtle aria-pressed:text-info-subtle-foreground", neutral: "", danger: "aria-pressed:bg-destructive-subtle aria-pressed:text-destructive-subtle-foreground", }; export interface SegmentedOption { value: T; label: ReactNode; icon?: ReactNode; /** จำนวนท้ายตัวเลือก เช่นจำนวนรายการในตัวกรองนั้น */ count?: number; /** * สีตามความหมายของตัวเลือกนั้นเมื่อถูกเลือก (รายรับ = success, รายจ่าย = destructive) * ไม่ส่ง = พื้นเทาเหมือนเดิม — ใช้เมื่อ "ประเภท" มีความหมายทางสี ไม่ใช่ตกแต่ง */ tone?: SegmentedTone; disabled?: boolean; } /** * ตัวเลือกแบบปุ่มติดกัน "เลือกได้ทีละหนึ่ง และต้องมีค่าเสมอ" (ตัวกรองสถานะ, มุมมอง รายการ/ตาราง) * ต่างจาก ToggleGroup ตรง ๆ: กดตัวที่เลือกอยู่แล้วไม่หลุดเป็นค่าว่าง และ value เป็น string ไม่ใช่ array * (DueBell bill-list-filter / PDF ตัวกรองโปรเจกต์ เขียนเองพร้อมตัวเลขนับ) * * */ export function SegmentedControl({ value, onValueChange, options, label, size = "default", fullWidth = false, className, }: { value: T; onValueChange: (value: T) => void; options: ReadonlyArray>; /** aria-label ของกลุ่ม */ label: string; size?: "sm" | "default"; fullWidth?: boolean; className?: string; }) { return ( { // กดตัวที่เลือกอยู่ → Base UI คืน [] — คงค่าเดิมไว้ (ต้องมีค่าเสมอ) const v = next[0]; if (v !== undefined && v !== value) onValueChange(v as T); }} variant="outline" size={size === "sm" ? "sm" : "default"} spacing={0} className={cn(fullWidth && "w-full", className)} > {options.map((o) => ( {o.icon} {o.label} {o.count !== undefined && ( )} ))} ); }