Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [open-formulieren/open-forms#4659] Ensure that textfield have a valid empty value #184

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading