'use client' import React, { useState, useEffect } from 'react' import { ChevronLeftIcon, ChevronRightIcon, Cross2Icon } from '@radix-ui/react-icons' interface DateRange { start: string end: string } interface DatePickerProps { isOpen: boolean onClose: () => void onApply: (range: DateRange) => void initialRange: DateRange } export default function DatePicker({ isOpen, onClose, onApply, initialRange }: DatePickerProps) { const [startDate, setStartDate] = useState(new Date(initialRange.start)) const [endDate, setEndDate] = useState(new Date(initialRange.end)) const [currentMonth, setCurrentMonth] = useState(new Date(initialRange.end)) const [selectingStart, setSelectingStart] = useState(true) useEffect(() => { if (isOpen) { setStartDate(new Date(initialRange.start)) setEndDate(new Date(initialRange.end)) setCurrentMonth(new Date(initialRange.end)) } }, [isOpen, initialRange]) if (!isOpen) return null const getDaysInMonth = (date: Date) => { const year = date.getFullYear() const month = date.getMonth() const days = new Date(year, month + 1, 0).getDate() const firstDay = new Date(year, month, 1).getDay() return { days, firstDay } } const { days, firstDay } = getDaysInMonth(currentMonth) const handleDateClick = (day: number) => { const clickedDate = new Date(currentMonth.getFullYear(), currentMonth.getMonth(), day) if (selectingStart) { setStartDate(clickedDate) // If clicked date is after current end date, reset end date if (clickedDate > endDate) { setEndDate(clickedDate) } setSelectingStart(false) } else { if (clickedDate < startDate) { setStartDate(clickedDate) setSelectingStart(false) // Keep selecting start effectively if they clicked before start } else { setEndDate(clickedDate) setSelectingStart(true) // Reset to start for next interaction or just done } } } const handleMonthChange = (increment: number) => { const newMonth = new Date(currentMonth) newMonth.setMonth(newMonth.getMonth() + increment) setCurrentMonth(newMonth) } const formatDate = (date: Date) => date.toISOString().split('T')[0] const isSelected = (day: number) => { const date = new Date(currentMonth.getFullYear(), currentMonth.getMonth(), day) return ( date.getTime() === startDate.getTime() || date.getTime() === endDate.getTime() ) } const isInRange = (day: number) => { const date = new Date(currentMonth.getFullYear(), currentMonth.getMonth(), day) return date > startDate && date < endDate } const isToday = (day: number) => { const today = new Date() const date = new Date(currentMonth.getFullYear(), currentMonth.getMonth(), day) return ( date.getDate() === today.getDate() && date.getMonth() === today.getMonth() && date.getFullYear() === today.getFullYear() ) } return (

Select Date Range

{currentMonth.toLocaleDateString('en-US', { month: 'long', year: 'numeric' })}
{['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'].map(day => (
{day}
))}
{Array.from({ length: firstDay }).map((_, i) => (
))} {Array.from({ length: days }).map((_, i) => { const day = i + 1 const selected = isSelected(day) const inRange = isInRange(day) const today = isToday(day) return ( ) })}
{startDate.toLocaleDateString()} {' - '} {endDate.toLocaleDateString()}
) }