Skip to content

Commit 621d175

Browse files
committed
fix(material/checkbox): focus not moved to input when clicking on touch target (#26545)
Fixes that while we were toggling the checked state of a checkbox when its touch target is clicked, we weren't moving focus to the internal `input`. Fixes #26486. (cherry picked from commit 48b4445)
1 parent 719cff0 commit 621d175

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

src/material/checkbox/checkbox.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
(click)="_preventBubblingFromLabel($event)">
44
<div #checkbox class="mdc-checkbox">
55
<!-- Render this element first so the input is on top. -->
6-
<div class="mat-mdc-checkbox-touch-target" (click)="_onInputClick()"></div>
6+
<div class="mat-mdc-checkbox-touch-target" (click)="_onTouchTargetClick()"></div>
77
<input #input
88
type="checkbox"
99
class="mdc-checkbox__native-control"

src/material/checkbox/checkbox.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,20 @@ describe('MDC-based MatCheckbox', () => {
404404
expect(document.activeElement).toBe(inputElement);
405405
}));
406406

407+
it('should focus underlying input element when the touch target is clicked', fakeAsync(() => {
408+
const touchTarget = checkboxElement.querySelector(
409+
'.mat-mdc-checkbox-touch-target',
410+
) as HTMLElement;
411+
412+
expect(document.activeElement).not.toBe(inputElement);
413+
414+
touchTarget.click();
415+
fixture.detectChanges();
416+
flush();
417+
418+
expect(document.activeElement).toBe(inputElement);
419+
}));
420+
407421
it('should forward the value to input element', fakeAsync(() => {
408422
testComponent.checkboxValue = 'basic_checkbox';
409423
fixture.detectChanges();

src/material/checkbox/checkbox.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,16 @@ export class MatCheckbox
547547
super._handleInputClick();
548548
}
549549

550+
_onTouchTargetClick() {
551+
super._handleInputClick();
552+
553+
if (!this.disabled) {
554+
// Normally the input should be focused already, but if the click
555+
// comes from the touch target, then we might have to focus it ourselves.
556+
this._inputElement.nativeElement.focus();
557+
}
558+
}
559+
550560
/**
551561
* Prevent click events that come from the `<label/>` element from bubbling. This prevents the
552562
* click handler on the host from triggering twice when clicking on the `<label/>` element. After

tools/public_api_guard/material/checkbox.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export class MatCheckbox extends _MatCheckboxBase<MatCheckboxChange> implements
5858
protected _getAnimationTargetElement(): HTMLInputElement;
5959
// (undocumented)
6060
_onInputClick(): void;
61+
// (undocumented)
62+
_onTouchTargetClick(): void;
6163
_preventBubblingFromLabel(event: MouseEvent): void;
6264
// (undocumented)
6365
static ɵcmp: i0.ɵɵComponentDeclaration<MatCheckbox, "mat-checkbox", ["matCheckbox"], { "disableRipple": "disableRipple"; "color": "color"; "tabIndex": "tabIndex"; }, {}, never, ["*"], false, never>;

0 commit comments

Comments
 (0)