'use client' import { useState, useEffect } from 'react' import { ChevronLeft, ChevronRight } from 'lucide-react' type DateTimePickerPopupProps = { value: Date | null onChange: (date: Date) => void type: 'date' | 'time' | 'datetime-local' onClose?: () => void anchorName?: string } const DAYS = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'] const MONTHS = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] export default function DateTimePickerPopup({ value, onChange, type, onClose, anchorName, }: DateTimePickerPopupProps) { const [currentDate, setCurrentDate] = useState(new Date()) const [timeInput, setTimeInput] = useState({ hours: value ? value.getHours().toString() : '0', minutes: value ? value.getMinutes().toString() : '0', }) useEffect(() => { if (value) { setCurrentDate(value) setTimeInput(prev => ({ hours: prev.hours === '' && value.getHours() === 0 ? '' : value.getHours().toString(), minutes: prev.minutes === '' && value.getMinutes() === 0 ? '' : value.getMinutes().toString(), })) } }, [value]) function handleDateSelect(day: number) { const newDate = new Date(currentDate) newDate.setDate(day) if (value) { newDate.setHours(value.getHours()) newDate.setMinutes(value.getMinutes()) } else { newDate.setHours(0, 0, 0, 0) } onChange(newDate) if (type === 'date' && onClose) { onClose() } } function handleTimeChange(timeUnit: 'hours' | 'minutes', val: number) { const newDate = value ? new Date(value) : new Date() if (!value) { newDate.setHours(0, 0, 0, 0) } if (timeUnit === 'hours') { if (val < 0 || val > 23) return newDate.setHours(val) } if (timeUnit === 'minutes') { if (val < 0 || val > 59) return newDate.setMinutes(val) } onChange(newDate) } function onTimeInputChange(unit: 'hours' | 'minutes', val: string) { if (val === '') { setTimeInput(prev => ({ ...prev, [unit]: '' })) handleTimeChange(unit, 0) return } if (!/^\d+$/.test(val)) return const num = parseInt(val) if (unit === 'hours' && num > 23) return if (unit === 'minutes' && num > 59) return setTimeInput(prev => ({ ...prev, [unit]: val })) handleTimeChange(unit, num) } function onTimeInputBlur(unit: 'hours' | 'minutes') { if (timeInput[unit] === '') { const num = unit === 'hours' ? (value?.getHours() ?? 0) : (value?.getMinutes() ?? 0) setTimeInput(prev => ({ ...prev, [unit]: num.toString() })) } } function getDaysInMonth(year: number, month: number) { return new Date(year, month + 1, 0).getDate() } function getFirstDayOfMonth(year: number, month: number) { return new Date(year, month, 1).getDay() } function renderCalendar() { const year = currentDate.getFullYear() const month = currentDate.getMonth() const daysInMonth = getDaysInMonth(year, month) const firstDay = getFirstDayOfMonth(year, month) const days = [] const adjustedFirstDay = firstDay === 0 ? 6 : firstDay - 1 for (let i = 0; i < adjustedFirstDay; i++) { days.push(
) } for (let i = 1; i <= daysInMonth; i++) { const isSelected = value && value.getDate() === i && value.getMonth() === month && value.getFullYear() === year const isToday = new Date().getDate() === i && new Date().getMonth() === month && new Date().getFullYear() === year days.push( ) } return (