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 "An input component must either have a 'aria-label' attribute..." error when a <label> is present #1230

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/forty-cougars-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@orbit-ui/components": patch
---

Update message about accessibility on input elements
2 changes: 1 addition & 1 deletion packages/components/src/number-input/src/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export function InnerNumberInput(props: InnerNumberInputProps) {
} = adaptInputStylingProps(props, contextualProps);

if (isNil(ariaLabel) && isNil(ariaLabelledBy) && isNil(placeholder)) {
console.error("An input component must either have an \"aria-label\" attribute, an \"aria-labelledby\" attribute or a \"placeholder\" attribute.");
console.error("An input component must either be wrapped inside a `<Field>` component with a `<Label>`, have an \"aria-label\" attribute, an \"aria-labelledby\" attribute or a \"placeholder\" attribute.");
}

const fluidValue = useResponsiveValue(fluid);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Field, Label } from "@components/field";
import { act, screen, waitFor, renderWithTheme } from "@test-utils";
import { NumberInput } from "@components/number-input";
import { Text } from "@components/typography";
import { createRef } from "react";
import userEvent from "@testing-library/user-event";

afterEach(() => {
jest.clearAllMocks();
});

// ***** Behaviors *****

test("accept numbers", async () => {
Expand Down Expand Up @@ -574,3 +579,44 @@ test("set ref once", async () => {

await waitFor(() => expect(handler).toHaveBeenCalledTimes(1));
});

// ***** Accessibility *****

test("logs an error when label is missing", async () => {
const spy = jest.spyOn(console, "error");

renderWithTheme(<NumberInput />);

expect(spy).toHaveBeenCalled();
});

test("does not log an error when a label is present", async () => {
const spy = jest.spyOn(console, "error");

renderWithTheme(
<Field>
<Label>Label</Label>
<NumberInput />
</Field>
);

expect(spy).not.toHaveBeenCalled();

renderWithTheme(<NumberInput id="test" aria-label="Label" />);

expect(spy).not.toHaveBeenCalled();

renderWithTheme(<NumberInput id="test" placeholder="Label" />);

expect(spy).not.toHaveBeenCalled();

renderWithTheme(
<>
<Text id="label">Label</Text>
<NumberInput aria-labelledby="label" />
</>
);

expect(spy).not.toHaveBeenCalled();
});

2 changes: 1 addition & 1 deletion packages/components/src/text-area/src/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function InnerTextArea(props: InnerTextAreaProps) {
} = adaptInputStylingProps(props, fieldProps);

if (isNil(ariaLabel) && isNil(ariaLabelledBy) && isNil(placeholder)) {
console.error("An input component must either have an \"aria-label\" attribute, an \"aria-labelledby\" attribute or a \"placeholder\" attribute.");
console.error("An input component must either be wrapped inside a `<Field>` component with a `<Label>`, have an \"aria-label\" attribute, an \"aria-labelledby\" attribute or a \"placeholder\" attribute.");
}

const fluidValue = useResponsiveValue(fluid);
Expand Down
46 changes: 46 additions & 0 deletions packages/components/src/text-area/tests/jest/TextArea.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { Field, Label } from "@components/field";
import { act, screen, waitFor, renderWithTheme } from "@test-utils";

import { TextArea } from "@components/text-area";
import { Text } from "@components/typography";
import { createRef } from "react";
import userEvent from "@testing-library/user-event";

afterEach(() => {
jest.clearAllMocks();
});

// ***** Behaviors *****

Object.defineProperty(document, "fonts", {
Expand Down Expand Up @@ -147,3 +152,44 @@ test("set ref once", async () => {

await waitFor(() => expect(handler).toHaveBeenCalledTimes(1));
});


// ***** Accessibility *****

test("logs an error when label is missing", async () => {
const spy = jest.spyOn(console, "error");

renderWithTheme(<TextArea />);

expect(spy).toHaveBeenCalled();
});

test("does not log an error when a label is present", async () => {
const spy = jest.spyOn(console, "error");

renderWithTheme(
<Field>
<Label>Label</Label>
<TextArea />
</Field>
);

expect(spy).not.toHaveBeenCalled();

renderWithTheme(<TextArea id="test" aria-label="Label" />);

expect(spy).not.toHaveBeenCalled();

renderWithTheme(<TextArea id="test" placeholder="Label" />);

expect(spy).not.toHaveBeenCalled();

renderWithTheme(
<>
<Text id="label">Label</Text>
<TextArea aria-labelledby="label" />
</>
);

expect(spy).not.toHaveBeenCalled();
});
2 changes: 1 addition & 1 deletion packages/components/src/text-input/src/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function InnerTextInput(props: InnerTextInputProps) {
} = adaptInputStylingProps(props, contextProps);

if (isNil(ariaLabel) && isNil(ariaLabelledBy) && isNil(placeholder)) {
console.error("An input component must either have an \"aria-label\" attribute, an \"aria-labelledby\" attribute or a \"placeholder\" attribute.");
console.error("An input component must either be wrapped inside a `<Field>` component with a `<Label>`, have an \"aria-label\" attribute, an \"aria-labelledby\" attribute or a \"placeholder\" attribute.");
}

const fluidValue = useResponsiveValue(fluid);
Expand Down
47 changes: 47 additions & 0 deletions packages/components/src/text-input/tests/jest/TextInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { screen, waitFor, renderWithTheme } from "@test-utils";
import { TextInput } from "@components/text-input";
import { createRef } from "react";
import userEvent from "@testing-library/user-event";
import { Text } from "@components/typography";

afterEach(() => {
jest.clearAllMocks();
});

test("when a className is provided, render the className on the input element", async () => {
renderWithTheme(
Expand Down Expand Up @@ -187,3 +192,45 @@ test("set ref once", async () => {

await waitFor(() => expect(handler).toHaveBeenCalledTimes(1));
});

// ***** Accessibility *****

test("logs an error when label is missing", async () => {
const spy = jest.spyOn(console, "error");

renderWithTheme(<TextInput />);

expect(spy).toHaveBeenCalled();
});

test("does not log an error when a label is present", async () => {
const spy = jest.spyOn(console, "error");

renderWithTheme(
<Field>
<Label>Label</Label>
<TextInput />
</Field>
);

expect(spy).not.toHaveBeenCalled();


renderWithTheme(<TextInput id="test" aria-label="Label" />);

expect(spy).not.toHaveBeenCalled();

renderWithTheme(<TextInput id="test" placeholder="Label" />);

expect(spy).not.toHaveBeenCalled();

renderWithTheme(
<>
<Text id="label">Label</Text>
<TextInput aria-labelledby="label" />
</>
);

expect(spy).not.toHaveBeenCalled();
});