({
onChange,
initialDate,
title = 'Select Date & Time',
}: Props)
| 42 | }; |
| 43 | |
| 44 | export default function DateTimePicker({ |
| 45 | onChange, |
| 46 | initialDate, |
| 47 | title = 'Select Date & Time', |
| 48 | }: Props) { |
| 49 | const scrollRef = useRef<HTMLDivElement>(null); |
| 50 | const [selectedDate, setSelectedDate] = useState<Date>( |
| 51 | roundToNearestFiveMinutes(initialDate || new Date()), |
| 52 | ); |
| 53 | |
| 54 | // Generate all time options with 5-minute intervals |
| 55 | const generateTimeOptions = () => { |
| 56 | const times = []; |
| 57 | for (let hour = 0; hour < 24; hour++) { |
| 58 | for (let minute = 0; minute < 60; minute += 5) { |
| 59 | const timeString = `${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`; |
| 60 | const displayTime = format(new Date(2000, 0, 1, hour, minute), 'HH:mm'); |
| 61 | times.push({ value: timeString, label: displayTime }); |
| 62 | } |
| 63 | } |
| 64 | return times; |
| 65 | }; |
| 66 | |
| 67 | const timeOptions = generateTimeOptions(); |
| 68 | |
| 69 | function handleDateSelect(date: Date | undefined) { |
| 70 | if (date) { |
| 71 | // Preserve the existing time when changing date |
| 72 | const newDate = new Date(date); |
| 73 | newDate.setHours(selectedDate.getHours()); |
| 74 | newDate.setMinutes(selectedDate.getMinutes()); |
| 75 | // Round to nearest 5-minute interval |
| 76 | setSelectedDate(roundToNearestFiveMinutes(newDate)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | function handleTimeSelect(timeValue: string) { |
| 81 | const [hours, minutes] = timeValue.split(':').map(Number); |
| 82 | const newDate = new Date(selectedDate); |
| 83 | newDate.setHours(hours); |
| 84 | newDate.setMinutes(minutes); |
| 85 | // Ensure alignment to 5-minute intervals (safety measure) |
| 86 | setSelectedDate(roundToNearestFiveMinutes(newDate)); |
| 87 | } |
| 88 | |
| 89 | const currentTimeValue = `${selectedDate.getHours().toString().padStart(2, '0')}:${selectedDate.getMinutes().toString().padStart(2, '0')}`; |
| 90 | |
| 91 | // Scroll to selected time when modal opens |
| 92 | useEffect(() => { |
| 93 | const buttonSize = 32; |
| 94 | const buttonMargin = 2; |
| 95 | const containerPadding = 4; |
| 96 | |
| 97 | const scrollContainer = scrollRef.current; |
| 98 | const buttonIndex = timeOptions.findIndex( |
| 99 | (time) => time.value === currentTimeValue, |
| 100 | ); |
| 101 | const calculatedScrollTo = |
nothing calls this directly
no test coverage detected