Skip to content

Commit

Permalink
feat(material/checkbox): allow focus origin to be optional input in f…
Browse files Browse the repository at this point in the history
…ocus method (#20905)

Co-authored-by: Jessica Xu <jessicacxu@google.com>
  • Loading branch information
Jessica Xu and jesscxu authored Nov 3, 2020
1 parent 23d0b80 commit 5575673
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/material/checkbox/checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,21 @@ describe('MatCheckbox', () => {
expect(secondId).toMatch(/mat-checkbox-\d+-input/);
expect(firstId).not.toEqual(secondId);
});

it('should not change focus origin if origin not specified', () => {
let [firstCheckboxDebugEl, secondCheckboxDebugEl] =
fixture.debugElement.queryAll(By.directive(MatCheckbox));
fixture.detectChanges();

const firstCheckboxInstance = firstCheckboxDebugEl.componentInstance as MatCheckbox;
const secondCheckboxInstance = secondCheckboxDebugEl.componentInstance as MatCheckbox;

firstCheckboxInstance.focus('mouse');
secondCheckboxInstance.focus();

expect(secondCheckboxDebugEl.nativeElement.classList).toContain('cdk-focused');
expect(secondCheckboxDebugEl.nativeElement.classList).toContain('cdk-mouse-focused');
});
});

describe('with ngModel', () => {
Expand Down
8 changes: 6 additions & 2 deletions src/material/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,12 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
}

/** Focuses the checkbox. */
focus(origin: FocusOrigin = 'keyboard', options?: FocusOptions): void {
this._focusMonitor.focusVia(this._inputElement, origin, options);
focus(origin?: FocusOrigin, options?: FocusOptions): void {
if (origin) {
this._focusMonitor.focusVia(this._inputElement, origin, options);
} else {
this._inputElement.nativeElement.focus(options);
}
}

_onInteractionEvent(event: Event) {
Expand Down

0 comments on commit 5575673

Please sign in to comment.