Skip to content

Commit fbb4b1e

Browse files
committed
🐛 [open-formulieren/open-forms#4659] Ensure that textfield have a valid empty value
1 parent c768a2d commit fbb4b1e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/components/formio/textfield.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import clsx from 'clsx';
22
import {Field, useFormikContext} from 'formik';
3-
import {useContext, useRef} from 'react';
3+
import {useContext, useEffect, useRef} from 'react';
44

55
import {RenderContext} from '@/context';
66
import CharCount from '@/utils/charcount';
@@ -35,7 +35,7 @@ export const TextField: React.FC<JSX.IntrinsicElements['input'] & TextFieldProps
3535
childrenAfterField,
3636
...props
3737
}) => {
38-
const {getFieldProps, getFieldMeta} = useFormikContext();
38+
const {getFieldProps, getFieldMeta, setFieldValue} = useFormikContext();
3939
const {value, onChange: formikOnChange} = getFieldProps<string | undefined>(name);
4040
const {touched} = getFieldMeta<string | undefined>(name);
4141
const {errors, hasErrors} = useValidationErrors(name);
@@ -48,6 +48,13 @@ export const TextField: React.FC<JSX.IntrinsicElements['input'] & TextFieldProps
4848
props = {...props, value: ''};
4949
}
5050

51+
useEffect(() => {
52+
if (value === undefined || value === null) {
53+
// Make sure value is valid
54+
setFieldValue(name, '');
55+
}
56+
}, [value]);
57+
5158
// XXX: this is only accepted in the form builder to get to (close to) vanilla form
5259
// builder feature parity - setting the value with mask placeholders is bad for
5360
// accessibility.

0 commit comments

Comments
 (0)