Skip to content

Commit ab35145

Browse files
committed
feat(mat/checkbox): add new aria properties to MatCheckbox
Added three new aria properties to MatCheckbox: `aria-expanded`: Indicates whether the checkbox controls the visibility of another element. This should be a boolean value (true or false). `aria-controls`: Specifies the ID of the element that the checkbox controls. `aria-owns`: Specifies the ID of the element that the checkbox visually owns. These attributes will be added to the generated checkbox element if they are specified and won't be present in the HTML if not provided. Also added a small paragraph at the end of the checkbox.md file. Fixes #28761
1 parent 174cf42 commit ab35145

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

src/material/checkbox/checkbox.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
[attr.aria-labelledby]="ariaLabelledby"
1111
[attr.aria-describedby]="ariaDescribedby"
1212
[attr.aria-checked]="indeterminate ? 'mixed' : null"
13+
[attr.aria-expanded]="ariaExpanded"
14+
[attr.aria-controls]="ariaControls"
15+
[attr.aria-owns]="ariaOwns"
1316
[attr.name]="name"
1417
[attr.value]="value"
1518
[checked]="checked"

src/material/checkbox/checkbox.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,9 @@ binding these properties, as demonstrated below.
7474
<mat-checkbox [aria-label]="isSubscribedToEmailsMessage">
7575
</mat-checkbox>
7676
```
77+
78+
Additionally, `MatCheckbox` now supports the following accessibility properties:
79+
80+
- **`aria-expanded`**: Indicates whether the checkbox controls the visibility of another element. This should be a boolean value (`true` or `false`).
81+
- **`aria-controls`**: Specifies the ID of the element that the checkbox controls.
82+
- **`aria-owns`**: Specifies the ID of the element that the checkbox visually owns.

src/material/checkbox/checkbox.spec.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,94 @@ describe('MDC-based MatCheckbox', () => {
776776
});
777777
});
778778

779+
describe('with provided aria-expanded', () => {
780+
let checkboxDebugElement: DebugElement;
781+
let checkboxNativeElement: HTMLElement;
782+
let inputElement: HTMLInputElement;
783+
784+
it('should use the provided postive aria-expanded', () => {
785+
fixture = createComponent(CheckboxWithPositiveAriaExpanded);
786+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
787+
checkboxNativeElement = checkboxDebugElement.nativeElement;
788+
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
789+
790+
fixture.detectChanges();
791+
expect(inputElement.getAttribute('aria-expanded')).toBe('true');
792+
});
793+
794+
it('should use the provided negative aria-expanded', () => {
795+
fixture = createComponent(CheckboxWithNegativeAriaExpanded);
796+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
797+
checkboxNativeElement = checkboxDebugElement.nativeElement;
798+
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
799+
800+
fixture.detectChanges();
801+
expect(inputElement.getAttribute('aria-expanded')).toBe('false');
802+
});
803+
804+
it('should not assign aria-expanded if none is provided', () => {
805+
fixture = createComponent(SingleCheckbox);
806+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
807+
checkboxNativeElement = checkboxDebugElement.nativeElement;
808+
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
809+
810+
fixture.detectChanges();
811+
expect(inputElement.getAttribute('aria-expanded')).toBe(null);
812+
});
813+
});
814+
815+
describe('with provided aria-controls', () => {
816+
let checkboxDebugElement: DebugElement;
817+
let checkboxNativeElement: HTMLElement;
818+
let inputElement: HTMLInputElement;
819+
820+
it('should use the provided aria-controls', () => {
821+
fixture = createComponent(CheckboxWithAriaControls);
822+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
823+
checkboxNativeElement = checkboxDebugElement.nativeElement;
824+
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
825+
826+
fixture.detectChanges();
827+
expect(inputElement.getAttribute('aria-controls')).toBe('some-id');
828+
});
829+
830+
it('should not assign aria-controls if none is provided', () => {
831+
fixture = createComponent(SingleCheckbox);
832+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
833+
checkboxNativeElement = checkboxDebugElement.nativeElement;
834+
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
835+
836+
fixture.detectChanges();
837+
expect(inputElement.getAttribute('aria-controls')).toBe(null);
838+
});
839+
});
840+
841+
describe('with provided aria-owns', () => {
842+
let checkboxDebugElement: DebugElement;
843+
let checkboxNativeElement: HTMLElement;
844+
let inputElement: HTMLInputElement;
845+
846+
it('should use the provided aria-owns', () => {
847+
fixture = createComponent(CheckboxWithAriaOwns);
848+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
849+
checkboxNativeElement = checkboxDebugElement.nativeElement;
850+
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
851+
852+
fixture.detectChanges();
853+
expect(inputElement.getAttribute('aria-owns')).toBe('some-id');
854+
});
855+
856+
it('should not assign aria-owns if none is provided', () => {
857+
fixture = createComponent(SingleCheckbox);
858+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
859+
checkboxNativeElement = checkboxDebugElement.nativeElement;
860+
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
861+
862+
fixture.detectChanges();
863+
expect(inputElement.getAttribute('aria-owns')).toBe(null);
864+
});
865+
});
866+
779867
describe('with provided tabIndex', () => {
780868
let checkboxDebugElement: DebugElement;
781869
let checkboxNativeElement: HTMLElement;
@@ -1206,6 +1294,38 @@ class CheckboxWithAriaLabelledby {}
12061294
})
12071295
class CheckboxWithAriaDescribedby {}
12081296

1297+
/** Simple test component with an aria-expanded set with true. */
1298+
@Component({
1299+
template: `<mat-checkbox aria-expanded="true"></mat-checkbox>`,
1300+
standalone: true,
1301+
imports: [MatCheckbox],
1302+
})
1303+
class CheckboxWithPositiveAriaExpanded {}
1304+
1305+
/** Simple test component with an aria-expanded set with false. */
1306+
@Component({
1307+
template: `<mat-checkbox aria-expanded="false"></mat-checkbox>`,
1308+
standalone: true,
1309+
imports: [MatCheckbox],
1310+
})
1311+
class CheckboxWithNegativeAriaExpanded {}
1312+
1313+
/** Simple test component with an aria-controls set. */
1314+
@Component({
1315+
template: `<mat-checkbox aria-controls="some-id"></mat-checkbox>`,
1316+
standalone: true,
1317+
imports: [MatCheckbox],
1318+
})
1319+
class CheckboxWithAriaControls {}
1320+
1321+
/** Simple test component with an aria-owns set. */
1322+
@Component({
1323+
template: `<mat-checkbox aria-owns="some-id"></mat-checkbox>`,
1324+
standalone: true,
1325+
imports: [MatCheckbox],
1326+
})
1327+
class CheckboxWithAriaOwns {}
1328+
12091329
/** Simple test component with name attribute */
12101330
@Component({
12111331
template: `<mat-checkbox name="test-name"></mat-checkbox>`,

src/material/checkbox/checkbox.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ export class MatCheckbox
159159
/** The 'aria-describedby' attribute is read after the element's label and field type. */
160160
@Input('aria-describedby') ariaDescribedby: string;
161161

162+
/**
163+
* Users can specify the `aria-expanded` attribute which will be forwarded to the input element
164+
*/
165+
@Input({alias: 'aria-expanded', transform: booleanAttribute}) ariaExpanded: boolean;
166+
167+
/**
168+
* Users can specify the `aria-controls` attribute which will be forwarded to the input element
169+
*/
170+
@Input('aria-controls') ariaControls: string;
171+
172+
/** Users can specify the `aria-owns` attribute which will be forwarded to the input element */
173+
@Input('aria-owns') ariaOwns: string;
174+
162175
private _uniqueId: string;
163176

164177
/** A unique id for the checkbox input. If none is supplied, it will be auto-generated. */

0 commit comments

Comments
 (0)