Skip to content

Commit

Permalink
fix(a11y): Use role="alert" as default value for ErrorWrapper (#2974
Browse files Browse the repository at this point in the history
)
  • Loading branch information
DafyddLlyr authored Apr 3, 2024
1 parent 5b642e6 commit 77a5056
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ it("should not have any accessibility violations whilst in the error state", asy
// Main ErrorWrapper does display, and is in error state
await waitFor(() => expect(mainErrorMessage).not.toBeEmptyDOMElement());
const [mainErrorWrapper, ..._rest] = screen.getAllByTestId("error-wrapper");
expect(mainErrorWrapper).toHaveAttribute("role", "status");
expect(mainErrorWrapper).toHaveAttribute("role", "alert");

const results = await axe(container);
expect(results).toHaveNoViolations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,5 @@ it("should change the role of the ErrorWrapper when an invalid input is given",
user.click(screen.getByTestId("continue-button"));

expect(errorWrapper).not.toBeEmptyDOMElement();
expect(errorWrapper).toHaveAttribute("role", "status");
expect(errorWrapper).toHaveAttribute("role", "alert");
});
13 changes: 10 additions & 3 deletions editor.planx.uk/src/ui/shared/ErrorWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export interface Props {
error?: string;
children?: ReactElement;
id?: string;
// "alert" - important and time-sensitive information (e.g. invalid input feedback)
// "status" - advisory information which does not interrupt focus (e.g. file upload status)
role?: "alert" | "status";
}

const Root = styled(Box, {
Expand All @@ -35,7 +38,12 @@ const ErrorText = styled(Typography)(({ theme }) => ({
fontWeight: FONT_WEIGHT_SEMI_BOLD,
}));

export default function ErrorWrapper({ id, error, children }: Props): FCReturn {
export default function ErrorWrapper({
id,
error,
children,
role = "alert",
}: Props): FCReturn {
const inputId = id ? `${ERROR_MESSAGE}-${id}` : undefined;
const { trackInputErrors } = useAnalyticsTracking();

Expand All @@ -44,8 +52,7 @@ export default function ErrorWrapper({ id, error, children }: Props): FCReturn {
}, [error, trackInputErrors]);

return (
// role="status" immediately announces the error to screenreaders without interrupting focus
<Root error={error} role="status" data-testid="error-wrapper">
<Root error={error} role={role} data-testid="error-wrapper">
<ErrorText id={inputId} data-testid={inputId} variant="body1">
{error && error}
</ErrorText>
Expand Down

0 comments on commit 77a5056

Please sign in to comment.