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

fix (a11): add aria-invalid to career form fields #817

Merged
merged 8 commits into from
Sep 22, 2024
Merged
18 changes: 14 additions & 4 deletions src/components/forms/checkbox/checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,18 @@ const HiddenCheckbox = styled.input.attrs({
visibility: hidden;
`;

const CheckboxLabelTextWrapper = ({ label, required }) => {
const CheckboxLabelTextWrapper = ({ label, required, id }) => {
return (
<CheckboxLabelText>
<CheckboxLabelText id={id}>
{label} {required && <span aria-hidden={true}>*</span>}
</CheckboxLabelText>
);
};

export const Checkbox = (
props: UseControllerProps<any> & { label: string | JSX.Element },
props: UseControllerProps<any> & {
label: string | JSX.Element;
},
) => {
const { field, fieldState, formState } = useController(props);
const errorMessage = fieldState?.error?.message;
Expand All @@ -83,6 +85,9 @@ export const Checkbox = (
<CheckboxLabel>
<HiddenCheckbox
aria-required={true}
aria-describedby={errorMessage && `${props.name}-error`}
aria-labelledby={props.label && `${props.name}-label`}
aria-invalid={Boolean(errorMessage)}
disabled={formState.isSubmitting}
{...field}
value={field?.value || false}
Expand All @@ -95,12 +100,17 @@ export const Checkbox = (
</StyledCheckbox>
{props.label && (
<CheckboxLabelTextWrapper
id={`${props.name}-label`}
label={props.label}
required={props.rules?.required}
/>
)}
</CheckboxLabel>
{errorMessage && <StyledErrorMessage>{errorMessage}</StyledErrorMessage>}
{errorMessage && (
<StyledErrorMessage id={`${props.name}-error`}>
{errorMessage}
</StyledErrorMessage>
)}
</div>
);
};
8 changes: 6 additions & 2 deletions src/components/forms/file-dropper/file-dropper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ export const FileDropper = ({
$hasErrors={Boolean(errors?.documents)}
>
<StyledLabel>
<input {...getInputProps({ ...register(name) })} />
<input
{...getInputProps({ ...register(name) })}
aria-invalid={Boolean(errors?.documents)}
aria-describedby={errors?.documents && `${name}-error`}
/>
</StyledLabel>
{!hasFiles && illustration && (
<Illustration show={illustration} size={IllustrationSize.NORMAL} />
Expand All @@ -235,7 +239,7 @@ export const FileDropper = ({
))}

{errors?.documents && (
<StyledErrorMessage>
<StyledErrorMessage id={`${name}-error`}>
{errors.documents.message as unknown as string}
</StyledErrorMessage>
)}
Expand Down
8 changes: 7 additions & 1 deletion src/components/forms/text-area/text-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export const TextArea = (
)}
<StyledTextArea
aria-required={true}
aria-describedby={errorMessage && `${props.name}-error`}
aria-invalid={Boolean(errorMessage)}
$hasError={Boolean(errorMessage)}
disabled={formState.isSubmitting}
{...field}
Expand All @@ -56,7 +58,11 @@ export const TextArea = (
placeholder={props.placeholder}
/>

{errorMessage && <StyledErrorMessage>{errorMessage}</StyledErrorMessage>}
{errorMessage && (
<StyledErrorMessage id={`${props.name}-error`}>
{errorMessage}
</StyledErrorMessage>
)}
</div>
);
};
8 changes: 7 additions & 1 deletion src/components/forms/text-input/text-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,20 @@ export const TextInput = (
<Input
type={'text'}
aria-required={true}
aria-describedby={errorMessage && `${props.name}-error`}
aria-invalid={Boolean(errorMessage)}
$hasError={Boolean(errorMessage)}
disabled={formState.isSubmitting}
id={props.name}
virus-rpi marked this conversation as resolved.
Show resolved Hide resolved
placeholder={props.placeholder}
{...field}
value={(field?.value as string) || ''}
/>
{errorMessage && <StyledErrorMessage>{errorMessage}</StyledErrorMessage>}
{errorMessage && (
<StyledErrorMessage id={`${props.name}-error`}>
{errorMessage}
</StyledErrorMessage>
)}
</div>
);
};