Skip to content

Commit

Permalink
feat(Textarea): add aria-invalid and aria-describedby
Browse files Browse the repository at this point in the history
Accessibility properties added to the correct states
  • Loading branch information
DSil authored and mainframev committed Jan 5, 2023
1 parent eb49bb4 commit 05f955a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Expand Up @@ -44,6 +44,7 @@ describe("Textarea", () => {
expect(textarea).toHaveAttribute("maxlength", maxLength.toString());
expect(textarea).toHaveAttribute("rows", "4");
expect(textarea).toHaveAttribute("name", name);
expect(textarea).not.toBeInvalid();
expect(textarea.parentElement).toHaveStyle({ marginBottom: "12px" });
expect(textarea).toHaveStyle({ padding: "12px" });

Expand All @@ -64,9 +65,13 @@ describe("Textarea", () => {

it("should have error", async () => {
render(<Textarea error="error" size="small" />);
const textarea = screen.getByRole("textbox");

userEvent.tab();
expect(screen.getByText("error")).toBeInTheDocument();
expect(screen.getByRole("textbox")).toHaveStyle({ padding: "8px 12px" });
expect(textarea).toHaveStyle({ padding: "8px 12px" });
expect(textarea).toBeInvalid();
expect(textarea).toHaveDescription("error");
// Needs to flush async `floating-ui` hooks
// https://github.com/floating-ui/floating-ui/issues/1520
await act(async () => {});
Expand Down
9 changes: 8 additions & 1 deletion packages/orbit-components/src/Textarea/index.tsx
Expand Up @@ -13,6 +13,7 @@ import useErrorTooltip from "../ErrorFormTooltip/hooks/useErrorTooltip";
import getFieldDataState from "../common/getFieldDataState";
import mq from "../utils/mediaQuery";
import type { Props } from "./types";
import useRandomId from "../hooks/useRandomId";

const Field = styled.label<{ fullHeight?: boolean; spaceAfter?: Common.SpaceAfterSizes }>`
font-family: ${({ theme }) => theme.orbit.fontFamily};
Expand Down Expand Up @@ -145,6 +146,9 @@ const Textarea = React.forwardRef<HTMLTextAreaElement, Props>((props, ref) => {
required,
}: Props = props;

const forID = useRandomId();
const inputId = id || forID;

const {
tooltipShown,
tooltipShownHover,
Expand Down Expand Up @@ -178,7 +182,7 @@ const Textarea = React.forwardRef<HTMLTextAreaElement, Props>((props, ref) => {
data-state={getFieldDataState(!!error)}
data-test={dataTest}
aria-required={!!required}
id={id}
id={inputId}
name={name}
value={value}
size={size}
Expand All @@ -195,8 +199,11 @@ const Textarea = React.forwardRef<HTMLTextAreaElement, Props>((props, ref) => {
tabIndex={tabIndex ? Number(tabIndex) : undefined}
readOnly={readOnly}
ref={ref}
aria-describedby={shown ? `${inputId}-feedback` : undefined}
aria-invalid={error ? true : undefined}
/>
<ErrorFormTooltip
id={`${inputId}-feedback`}
help={help}
inputSize={size}
helpClosable={helpClosable}
Expand Down

0 comments on commit 05f955a

Please sign in to comment.