Fix TextField ARIA attributes
This commit is contained in:
parent
fce03b7531
commit
46cc41221d
@ -7,16 +7,22 @@ export type TextFieldInputProps = JSX.InputHTMLAttributes<HTMLInputElement>;
|
|||||||
|
|
||||||
/** The input element. Automatically wired to Label, Description, and ErrorMessage. */
|
/** The input element. Automatically wired to Label, Description, and ErrorMessage. */
|
||||||
export function TextFieldInput(props: TextFieldInputProps): JSX.Element {
|
export function TextFieldInput(props: TextFieldInputProps): JSX.Element {
|
||||||
const [local, rest] = splitProps(props, ["id", "aria-describedby"]);
|
const [local, rest] = splitProps(props, [
|
||||||
|
"id",
|
||||||
|
"aria-describedby",
|
||||||
|
"aria-invalid",
|
||||||
|
"aria-required",
|
||||||
|
"disabled",
|
||||||
|
]);
|
||||||
const ctx = useTextFieldContext();
|
const ctx = useTextFieldContext();
|
||||||
return (
|
return (
|
||||||
<input
|
<input
|
||||||
id={local.id ?? ctx.inputId()}
|
id={local.id ?? ctx.inputId()}
|
||||||
aria-describedby={local["aria-describedby"] ?? ctx.descriptionId()}
|
aria-describedby={local["aria-describedby"] ?? ctx.descriptionId()}
|
||||||
aria-errormessage={ctx.invalid() ? ctx.errorMessageId() : undefined}
|
aria-errormessage={ctx.invalid() ? ctx.errorMessageId() : undefined}
|
||||||
aria-invalid={ctx.invalid() || undefined}
|
aria-invalid={ctx.invalid() ? "true" : undefined}
|
||||||
aria-required={ctx.required() || undefined}
|
aria-required={ctx.required() ? "true" : undefined}
|
||||||
disabled={ctx.disabled()}
|
disabled={local.disabled ?? ctx.disabled()}
|
||||||
{...rest}
|
{...rest}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -75,4 +75,16 @@ describe("TextField", () => {
|
|||||||
));
|
));
|
||||||
expect(screen.getByRole("textbox").getAttribute("aria-required")).toBe("true");
|
expect(screen.getByRole("textbox").getAttribute("aria-required")).toBe("true");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("input aria-errormessage links to error message id", () => {
|
||||||
|
render(() => (
|
||||||
|
<TextField invalid>
|
||||||
|
<TextField.Input />
|
||||||
|
<TextField.ErrorMessage>Required</TextField.ErrorMessage>
|
||||||
|
</TextField>
|
||||||
|
));
|
||||||
|
const input = screen.getByRole("textbox");
|
||||||
|
const errorEl = screen.getByText("Required");
|
||||||
|
expect(input.getAttribute("aria-errormessage")).toBe(errorEl.id);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user