Skip to content

Commit

Permalink
🐛 [open-formulieren/open-forms#4659] Ensure that textfield have a val…
Browse files Browse the repository at this point in the history
…id empty value
  • Loading branch information
robinmolen committed Oct 2, 2024
1 parent c768a2d commit fbb4b1e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/components/formio/textfield.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -35,7 +35,7 @@ export const TextField: React.FC<JSX.IntrinsicElements['input'] & TextFieldProps
childrenAfterField,
...props
}) => {
const {getFieldProps, getFieldMeta} = useFormikContext();
const {getFieldProps, getFieldMeta, setFieldValue} = useFormikContext();
const {value, onChange: formikOnChange} = getFieldProps<string | undefined>(name);
const {touched} = getFieldMeta<string | undefined>(name);
const {errors, hasErrors} = useValidationErrors(name);
Expand All @@ -48,6 +48,13 @@ export const TextField: React.FC<JSX.IntrinsicElements['input'] & TextFieldProps
props = {...props, value: ''};
}

useEffect(() => {
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.
Expand Down

0 comments on commit fbb4b1e

Please sign in to comment.