Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ const sections = [
<td>Reference to the component.</td>
<td>-</td>
</tr>
<tr>
<td>ariaLabel</td>
<td>
<TableCode>{"string"}</TableCode>
</td>
<td>Specifies a string to be used as the name for the switch element when no `label` is provided.</td>
<td>'Switch'</td>
</tr>
</tbody>
</DxcTable>
),
Expand Down
7 changes: 7 additions & 0 deletions packages/lib/src/switch/Switch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<DxcSwitch ariaLabel="Example aria label" />);
const switchEl = getByRole("switch");
expect(switchEl.getAttribute("aria-label")).toBe("Example aria label");
});

test("Renders disabled switch correctly", () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/lib/src/switch/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const DxcSwitch = forwardRef<RefType, SwitchPropsType>(
margin,
size = "fitContent",
tabIndex = 0,
ariaLabel = "Switch",
},
ref
): JSX.Element => {
Expand Down Expand Up @@ -83,6 +84,7 @@ const DxcSwitch = forwardRef<RefType, SwitchPropsType>(
aria-disabled={disabled}
disabled={disabled}
aria-labelledby={labelId}
aria-label={label ? undefined : ariaLabel}
tabIndex={!disabled ? tabIndex : -1}
ref={refTrack}
/>
Expand Down
4 changes: 4 additions & 0 deletions packages/lib/src/switch/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down
Loading