Skip to content

Commit 40b345d

Browse files
committed
fix(material-experimental/mdc-checkbox): remove extra a11y tree node for the <label/>
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 cc5326c commit 40b345d

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="mdc-form-field"
2-
[class.mdc-form-field--align-end]="labelPosition == 'before'">
2+
[class.mdc-form-field--align-end]="labelPosition == 'before'" (click)="filterLabelClicks($event)">
33
<div #checkbox class="mdc-checkbox">
44
<!-- Render this element first so the input is on top. -->
55
<div class="mat-mdc-checkbox-touch-target" (click)="_onClick()"></div>
@@ -38,8 +38,7 @@
3838
[matRippleCentered]="true"></div>
3939
</div>
4040
<label #label
41-
[for]="inputId"
42-
(click)="$event.stopPropagation()">
41+
[for]="inputId">
4342
<ng-content></ng-content>
4443
</label>
4544
</div>

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,20 @@ describe('MDC-based MatCheckbox', () => {
306306
);
307307
}));
308308

309-
it('should not trigger the click event multiple times', fakeAsync(() => {
309+
it('should trigger the click once when clicking on the <input/>', fakeAsync(() => {
310+
spyOn(testComponent, 'onCheckboxClick');
311+
312+
expect(inputElement.checked).toBe(false);
313+
314+
inputElement.click();
315+
fixture.detectChanges();
316+
flush();
317+
318+
expect(inputElement.checked).toBe(true);
319+
expect(testComponent.onCheckboxClick).toHaveBeenCalledTimes(1);
320+
}));
321+
322+
it('should trigger the click event once when clicking on the label', fakeAsync(() => {
310323
// By default, when clicking on a label element, a generated click will be dispatched
311324
// on the associated input element.
312325
// Since we're using a label element and a visual hidden input, this behavior can led
@@ -1034,7 +1047,7 @@ describe('MatCheckboxDefaultOptions', () => {
10341047
/** Simple component for testing a single checkbox. */
10351048
@Component({
10361049
template: `
1037-
<div (click)="parentElementClicked = true" (keyup)="parentElementKeyedUp = true">
1050+
<div (click)="parentElementClicked = true" (keyup)="parentElementKeyedUp = true" (click)="onCheckboxClick($event)">
10381051
<mat-checkbox
10391052
[id]="checkboxId"
10401053
[required]="isRequired"
@@ -1045,7 +1058,6 @@ describe('MatCheckboxDefaultOptions', () => {
10451058
[color]="checkboxColor"
10461059
[disableRipple]="disableRipple"
10471060
[value]="checkboxValue"
1048-
(click)="onCheckboxClick($event)"
10491061
(change)="onCheckboxChange($event)">
10501062
Simple checkbox
10511063
</mat-checkbox>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,15 @@ export class MatCheckbox
388388
nativeCheckbox.nativeElement.indeterminate = value;
389389
}
390390
}
391+
392+
// By default, when clicking on a label element, a generated click will be dispatched
393+
// on the associated input element.
394+
// Since we're using a label element and a visual hidden input, this behavior can led
395+
// to an issue, where the click events on the checkbox are getting executed twice.
396+
397+
protected filterLabelClicks(event: MouseEvent) {
398+
if (event.target instanceof HTMLElement && this._label.nativeElement.contains(event.target)) {
399+
event.stopPropagation();
400+
}
401+
}
391402
}

0 commit comments

Comments
 (0)