From 2a0745770811f7bc09e6d3622b90bdf3b2c9b2ba Mon Sep 17 00:00:00 2001 From: Jialecl Date: Mon, 13 Jan 2025 13:56:40 +0100 Subject: [PATCH 1/4] ariaLabel prop added to textInput --- .../components/text-input/code/TextInputCodePage.tsx | 11 +++++++++++ packages/lib/src/text-input/TextInput.test.tsx | 9 +++++++++ packages/lib/src/text-input/TextInput.tsx | 2 ++ packages/lib/src/text-input/types.ts | 4 ++++ 4 files changed, 26 insertions(+) diff --git a/apps/website/screens/components/text-input/code/TextInputCodePage.tsx b/apps/website/screens/components/text-input/code/TextInputCodePage.tsx index 55df2016ff..27e50064fe 100644 --- a/apps/website/screens/components/text-input/code/TextInputCodePage.tsx +++ b/apps/website/screens/components/text-input/code/TextInputCodePage.tsx @@ -330,6 +330,17 @@ const sections = [ Reference to the component. - + + ariaLabel + + string + + + Specifies a string to be used as the name for the textInput element when no label is + provided. + + 'Text input' + ), diff --git a/packages/lib/src/text-input/TextInput.test.tsx b/packages/lib/src/text-input/TextInput.test.tsx index ec1994dae1..22be2c6ff8 100644 --- a/packages/lib/src/text-input/TextInput.test.tsx +++ b/packages/lib/src/text-input/TextInput.test.tsx @@ -50,6 +50,15 @@ describe("TextInput component tests", () => { expect(input.getAttribute("aria-errormessage")).toBe(errorMessage.id); expect(input.getAttribute("aria-invalid")).toBe("true"); expect(errorMessage.getAttribute("aria-live")).toBe("assertive"); + expect(input.getAttribute("aria-label")).toBeNull(); + }); + + test("Renders with correct error aria label", () => { + const { getByRole } = render( + + ); + const input = getByRole("textbox"); + expect(input.getAttribute("aria-label")).toBe("Example aria label"); }); test("Renders with correct initial value", () => { diff --git a/packages/lib/src/text-input/TextInput.tsx b/packages/lib/src/text-input/TextInput.tsx index 56957e107a..7d841b3c7f 100644 --- a/packages/lib/src/text-input/TextInput.tsx +++ b/packages/lib/src/text-input/TextInput.tsx @@ -105,6 +105,7 @@ const DxcTextInput = forwardRef( margin, size = "medium", tabIndex = 0, + ariaLabel = "Text input", }, ref ): JSX.Element => { @@ -520,6 +521,7 @@ const DxcTextInput = forwardRef( aria-invalid={!!error} aria-errormessage={error ? errorId : undefined} aria-required={!disabled && !optional} + aria-label={label ? undefined : ariaLabel} /> {!disabled && error && (