Skip to content

Commit 5575673

Browse files
Jessica Xujesscxu
Jessica Xu
andauthored
feat(material/checkbox): allow focus origin to be optional input in focus method (#20905)
Co-authored-by: Jessica Xu <[email protected]>
1 parent 23d0b80 commit 5575673

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/material/checkbox/checkbox.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,21 @@ describe('MatCheckbox', () => {
909909
expect(secondId).toMatch(/mat-checkbox-\d+-input/);
910910
expect(firstId).not.toEqual(secondId);
911911
});
912+
913+
it('should not change focus origin if origin not specified', () => {
914+
let [firstCheckboxDebugEl, secondCheckboxDebugEl] =
915+
fixture.debugElement.queryAll(By.directive(MatCheckbox));
916+
fixture.detectChanges();
917+
918+
const firstCheckboxInstance = firstCheckboxDebugEl.componentInstance as MatCheckbox;
919+
const secondCheckboxInstance = secondCheckboxDebugEl.componentInstance as MatCheckbox;
920+
921+
firstCheckboxInstance.focus('mouse');
922+
secondCheckboxInstance.focus();
923+
924+
expect(secondCheckboxDebugEl.nativeElement.classList).toContain('cdk-focused');
925+
expect(secondCheckboxDebugEl.nativeElement.classList).toContain('cdk-mouse-focused');
926+
});
912927
});
913928

914929
describe('with ngModel', () => {

src/material/checkbox/checkbox.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,12 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
425425
}
426426

427427
/** Focuses the checkbox. */
428-
focus(origin: FocusOrigin = 'keyboard', options?: FocusOptions): void {
429-
this._focusMonitor.focusVia(this._inputElement, origin, options);
428+
focus(origin?: FocusOrigin, options?: FocusOptions): void {
429+
if (origin) {
430+
this._focusMonitor.focusVia(this._inputElement, origin, options);
431+
} else {
432+
this._inputElement.nativeElement.focus(options);
433+
}
430434
}
431435

432436
_onInteractionEvent(event: Event) {

0 commit comments

Comments
 (0)