Skip to content

Commit 4ec9163

Browse files
authored
Merge pull request #2143 from dxc-technology/jialecl-switch-ariaLabel
ariaLabel prop added to switch component
2 parents 1173457 + a0dd252 commit 4ec9163

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

apps/website/screens/components/switch/code/SwitchCodePage.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ const sections = [
152152
<td>Reference to the component.</td>
153153
<td>-</td>
154154
</tr>
155+
<tr>
156+
<td>ariaLabel</td>
157+
<td>
158+
<TableCode>{"string"}</TableCode>
159+
</td>
160+
<td>Specifies a string to be used as the name for the switch element when no `label` is provided.</td>
161+
<td>'Switch'</td>
162+
</tr>
155163
</tbody>
156164
</DxcTable>
157165
),

packages/lib/src/switch/Switch.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ describe("Switch component tests", () => {
101101
const label = getByText("Default label");
102102
expect(switchEl.getAttribute("aria-labelledby")).toBe(label.id);
103103
expect(switchEl.getAttribute("aria-checked")).toBe("false");
104+
expect(switchEl.getAttribute("aria-label")).toBeNull();
105+
});
106+
107+
test("Renders with correct aria-label", () => {
108+
const { getByRole } = render(<DxcSwitch ariaLabel="Example aria label" />);
109+
const switchEl = getByRole("switch");
110+
expect(switchEl.getAttribute("aria-label")).toBe("Example aria label");
104111
});
105112

106113
test("Renders disabled switch correctly", () => {

packages/lib/src/switch/Switch.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const DxcSwitch = forwardRef<RefType, SwitchPropsType>(
2020
margin,
2121
size = "fitContent",
2222
tabIndex = 0,
23+
ariaLabel = "Switch",
2324
},
2425
ref
2526
): JSX.Element => {
@@ -83,6 +84,7 @@ const DxcSwitch = forwardRef<RefType, SwitchPropsType>(
8384
aria-disabled={disabled}
8485
disabled={disabled}
8586
aria-labelledby={labelId}
87+
aria-label={label ? undefined : ariaLabel}
8688
tabIndex={!disabled ? tabIndex : -1}
8789
ref={refTrack}
8890
/>

packages/lib/src/switch/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ type Props = {
5353
* Value of the tabindex.
5454
*/
5555
tabIndex?: number;
56+
/**
57+
* Specifies a string to be used as the name for the switch element when no `label` is provided.
58+
*/
59+
ariaLabel?: string;
5660
};
5761

5862
/**

0 commit comments

Comments
 (0)