From 9ae1f4cd5cd04a01b99fc35ec25841c7dd690a43 Mon Sep 17 00:00:00 2001 From: Jessica Xu Date: Mon, 26 Oct 2020 16:25:28 -0700 Subject: [PATCH] feat(material/button): allow focus origin to be optional input in focus method --- src/material/button/button.spec.ts | 17 +++++++++++++++++ src/material/button/button.ts | 8 ++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/material/button/button.spec.ts b/src/material/button/button.spec.ts index fa395a6d5b44..c4bd26d691af 100644 --- a/src/material/button/button.spec.ts +++ b/src/material/button/button.spec.ts @@ -84,6 +84,23 @@ describe('MatButton', () => { expect(buttonDebugEl.nativeElement.classList).toContain('cdk-touch-focused'); }); + it('should not change focus origin if origin not specified', () => { + const fixture = TestBed.createComponent(TestApp); + fixture.detectChanges(); + + const fabButtonDebugEl = fixture.debugElement.query(By.css('button[mat-fab]'))!; + const fabButtonInstance = fabButtonDebugEl.componentInstance as MatButton; + fabButtonInstance.focus('mouse'); + + const miniFabButtonDebugEl = fixture.debugElement.query(By.css('button[mat-mini-fab]'))!; + const miniFabButtonInstance = miniFabButtonDebugEl.componentInstance as MatButton; + + miniFabButtonInstance.focus(); + + expect(miniFabButtonDebugEl.nativeElement.classList).toContain('cdk-focused'); + expect(miniFabButtonDebugEl.nativeElement.classList).toContain('cdk-mouse-focused'); + }); + describe('button[mat-fab]', () => { it('should have accent palette by default', () => { const fixture = TestBed.createComponent(TestApp); diff --git a/src/material/button/button.ts b/src/material/button/button.ts index f587d2678a2a..4db741e3f9fd 100644 --- a/src/material/button/button.ts +++ b/src/material/button/button.ts @@ -127,8 +127,12 @@ export class MatButton extends _MatButtonMixinBase } /** Focuses the button. */ - focus(origin: FocusOrigin = 'program', options?: FocusOptions): void { - this._focusMonitor.focusVia(this._getHostElement(), origin, options); + focus(origin?: FocusOrigin, options?: FocusOptions): void { + if (origin) { + this._focusMonitor.focusVia(this._getHostElement(), origin, options); + } else { + this._getHostElement().focus(options); + } } _getHostElement() {