-
I have the following component in my React app: const DateField = ({ name }: { name: string }) => {
const { control } = useFormContext();
return (
<Controller
name={name}
control={control}
render={({ field: { onChange, value } }) => (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DatePicker
format="YYYY-DD-MM"
value={value || dayjs()}
onChange={onChange}
/>
</LocalizationProvider>
)}
/>
);
}; My problem is that my interface saves the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I use something very similar to this and rather than storing the value as a Date object and then converting it as part of the submit handler, I store the value as a string.
My helper methods are transformTimeOutput and transformTimeInput.
That is my approach to it, maybe someone else can comment and we can have a different perspective. |
Beta Was this translation helpful? Give feedback.
-
So this ended up working but my problem now is that the |
Beta Was this translation helpful? Give feedback.
I use something very similar to this and rather than storing the value as a Date object and then converting it as part of the submit handler, I store the value as a string.
So my value and my onChange method house the logic for the conversion.
I use the hours from the date object as I am using a time picker, but I am sure you can apply similar logic for dates.