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

Feat/add aria required for screen readers #1551

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions app/components/inputs/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const Select = ({
aria-invalid={error !== undefined}
aria-describedby={error && errorId}
aria-errormessage={error && errorId}
aria-required={!!errorMessages?.find((err) => err.code === "required")}
>
{placeholder && <option value="">{placeholder}</option>}
{options.map((option) => {
Expand Down
1 change: 1 addition & 0 deletions app/components/inputs/Textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const Textarea = ({
aria-invalid={error !== undefined}
aria-describedby={error && errorId}
aria-errormessage={error && errorId}
aria-required={!!errorMessages?.find((err) => err.code === "required")}
/>
<InputError id={errorId}>
{errorMessages?.find((err) => err.code === error)?.text ?? error}
Expand Down
32 changes: 31 additions & 1 deletion app/components/inputs/__test__/Select.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from "@testing-library/react";
import { render, screen } from "@testing-library/react";
import * as remixValidatedForm from "remix-validated-form";
import type { SelectProps } from "../Select";
import Select from "../Select";
Expand Down Expand Up @@ -57,4 +57,34 @@ describe("Select", () => {
);
},
);

describe("Select field with aria-required attribute", () => {
it("has aria-required attribute set to true if errorMessages contain inputRequired", () => {
render(
<Select
name="select"
options={[]}
label="Test Label"
formId="formId"
errorMessages={[{ code: "required", text: "error" }]}
/>,
);
const element = screen.getByRole("combobox");
expect(element).toHaveAttribute("aria-required", "true");
});

it("has aria-required attribute set to false if errorMessages do not contain inputRequired", () => {
render(
<Select
name="select"
options={[]}
label="Test Label"
formId="formId"
errorMessages={undefined}
/>,
);
const element = screen.getByRole("combobox");
expect(element).toHaveAttribute("aria-required", "false");
});
});
});
22 changes: 22 additions & 0 deletions app/components/inputs/__test__/Textarea.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,26 @@ describe("Textarea component", () => {

expect(textarea.getAttribute("maxLength")).toBe(maxLength.toString());
});

describe("Textarea field with aria-required attribute", () => {
it("has aria-required attribute set to true if errorMessages contain inputRequired", () => {
render(
<Textarea
name="test"
errorMessages={[{ code: "required", text: "error" }]}
formId="formId"
/>,
);
const element = screen.getByRole("textbox");
expect(element).toHaveAttribute("aria-required", "true");
});

it("has aria-required attribute set to false if errorMessages do not contain inputRequired", () => {
render(
<Textarea name="test" errorMessages={undefined} formId="formId" />,
);
const element = screen.getByRole("textbox");
expect(element).toHaveAttribute("aria-required", "false");
});
});
});
Loading