"use client"; import { useEffect, useId, useMemo, useRef, useState } from "react"; import { Icon } from "./Icon"; const THAI_MONTHS = ["มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม"]; const THAI_DAYS = ["อา.", "จ.", "อ.", "พ.", "พฤ.", "ศ.", "ส."]; const pad = (value: number) => String(value).padStart(2, "0"); function parseValue(value: string) { const match = /^(\d{4})-(\d{2})-(\d{2})(?:T(\d{2}):(\d{2}))?$/.exec(value); return match ? { year: +match[1], month: +match[2] - 1, day: +match[3], hour: +(match[4] ?? 9), minute: +(match[5] ?? 0) } : null; } function bangkokToday() { const parts = new Intl.DateTimeFormat("en-CA", { timeZone: "Asia/Bangkok", year: "numeric", month: "2-digit", day: "2-digit" }).formatToParts(new Date()); const get = (type: Intl.DateTimeFormatPartTypes) => Number(parts.find((part) => part.type === type)?.value); return { year: get("year"), month: get("month") - 1, day: get("day") }; } export function ThaiDateTimePicker({ value, onChange, dateOnly = false, disabled = false, label = "เลือกวันและเวลา" }: { value: string; onChange: (value: string) => void; dateOnly?: boolean; disabled?: boolean; label?: string }) { const id = useId(); const headingId = `${id}-heading`; const rootRef = useRef(null); const selected = parseValue(value); const [today] = useState(() => bangkokToday()); const [open, setOpen] = useState(false); const [view, setView] = useState(() => ({ year: selected?.year ?? today.year, month: selected?.month ?? today.month })); useEffect(() => { const close = (event: PointerEvent) => { if (!rootRef.current?.contains(event.target as Node)) setOpen(false); }; const escape = (event: KeyboardEvent) => { if (event.key === "Escape") setOpen(false); }; document.addEventListener("pointerdown", close); document.addEventListener("keydown", escape); return () => { document.removeEventListener("pointerdown", close); document.removeEventListener("keydown", escape); }; }, []); const days = useMemo(() => { const first = new Date(Date.UTC(view.year, view.month, 1)).getUTCDay(); const count = new Date(Date.UTC(view.year, view.month + 1, 0)).getUTCDate(); return Array.from({ length: 42 }, (_, index) => { const day = index - first + 1; return day >= 1 && day <= count ? day : null; }); }, [view]); const display = selected ? `${selected.day} ${THAI_MONTHS[selected.month]} ${selected.year + 543}${dateOnly ? "" : ` เวลา ${pad(selected.hour)}:${pad(selected.minute)} น.`}` : dateOnly ? "เลือกวันที่" : "เลือกวันและเวลา"; function pickDay(day: number) { const time = selected ?? { hour: 9, minute: 0 }; onChange(`${view.year}-${pad(view.month + 1)}-${pad(day)}${dateOnly ? "" : `T${pad(time.hour)}:${pad(time.minute)}`}`); if (dateOnly) setOpen(false); } function pickToday() { const time = selected ?? { hour: 9, minute: 0 }; setView({ year: today.year, month: today.month }); onChange(`${today.year}-${pad(today.month + 1)}-${pad(today.day)}${dateOnly ? "" : `T${pad(time.hour)}:${pad(time.minute)}`}`); if (dateOnly) setOpen(false); } function changeTime(kind: "hour" | "minute", next: number) { const date = selected ?? { ...today, hour: 9, minute: 0 }; const result = { ...date, [kind]: next }; onChange(`${result.year}-${pad(result.month + 1)}-${pad(result.day)}T${pad(result.hour)}:${pad(result.minute)}`); } function moveMonth(offset: number) { const next = new Date(Date.UTC(view.year, view.month + offset, 1)); setView({ year: next.getUTCFullYear(), month: next.getUTCMonth() }); } function togglePicker() { if (!open && selected) setView({ year: selected.year, month: selected.month }); setOpen((current) => !current); } return
{open && }
; }