Skip to content

Commit 904d2ec

Browse files
author
Hector Arce De Las Heras
committed
Enhance Accessibility for Checkbox Component
This commit improves the accessibility of the Checkbox component. The aria-describedby attribute of the checkbox now includes the id of the checkbox label, providing more context for screen readers. Changes include: Updated test in checkbox.test.tsx to verify that the aria-describedby attribute includes the checkbox label id. In checkboxStandAlone.tsx, added a new id for the checkbox label and included this id in the aria-describedby attribute. Updated buildAriaDescribedBy function in aria.utils.ts to include the checkbox label id in the aria-describedby attribute when a label is provided. These changes help screen readers provide more context to users about the checkbox, improving overall accessibility.
1 parent fd6737d commit 904d2ec

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/components/checkbox/__tests__/checkbox.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,10 @@ describe('Checkbox component', () => {
247247
const checkbox = screen.getByRole('checkbox');
248248

249249
expect(checkbox).toBeInTheDocument();
250-
expect(checkbox).toHaveAttribute('aria-describedby', `${checkboxId}ScreenReader`);
250+
expect(checkbox).toHaveAttribute(
251+
'aria-describedby',
252+
`${checkboxId}Label ${checkboxId}ScreenReader`
253+
);
251254

252255
const results = await axe(container);
253256
expect(container).toHTMLValidate();

src/components/checkbox/checkboxStandAlone.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const CheckboxStandAloneComponent = (
5151
const { isChecked, isDisabled, hasError } = checkboxState(state);
5252

5353
const checkBoxId = id ?? uniqueId;
54+
const checkBoxLabelId = `${checkBoxId}Label`;
5455
const checkBoxkHelpContentId = `${checkBoxId}HelpContent`;
5556
const checkBoxErrorId = `${checkBoxId}Error`;
5657
const screenReaderId = `${checkBoxId}ScreenReader`;
@@ -68,6 +69,7 @@ const CheckboxStandAloneComponent = (
6869
color={labelTypography?.color}
6970
cursor={isDisabled ? CURSOR_DEFAULT : CURSOR_POINTER}
7071
dataTestId={`${dataTestId}Label`}
72+
id={checkBoxLabelId}
7173
inputId={checkBoxId}
7274
required={required}
7375
textVariant={labelTypography?.font_variant}
@@ -126,6 +128,8 @@ const CheckboxStandAloneComponent = (
126128
ref={ref}
127129
aria-describedby={buildAriaDescribedBy({
128130
extraAriaDescribedBy,
131+
label: label?.content,
132+
checkBoxLabelId,
129133
helpContent: helperContent?.content,
130134
checkBoxkHelpContentId,
131135
hasError,

src/components/checkbox/utils/aria.utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* Build the aria-describedby attribute for the checkbox
33
* @param extraAriaDescribedBy
4+
* @param label
5+
* @param checkBoxLabelId
46
* @param helpContent
57
* @param checkBoxkHelpContentId
68
* @param textError
@@ -12,6 +14,8 @@
1214
*/
1315
export const buildAriaDescribedBy = ({
1416
extraAriaDescribedBy,
17+
label,
18+
checkBoxLabelId,
1519
helpContent,
1620
checkBoxkHelpContentId,
1721
errorText,
@@ -21,6 +25,8 @@ export const buildAriaDescribedBy = ({
2125
screenReaderId,
2226
}: {
2327
extraAriaDescribedBy: string;
28+
label?: JSX.Element | string;
29+
checkBoxLabelId: string;
2430
helpContent?: JSX.Element | string;
2531
checkBoxkHelpContentId: string;
2632
hasError: boolean;
@@ -30,6 +36,9 @@ export const buildAriaDescribedBy = ({
3036
screenReaderId: string;
3137
}): string => {
3238
let res = extraAriaDescribedBy;
39+
if (label) {
40+
res += ` ${checkBoxLabelId}`;
41+
}
3342
if (screenReaderText) {
3443
res += ` ${screenReaderId}`;
3544
}

0 commit comments

Comments
 (0)