Skip to content

Commit

Permalink
♿ [open-formulieren/open-forms#4716] Connect input to error message u…
Browse files Browse the repository at this point in the history
…sing aria-describedby
  • Loading branch information
robinmolen authored and sergei-maertens committed Oct 23, 2024
1 parent 96a39a5 commit 608b1dc
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/components/forms/Checkbox/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ const Checkbox = ({
invalid={invalid}
required={isRequired}
appearance="custom"
aria-describedby={invalid ? `${id}-error-message` : undefined}
{...inputProps}
/>
<HelpText>{description}</HelpText>
{touched && <ValidationErrors error={error} />}
{touched && <ValidationErrors error={error} inputId={id} />}
</FormField>
);
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/forms/NumberField/NumberField.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ const NumberField = ({
type={useNumberType ? 'number' : 'text'}
customInput={Textbox}
readOnly={readOnly}
aria-describedby={invalid ? `${id}-error-message` : undefined}
{...separatorProps}
/>
</Paragraph>
<HelpText>{description}</HelpText>
{touched && <ValidationErrors error={error} />}
{touched && <ValidationErrors error={error} inputId={id} />}
</UtrechtFormField>
);
};
Expand Down
4 changes: 3 additions & 1 deletion src/components/forms/RadioField/RadioField.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const RadioField = ({
const {error, touched} = getFieldMeta(name);
const invalid = touched && !!error;
const descriptionid = `${id}-description`;
// @TODO check aria descriptions with screen reader... Is it still usable, with the description and error messages?
return (
<Fieldset
className="utrecht-form-fieldset--openforms"
Expand All @@ -58,6 +59,7 @@ export const RadioField = ({
id={`${id}-opt-${index}`}
name={name}
value={optionValue}
aria-describedby={invalid ? `${id}-error-message` : undefined}
{...inputProps}
/>
<Paragraph className="utrecht-form-field__label utrecht-form-field__label--radio">
Expand All @@ -74,7 +76,7 @@ export const RadioField = ({
))}

<HelpText id={descriptionid}>{description}</HelpText>
{touched && <ValidationErrors error={error} />}
{touched && <ValidationErrors error={error} inputId={id} />}
</Fieldset>
);
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/forms/SelectField/SelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ const SelectField = ({
}}
value={value}
onBlur={() => setTouched(true)}
aria-describedby={invalid ? `${id}-error-message` : undefined}
/>
<HelpText>{description}</HelpText>
{touched && <ValidationErrors error={error} />}
{touched && <ValidationErrors error={error} inputId={id} />}
</FormField>
);
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/forms/TextField/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ export const TextField = ({
className="utrecht-textbox--openforms"
disabled={disabled}
invalid={invalid}
aria-describedby={invalid ? `${id}-error-message` : undefined}
{...inputProps}
/>
</Paragraph>
<HelpText>{description}</HelpText>
{touched && <ValidationErrors error={error} />}
{touched && <ValidationErrors error={error} inputId={id} />}
</UtrechtFormField>
);
};
Expand Down
9 changes: 7 additions & 2 deletions src/components/forms/ValidationErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ import {FormFieldDescription} from '@utrecht/component-library-react';
import PropTypes from 'prop-types';
import React from 'react';

const ValidationErrors = ({error = ''}) => {
const ValidationErrors = ({error = '', inputId = ''}) => {
if (!error) return null;
return (
<FormFieldDescription invalid className="utrecht-form-field-description--openforms-errors">
<FormFieldDescription
id={`${inputId}-error-message`}
invalid
className="utrecht-form-field-description--openforms-errors"
>
{error}
</FormFieldDescription>
);
};

ValidationErrors.propTypes = {
error: PropTypes.string,
inputId: PropTypes.string,
};

export default ValidationErrors;
7 changes: 7 additions & 0 deletions src/formio/components/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class TextField extends Formio.Components.components.textfield {
return info;
}

checkValidity(data, dirty, row, silentCheck) {
const validity = super.checkValidity(data, dirty, row, silentCheck);
console.log({data, dirty, row, validity});
// info.attr['aria-describedby'] = '';
return validity;
}

checkComponentValidity(data, dirty, row, options = {}) {
let updatedOptions = {...options};
if (this.component.validate.plugins && this.component.validate.plugins.length) {
Expand Down

0 comments on commit 608b1dc

Please sign in to comment.