From 403e1e7cf8384732a2d4c8584b8de57d70b3edce Mon Sep 17 00:00:00 2001 From: Christel Westerberg Date: Mon, 2 Dec 2024 13:54:32 +0100 Subject: [PATCH] fix: handle default values in date select --- .../TempDesignSystem/Form/Date/index.tsx | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/components/TempDesignSystem/Form/Date/index.tsx b/components/TempDesignSystem/Form/Date/index.tsx index fa4b27528..ea1d5e672 100644 --- a/components/TempDesignSystem/Form/Date/index.tsx +++ b/components/TempDesignSystem/Form/Date/index.tsx @@ -76,19 +76,17 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) { } } - if (year && month && day) { - const newDate = dt() - .year(Number(year)) - .month(Number(month) - 1) - .date(Number(day)) + const newDate = dt() + .year(Number(year)) + .month(Number(month) - 1) + .date(Number(day)) - if (newDate.isValid()) { - setValue(name, newDate.format("YYYY-MM-DD"), { - shouldDirty: true, - shouldTouch: true, - shouldValidate: true, - }) - } + if (newDate.isValid()) { + setValue(name, newDate.format("YYYY-MM-DD"), { + shouldDirty: true, + shouldTouch: true, + shouldValidate: true, + }) } }, [year, month, day, setValue, name, formState.isSubmitting]) @@ -106,6 +104,16 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) { console.warn("Known error for parse date in DateSelect: ", error) } + useEffect(() => { + if (formState.isSubmitting) return + + if (!(day && month && year) && dateValue) { + setValue(DateName.day, Number(dateValue.day)) + setValue(DateName.month, Number(dateValue.month)) + setValue(DateName.year, Number(dateValue.year)) + } + }, [setValue, formState.isSubmitting, dateValue, day, month, year]) + return (