Skip to content

Commit c543db5

Browse files
authored
fix(material-experimental/mdc-checkbox): remove extra a11y tree node for the <label/> (#24907)
In the mdc checkbox component, removes the click handler on <label/> and handles stoping propgation of clicks on the label in the label's parent. This removes the extra a11y tree node on the label and fixes TalkBack having an extra navigation stop (#14385). A11y tree before this commit. It has an un-necessary node, which coresponds to the `<label>` element. ``` - Generic - Checkbox, "Field A" - Textlabel, "Field A" ``` A11y tree with this commit applied ``` - Generic - Checkbox, "Field A" ``` fixes #14385
1 parent 450130f commit c543db5

File tree

6 files changed

+41
-12
lines changed

6 files changed

+41
-12
lines changed

src/material-experimental/mdc-checkbox/checkbox.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<div class="mdc-form-field"
2-
[class.mdc-form-field--align-end]="labelPosition == 'before'">
2+
[class.mdc-form-field--align-end]="labelPosition == 'before'"
3+
(click)="_preventBubblingFromLabel($event)">
34
<div #checkbox class="mdc-checkbox">
45
<!-- Render this element first so the input is on top. -->
56
<div class="mat-mdc-checkbox-touch-target" (click)="_onInputClick()"></div>
@@ -38,9 +39,13 @@
3839
[matRippleDisabled]="disableRipple || disabled"
3940
[matRippleCentered]="true"></div>
4041
</div>
42+
<!--
43+
Avoid putting a click handler on the <label/> to fix duplicate navigation stop on Talk Back
44+
(#14385). Putting a click handler on the <label/> caused this bug because the browser produced
45+
an unnecessary accessibility tree node.
46+
-->
4147
<label #label
42-
[for]="inputId"
43-
(click)="$event.stopPropagation()">
48+
[for]="inputId">
4449
<ng-content></ng-content>
4550
</label>
4651
</div>

src/material-experimental/mdc-checkbox/checkbox.spec.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,20 @@ describe('MDC-based MatCheckbox', () => {
309309
);
310310
}));
311311

312-
it('should not trigger the click event multiple times', fakeAsync(() => {
312+
it('should trigger the click once when clicking on the <input/>', fakeAsync(() => {
313+
spyOn(testComponent, 'onCheckboxClick');
314+
315+
expect(inputElement.checked).toBe(false);
316+
317+
inputElement.click();
318+
fixture.detectChanges();
319+
flush();
320+
321+
expect(inputElement.checked).toBe(true);
322+
expect(testComponent.onCheckboxClick).toHaveBeenCalledTimes(1);
323+
}));
324+
325+
it('should trigger the click event once when clicking on the label', fakeAsync(() => {
313326
// By default, when clicking on a label element, a generated click will be dispatched
314327
// on the associated input element.
315328
// Since we're using a label element and a visual hidden input, this behavior can led
@@ -1037,7 +1050,7 @@ describe('MatCheckboxDefaultOptions', () => {
10371050
/** Simple component for testing a single checkbox. */
10381051
@Component({
10391052
template: `
1040-
<div (click)="parentElementClicked = true" (keyup)="parentElementKeyedUp = true">
1053+
<div (click)="parentElementClicked = true" (keyup)="parentElementKeyedUp = true" (click)="onCheckboxClick($event)">
10411054
<mat-checkbox
10421055
[id]="checkboxId"
10431056
[required]="isRequired"
@@ -1048,7 +1061,6 @@ describe('MatCheckboxDefaultOptions', () => {
10481061
[color]="checkboxColor"
10491062
[disableRipple]="disableRipple"
10501063
[value]="checkboxValue"
1051-
(click)="onCheckboxClick($event)"
10521064
(change)="onCheckboxChange($event)">
10531065
Simple checkbox
10541066
</mat-checkbox>

src/material-experimental/mdc-checkbox/checkbox.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,17 @@ export class MatCheckbox
116116
_onInputClick() {
117117
super._handleInputClick();
118118
}
119+
120+
/**
121+
* Prevent click events that come from the `<label/>` element from bubbling. This prevents the
122+
* click handler on the host from triggering twice when clicking on the `<label/>` element. After
123+
* the click event on the `<label/>` propagates, the browsers dispatches click on the associated
124+
* `<input/>`. By preventing clicks on the label by bubbling, we ensure only one click event
125+
* bubbles when the label is clicked.
126+
*/
127+
_preventBubblingFromLabel(event: MouseEvent) {
128+
if (!!event.target && this._labelElement.nativeElement.contains(event.target as HTMLElement)) {
129+
event.stopPropagation();
130+
}
131+
}
119132
}

src/material/checkbox/checkbox.spec.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,7 @@ describe('MatCheckbox', () => {
296296
expect(checkboxNativeElement.classList).toContain('mat-checkbox-label-before');
297297
});
298298

299-
it('should not trigger the click event multiple times', () => {
300-
// By default, when clicking on a label element, a generated click will be dispatched
301-
// on the associated input element.
302-
// Since we're using a label element and a visual hidden input, this behavior can led
303-
// to an issue, where the click events on the checkbox are getting executed twice.
304-
299+
it('should trigger the click event once when clicking on the label', () => {
305300
spyOn(testComponent, 'onCheckboxClick');
306301

307302
expect(inputElement.checked).toBe(false);

src/material/checkbox/checkbox.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ export abstract class _MatCheckboxBase<E>
183183
/** The native `<input type="checkbox">` element */
184184
@ViewChild('input') _inputElement: ElementRef<HTMLInputElement>;
185185

186+
/** The native `<label>` element */
187+
@ViewChild('label') _labelElement: ElementRef<HTMLInputElement>;
188+
186189
/** Reference to the ripple instance of the checkbox. */
187190
@ViewChild(MatRipple) ripple: MatRipple;
188191

tools/public_api_guard/material/checkbox.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export abstract class _MatCheckboxBase<E> extends _MatCheckboxMixinBase implemen
108108
get inputId(): string;
109109
// (undocumented)
110110
_isRippleDisabled(): boolean;
111+
_labelElement: ElementRef<HTMLInputElement>;
111112
labelPosition: 'before' | 'after';
112113
name: string | null;
113114
// (undocumented)

0 commit comments

Comments
 (0)