From 733b1526d54cdf48204af2d66e4970ee7d4235a3 Mon Sep 17 00:00:00 2001 From: robinvandermolen Date: Thu, 26 Sep 2024 12:33:47 +0200 Subject: [PATCH] :bug: [open-formulieren/open-forms#4659] Ensure that textfield have a valid empty value --- src/components/formio/textfield.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/components/formio/textfield.tsx b/src/components/formio/textfield.tsx index 082a210..cd21c2b 100644 --- a/src/components/formio/textfield.tsx +++ b/src/components/formio/textfield.tsx @@ -1,6 +1,6 @@ import clsx from 'clsx'; import {Field, useFormikContext} from 'formik'; -import {useContext, useRef} from 'react'; +import {useContext, useEffect, useRef} from 'react'; import {RenderContext} from '@/context'; import CharCount from '@/utils/charcount'; @@ -35,7 +35,7 @@ export const TextField: React.FC { - const {getFieldProps, getFieldMeta} = useFormikContext(); + const {getFieldProps, getFieldMeta, setFieldValue} = useFormikContext(); const {value, onChange: formikOnChange} = getFieldProps(name); const {touched} = getFieldMeta(name); const {errors, hasErrors} = useValidationErrors(name); @@ -48,6 +48,14 @@ export const TextField: React.FC { + if (value === undefined || value === null) { + // Make sure value is valid + setFieldValue(name, ''); + } + }, [value]); + + // XXX: this is only accepted in the form builder to get to (close to) vanilla form // builder feature parity - setting the value with mask placeholders is bad for // accessibility.