import { parseDate } from "@internationalized/date"; import { Controller, type FieldPath, type FieldValues, useFormContext, } from "react-hook-form"; import { dateToYYYYMMDD } from "../../utils/dates"; import type { DayMonthYear } from "../../utils/zod"; import { SendouDatePicker } from "../elements/DatePicker"; import type { FormFieldSize } from "./form-utils"; export function DateFormField({ label, name, bottomText, required, size, }: { label: string; name: FieldPath; bottomText?: string; required?: boolean; size?: FormFieldSize; }) { const methods = useFormContext(); return ( { const getValue = () => { const originalValue = value as DayMonthYear | null; if (!originalValue) return null; const isoString = dateToYYYYMMDD( new Date( Date.UTC( originalValue.year, originalValue.month, originalValue.day, 12, ), ), ); return parseDate(isoString); }; return ( { if (value) { onChange({ day: value.day, month: value.month - 1, year: value.year, }); } if (!value) { onChange(null); } }} bottomText={bottomText} /> ); }} /> ); }