import { usePage } from "@inertiajs/react"; import { enUS, fr } from "date-fns/locale"; import { CalendarIcon, RefreshCcw } from "lucide-react"; import { type ReactNode } from "react"; import InputError from "@/back/Components/InputError"; import { Button } from "@/components/ui/button"; import { Calendar } from "@/components/ui/calendar"; import { Label } from "@/components/ui/label"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area"; import { Separator } from "@/components/ui/separator"; import str from "@/hooks/use-string"; import trans from "@/hooks/use-translations"; import { cn, formatDate } from "@/lib/utils"; import useI18nStore from "@/store/i18n-store"; interface SimpleDatePickerProps { name: string; placeholder?: string; date?: Date | null; required?: boolean; disabled?: boolean; className?: string; label: ReactNode | string; hasTime?: boolean; parentErrors?: Record | null; setData: (name: string, value: Date | null) => void; } export default function SimpleDatePicker({ name, placeholder = "Sélectionner une date", date, required = false, disabled = false, className, label, hasTime = false, parentErrors = null, setData, }: Readonly) { const { errors } = usePage().props; const localErrors = parentErrors ?? errors; const { locale: currentLocale, timezone: currentTimezone } = useI18nStore.getState(); const calendarLocale = currentLocale === "en" ? enUS : fr; const parsedDate = date ? new Date(date) : undefined; const hours = Array.from({ length: 24 }, (_, i) => i); const minutes = Array.from({ length: 12 }, (_, i) => i * 5); const handleDateSelect = (selectedDate: Date | undefined) => { if (selectedDate) { setData(name, selectedDate); } }; const resetDate = () => { setData(name, null); }; function handleTimeChange(type: "hour" | "minute", value: string) { const currentDate = parsedDate ?? new Date(); const newDate = new Date(currentDate); if (type === "hour") { const hour = Number.parseInt(value, 10); newDate.setHours(hour); } else { newDate.setMinutes(Number.parseInt(value, 10)); } setData(name, newDate); } return (
{hasTime && (
{hours.map((hour) => ( ))}
{minutes.map((minute) => ( ))}
)}
); }