import { useState } from 'react'
import { format, startOfMonth, endOfMonth, eachDayOfInterval, isSameMonth, isSameDay, isToday, addMonths, subMonths } from 'date-fns'
import { es } from 'date-fns/locale'
import { ChevronLeft, ChevronRight } from 'lucide-react'
interface DatePickerProps {
selectedDate: Date | null
onDateSelect: (date: Date) => void
minDate?: Date
disabled?: boolean
}
export default function DatePicker({ selectedDate, onDateSelect, minDate, disabled }: DatePickerProps) {
const [currentMonth, setCurrentMonth] = useState(selectedDate ? new Date(selectedDate) : new Date())
const days = eachDayOfInterval({
start: startOfMonth(currentMonth),
end: endOfMonth(currentMonth)
})
const previousMonth = () => setCurrentMonth(subMonths(currentMonth,1))
const nextMonth = () => setCurrentMonth(addMonths(currentMonth,1))
const isDateDisabled = (date: Date) => {
if (minDate) {
return date < minDate
}
return false
}
const isDateSelected = (date: Date) => {
return selectedDate && isSameDay(date, selectedDate)
}
// Calcular el offset del primer día del mes
// getDay() devuelve: 0=Domingo, 1=Lunes, 2=Martes, ..., 6=Sábado
// Para calendario que empieza en Lunes, necesitamos ajustar:
// Si getDay() = 0 (Domingo), offset = 6
// Si getDay() = 1-6 (Lunes-Sábado), offset = getDay() - 1
const firstDayOfMonth = startOfMonth(currentMonth)
const dayOfWeek = firstDayOfMonth.getDay()
const offset = dayOfWeek === 0 ? 6 : dayOfWeek - 1
// Crear array con celdas vacías al inicio para el padding
const paddingDays = Array.from({ length: offset }, (_, i) => ({ day: null, key: `padding-${i}` }))
// Crear array de días con key único
const calendarDays = days.map((date, i) => ({ day: date, key: `day-${i}` }))
// Combinar padding + días del mes
const allDays = [...paddingDays, ...calendarDays]
return (
{format(currentMonth, 'MMMM yyyy', { locale: es })}
{['L', 'M', 'X', 'J', 'V', 'S', 'D'].map((day, index) => (
{day}
))}
{allDays.map(({ day, key }) => {
// Si es celda de padding (day es null)
if (!day) {
return (
)
}
const disabled = isDateDisabled(day)
const selected = isDateSelected(day)
const today = isToday(day)
const notCurrentMonth = !isSameMonth(day, currentMonth)
return (
)
})}
)
}
return false
}
const isDateSelected = (date: Date) => {
return selectedDate && isSameDay(date, selectedDate)
}
return (
{format(currentMonth, 'MMMM yyyy', { locale: es })}
{['L', 'M', 'X', 'J', 'V', 'S', 'D'].map((day, index) => (
{day}
))}
{days.map((date, index) => {
const disabled = isDateDisabled(date)
const selected = isDateSelected(date)
const today = isToday(date)
const notCurrentMonth = !isSameMonth(date, currentMonth)
return (
)
})}
)
}