From ae41a249dbaa204d70c9ebcb79466c48459cfcf3 Mon Sep 17 00:00:00 2001 From: Flaminia Cavallo Date: Wed, 27 Nov 2024 13:00:59 +0100 Subject: [PATCH] feat: handle empty validation on handlechange --- src/components/form/fields/DateField.tsx | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/components/form/fields/DateField.tsx b/src/components/form/fields/DateField.tsx index 79fbda05..05a9079d 100644 --- a/src/components/form/fields/DateField.tsx +++ b/src/components/form/fields/DateField.tsx @@ -33,24 +33,22 @@ export function DateField({ const { input, meta } = useField(name) - useEffect(() => { - if (input.value === '' && meta.touched && required) { + const handleChange: CalendarInputProps['onDateSelect'] = ( + payload: { + calendarDateString: string + validation?: ValidationProps + } | null + ) => { + if (!payload?.calendarDateString && required) { setValidation({ error: true, valid: false, validationCode: 'EMPTY', validationText: i18n.t('Required'), }) + } else { + setValidation(payload?.validation || { error: false }) } - }, [required, input.value, meta.touched]) - - const handleChange: CalendarInputProps['onDateSelect'] = ( - payload: { - calendarDateString: string - validation?: ValidationProps - } | null - ) => { - setValidation(payload?.validation || { error: false }) input.onChange(payload?.calendarDateString || '') input.onBlur() }