Skip to content

Commit 56866f4

Browse files
authored
Merge pull request #2136 from dxc-technology/jialecl-checkbox-ariaLabel
ariaLabel prop added to checkbox
2 parents af3aa81 + 8d560ab commit 56866f4

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

apps/website/screens/components/checkbox/code/CheckboxCodePage.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ const sections = [
164164
<td>Reference to the component.</td>
165165
<td>-</td>
166166
</tr>
167+
<tr>
168+
<td>ariaLabel</td>
169+
<td>
170+
<TableCode>string</TableCode>
171+
</td>
172+
<td>
173+
Specifies a string to be used as the name for the checkbox element when no <Code>label</Code> is provided.
174+
</td>
175+
<td>'Checkbox'</td>
176+
</tr>
167177
</tbody>
168178
</DxcTable>
169179
),

packages/lib/src/checkbox/Checkbox.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ describe("Checkbox component tests", () => {
1010
expect(getByRole("checkbox").getAttribute("aria-required")).toBe("true");
1111
expect(getByRole("checkbox").getAttribute("aria-readonly")).toBe("false");
1212
expect(getByRole("checkbox").getAttribute("aria-disabled")).toBe("false");
13+
expect(getByRole("checkbox").getAttribute("aria-label")).toBeNull();
14+
});
15+
test("Renders with correct aria-label", () => {
16+
const { getByRole } = render(<DxcCheckbox ariaLabel="Example aria label" />);
17+
const checkbox = getByRole("checkbox");
18+
expect(checkbox.getAttribute("aria-label")).toBe("Example aria label");
1319
});
1420
test("Optional checkbox renders with correct aria-required", () => {
1521
const { getByRole } = render(<DxcCheckbox label="Checkbox" optional />);

packages/lib/src/checkbox/Checkbox.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const DxcCheckbox = forwardRef<RefType, CheckboxPropsType>(
2727
margin,
2828
size = "fitContent",
2929
tabIndex = 0,
30+
ariaLabel = "Checkbox",
3031
},
3132
ref
3233
): JSX.Element => {
@@ -94,7 +95,7 @@ const DxcCheckbox = forwardRef<RefType, CheckboxPropsType>(
9495
aria-readonly={readOnly}
9596
aria-required={!disabled && !optional}
9697
aria-labelledby={label ? labelId : undefined}
97-
aria-label={label ? undefined : "Checkbox"}
98+
aria-label={label ? undefined : ariaLabel}
9899
checked={checked ?? innerChecked}
99100
disabled={disabled}
100101
readOnly={readOnly}

packages/lib/src/checkbox/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ type Props = {
5959
* Value of the tabindex.
6060
*/
6161
tabIndex?: number;
62+
/**
63+
* Specifies a string to be used as the name for the checkbox element when no `label` is provided.
64+
*/
65+
ariaLabel?: string;
6266
};
6367

6468
/**

0 commit comments

Comments
 (0)