Skip to content

Commit a326702

Browse files
committed
feat(material/slide-toggle): allow focus origin to be optional input in focus method
1 parent cc503c4 commit a326702

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/material/slide-toggle/slide-toggle.spec.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {MutationObserverFactory} from '@angular/cdk/observers';
22
import {dispatchFakeEvent} from '@angular/cdk/testing/private';
3-
import {Component} from '@angular/core';
3+
import {Component, DebugElement} from '@angular/core';
44
import {
55
ComponentFixture,
66
fakeAsync,
@@ -731,15 +731,18 @@ describe('MatSlideToggle with forms', () => {
731731
let fixture: ComponentFixture<SlideToggleWithFormControl>;
732732

733733
let testComponent: SlideToggleWithFormControl;
734+
let slideToggleDebug: DebugElement;
734735
let slideToggle: MatSlideToggle;
735736
let inputElement: HTMLInputElement;
736737

737738
beforeEach(() => {
738739
fixture = TestBed.createComponent(SlideToggleWithFormControl);
739740
fixture.detectChanges();
740741

742+
slideToggleDebug = fixture.debugElement.query(By.directive(MatSlideToggle))!;
743+
741744
testComponent = fixture.debugElement.componentInstance;
742-
slideToggle = fixture.debugElement.query(By.directive(MatSlideToggle))!.componentInstance;
745+
slideToggle = slideToggleDebug.componentInstance;
743746
inputElement = fixture.debugElement.query(By.css('input'))!.nativeElement;
744747
});
745748

@@ -759,6 +762,15 @@ describe('MatSlideToggle with forms', () => {
759762
expect(slideToggle.disabled).toBe(false);
760763
expect(inputElement.disabled).toBe(false);
761764
});
765+
766+
it('should not change focus origin if origin not specified', () => {
767+
slideToggle.focus('mouse');
768+
slideToggle.focus();
769+
fixture.detectChanges();
770+
771+
expect(slideToggleDebug.nativeElement.classList).toContain('cdk-focused');
772+
expect(slideToggleDebug.nativeElement.classList).toContain('cdk-mouse-focused');
773+
});
762774
});
763775

764776
describe('with form element', () => {

src/material/slide-toggle/slide-toggle.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {FocusMonitor} from '@angular/cdk/a11y';
9+
import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
1010
import {BooleanInput, coerceBooleanProperty, NumberInput} from '@angular/cdk/coercion';
1111
import {
1212
AfterContentInit,
@@ -253,8 +253,11 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
253253
}
254254

255255
/** Focuses the slide-toggle. */
256-
focus(options?: FocusOptions): void {
257-
this._focusMonitor.focusVia(this._inputElement, 'keyboard', options);
256+
focus(origin?: FocusOrigin, options?: FocusOptions): void {
257+
if (origin) {
258+
this._focusMonitor.focusVia(this._inputElement, origin, options);
259+
}
260+
this._inputElement.nativeElement.focus(options);
258261
}
259262

260263
/** Toggles the checked state of the slide-toggle. */

0 commit comments

Comments
 (0)