import { FC, useEffect, useState } from 'react'; import { format } from 'date-fns'; import { CalendarIcon } from '@radix-ui/react-icons'; import { cn } from '@marzneshin/utils'; import { useFormContext, FieldValues } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { Popover, PopoverTrigger, FormControl, Button, Calendar, PopoverContent, FormLabel, FormItem, FormField, FormMessage } from '@marzneshin/components'; interface DateFieldProps { name: keyof FieldValues; label: string; } function parseISODate(isoDateString: string | undefined): Date | undefined { if (!isoDateString) return undefined; const date = new Date(isoDateString); return isNaN(date.getTime()) ? undefined : date; } export const DateField: FC = ({ name, label }) => { const { t } = useTranslation(); const form = useFormContext(); const [selectedDate, setSelectedDate] = useState(parseISODate(form.getValues(name))); useEffect(() => { const newValue = selectedDate?.toISOString().slice(0, -5); const prevValue = form.getValues(name); if (newValue !== prevValue) { form.setValue(name, newValue, { shouldValidate: true, shouldTouch: true }); } }, [form, name, selectedDate]); return ( ( {t(label)} { setSelectedDate(date); }} disabled={(date) => date < new Date() } initialFocus /> )} /> ); };