From 2474fa4f6b421149cbb0dc6aed19491149a87ddd Mon Sep 17 00:00:00 2001 From: Jialecl Date: Wed, 15 Jan 2025 12:10:42 +0100 Subject: [PATCH 1/2] ariaLabel prop added to switch component --- .../screens/components/switch/code/SwitchCodePage.tsx | 8 ++++++++ packages/lib/src/switch/Switch.test.tsx | 7 +++++++ packages/lib/src/switch/Switch.tsx | 2 ++ packages/lib/src/switch/types.ts | 4 ++++ 4 files changed, 21 insertions(+) diff --git a/apps/website/screens/components/switch/code/SwitchCodePage.tsx b/apps/website/screens/components/switch/code/SwitchCodePage.tsx index 170c598243..ea2988d226 100644 --- a/apps/website/screens/components/switch/code/SwitchCodePage.tsx +++ b/apps/website/screens/components/switch/code/SwitchCodePage.tsx @@ -152,6 +152,14 @@ const sections = [ Reference to the component. - + + ariaLabel + + {"strig"} + + Specifies a string to be used as the name for the switch element when no `label` is provided. + 'Switch' + ), diff --git a/packages/lib/src/switch/Switch.test.tsx b/packages/lib/src/switch/Switch.test.tsx index d86b25340f..16079f5f24 100644 --- a/packages/lib/src/switch/Switch.test.tsx +++ b/packages/lib/src/switch/Switch.test.tsx @@ -101,6 +101,13 @@ describe("Switch component tests", () => { const label = getByText("Default label"); expect(switchEl.getAttribute("aria-labelledby")).toBe(label.id); expect(switchEl.getAttribute("aria-checked")).toBe("false"); + expect(switchEl.getAttribute("aria-label")).toBeNull(); + }); + + test("Renders with correct aria-label", () => { + const { getByRole } = render(); + const switchEl = getByRole("switch"); + expect(switchEl.getAttribute("aria-label")).toBe("Example aria label"); }); test("Renders disabled switch correctly", () => { diff --git a/packages/lib/src/switch/Switch.tsx b/packages/lib/src/switch/Switch.tsx index 544e78f623..67464536c4 100644 --- a/packages/lib/src/switch/Switch.tsx +++ b/packages/lib/src/switch/Switch.tsx @@ -20,6 +20,7 @@ const DxcSwitch = forwardRef( margin, size = "fitContent", tabIndex = 0, + ariaLabel = "Switch", }, ref ): JSX.Element => { @@ -83,6 +84,7 @@ const DxcSwitch = forwardRef( aria-disabled={disabled} disabled={disabled} aria-labelledby={labelId} + aria-label={label ? undefined : ariaLabel} tabIndex={!disabled ? tabIndex : -1} ref={refTrack} /> diff --git a/packages/lib/src/switch/types.ts b/packages/lib/src/switch/types.ts index 0c7c7656f9..1253a0f988 100644 --- a/packages/lib/src/switch/types.ts +++ b/packages/lib/src/switch/types.ts @@ -53,6 +53,10 @@ type Props = { * Value of the tabindex. */ tabIndex?: number; + /** + * Specifies a string to be used as the name for the switch element when no `label` is provided. + */ + ariaLabel?: string; }; /** From 7e2b3189a570dc37454fb79ba87876895a2b9fed Mon Sep 17 00:00:00 2001 From: Jialecl Date: Wed, 15 Jan 2025 14:11:53 +0100 Subject: [PATCH 2/2] fixed typo --- apps/website/screens/components/switch/code/SwitchCodePage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/website/screens/components/switch/code/SwitchCodePage.tsx b/apps/website/screens/components/switch/code/SwitchCodePage.tsx index ea2988d226..44040bdd3b 100644 --- a/apps/website/screens/components/switch/code/SwitchCodePage.tsx +++ b/apps/website/screens/components/switch/code/SwitchCodePage.tsx @@ -155,7 +155,7 @@ const sections = [ ariaLabel - {"strig"} + {"string"} Specifies a string to be used as the name for the switch element when no `label` is provided. 'Switch'