"use client"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { cn } from "@suphark/ui/lib/utils"; import { StarIcon } from "lucide-react"; import { type KeyboardEvent, useRef, useState } from "react"; type RatingSize = "sm" | "md" | "lg"; const ICON: Record = { sm: "size-4", md: "size-6", lg: "size-8" }; const BOX: Record = { sm: "size-6 pointer-coarse:size-11", md: "size-8 pointer-coarse:size-11", lg: "size-10 pointer-coarse:size-11", }; /** สีดาว: ทับด้วย CSS var `--rating` (ค่าเริ่มต้น `--warning`) */ const FILL = "[color:var(--rating,var(--warning))]"; /** ดาวหนึ่งดวง: เต็ม / ครึ่ง / ว่าง — ดาวว่างเป็นเส้นขอบ (ไม่พึ่งสีอย่างเดียว) */ function Star({ fill, size }: { fill: 0 | 0.5 | 1; size: RatingSize }) { return ( ); } const fillOf = (value: number, index: number): 0 | 0.5 | 1 => value >= index ? 1 : value >= index - 0.5 ? 0.5 : 0; export interface RatingProps { value?: number; defaultValue?: number; onValueChange?: (value: number) => void; /** จำนวนดาว ค่าเริ่มต้น 5 */ max?: number; /** ให้ครึ่งดาวได้ (คลิกครึ่งซ้ายของดาว / ลูกศรเพิ่มทีละ 0.5) */ allowHalf?: boolean; size?: RatingSize; /** แสดงอย่างเดียว (ใช้ `RatingDisplay` ถ้าต้องการตัวเลข/จำนวนรีวิวด้วย) */ readOnly?: boolean; disabled?: boolean; /** ส่งค่าไปกับฟอร์ม (input ซ่อน) */ name?: string; /** aria-label ของกลุ่ม (ค่าเริ่มต้น `UiText.rating.label`) */ label?: string; className?: string; } /** * ให้คะแนนดาว — `radiogroup`: ลูกศรซ้าย/ขวา (ขึ้น/ลง) เปลี่ยนค่า, Home = ล้าง, End = เต็ม, คลิกดาวที่เลือกอยู่ซ้ำ = ล้าง * แต่ละดาวมีพื้นที่แตะ 44px บนจอสัมผัส · ดาวว่างเป็นเส้นขอบ ดาวที่เลือกเป็นตัวทึบ * * */ export function Rating({ value: controlled, defaultValue = 0, onValueChange, max = 5, allowHalf = false, size = "md", readOnly = false, disabled = false, name, label, className, }: RatingProps) { const text = useUiText().rating; const [inner, setInner] = useState(defaultValue); const value = controlled ?? inner; const refs = useRef>([]); const step = allowHalf ? 0.5 : 1; const set = (next: number) => { const clamped = Math.min(max, Math.max(0, next)); if (controlled === undefined) setInner(clamped); onValueChange?.(clamped); }; const valueText = (text?.valueOf ?? "{value} จาก {max} ดาว") .replace("{value}", String(value)) .replace("{max}", String(max)); if (readOnly) { return ( {Array.from({ length: max }, (_, i) => ( // biome-ignore lint/suspicious/noArrayIndexKey: ดาวเรียงตามตำแหน่ง ไม่มีตัวตนอื่น ))} ); } // ดาวที่รับโฟกัส (roving tabindex): ดาวของค่าปัจจุบัน หรือดาวแรก const focusIndex = Math.max(0, Math.ceil(value) - 1); const onKeyDown = (event: KeyboardEvent) => { let next: number | null = null; if (event.key === "ArrowRight" || event.key === "ArrowUp") next = value + step; else if (event.key === "ArrowLeft" || event.key === "ArrowDown") next = value - step; else if (event.key === "Home") next = 0; else if (event.key === "End") next = max; if (next === null) return; event.preventDefault(); set(next); refs.current[Math.max(0, Math.ceil(Math.min(max, Math.max(0, next))) - 1)]?.focus(); }; return (
{Array.from({ length: max }, (_, i) => { const starValue = i + 1; const checked = Math.ceil(value) === starValue; return ( ); })} {name && }
); } /** * แสดงคะแนนเฉลี่ย: ดาว + ตัวเลข + จำนวนรีวิว (อ่านออกเสียง "4.5 จาก 5 ดาว") * * */ export function RatingDisplay({ value, max = 5, count, size = "sm", className, }: { value: number; max?: number; count?: number; size?: RatingSize; className?: string; }) { const text = useUiText().rating; const rounded = Math.round(value * 2) / 2; return ( {value.toFixed(1)} {count !== undefined && ( ({(text?.reviews ?? "{count} รีวิว").replace("{count}", count.toLocaleString())}) )} ); }