import { Button, Calendar, Field, FieldDescription, FieldError, FormControl, FormField, Popover, PopoverContent, PopoverTrigger, } from "@repo/shadcn-ui/components"; import { cn } from "@repo/shadcn-ui/lib/utils"; import { format, isValid, parseISO } from "date-fns"; import { CalendarIcon, LockIcon } from "lucide-react"; import React from "react"; import { type FieldPath, type FieldValues, useFormContext } from "react-hook-form"; import { FormFieldLabel } from "./form-field-label.tsx"; type DatePickerFieldProps = { name: FieldPath; label?: string; description?: string; disabled?: boolean; required?: boolean; readOnly?: boolean; placeholder?: string; orientation?: "vertical" | "horizontal" | "responsive"; className?: string; inputClassName?: string; formatDateFn?: (value: string) => string; }; const parseFieldDate = (value?: string): Date | undefined => { if (!value) return undefined; const parsed = parseISO(value); if (!isValid(parsed)) return undefined; return parsed; }; const toDateOnlyString = (date: Date): string => { return format(date, "yyyy-MM-dd"); }; export function DatePickerField({ name, label, placeholder, description, disabled = false, required = false, readOnly = false, orientation = "vertical", className, inputClassName, formatDateFn = (value) => { const parsed = parseFieldDate(value); return parsed ? format(parsed, "dd/MM/yyyy") : value; }, }: DatePickerFieldProps) { const triggerId = React.useId(); const { control, formState } = useFormContext(); const isDisabled = Boolean(disabled || readOnly || formState.isSubmitting); return ( { const selectedDate = parseFieldDate(field.value); const displayValue = field.value ? formatDateFn(field.value) : null; return ( {label ? ( {label} ) : null} {isDisabled ? null : ( { field.onChange(date ? toDateOnlyString(date) : ""); field.onBlur(); }} selected={selectedDate} /> )} {description ? ( {description} ) : (