From ecc356eceaa0602fc50d9b26373b915218a19bca Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Fri, 12 May 2017 15:04:28 +0100 Subject: [PATCH] fix(aio): set focus back to search box on "escape" For improved accessibility, the focus should be sent back to the input box when we are displaying the search results and the user hits ESC. See #16005 --- aio/src/app/app.component.spec.ts | 9 +++++++++ aio/src/app/app.component.ts | 1 + 2 files changed, 10 insertions(+) diff --git a/aio/src/app/app.component.spec.ts b/aio/src/app/app.component.spec.ts index 7dd6487b45..ac4c39ab7e 100644 --- a/aio/src/app/app.component.spec.ts +++ b/aio/src/app/app.component.spec.ts @@ -504,6 +504,15 @@ describe('AppComponent', () => { fixture.detectChanges(); expect(searchBox.focus).toHaveBeenCalled(); }); + + it('should set focus back to the search box when the search results are displayed and the escape key is pressed', () => { + const searchBox: SearchBoxComponent = fixture.debugElement.query(By.directive(SearchBoxComponent)).componentInstance; + spyOn(searchBox, 'focus'); + component.showSearchResults = true; + window.document.dispatchEvent(new KeyboardEvent('keyup', { 'key': 'Escape' })); + fixture.detectChanges(); + expect(searchBox.focus).toHaveBeenCalled(); + }); }); describe('showing search results', () => { diff --git a/aio/src/app/app.component.ts b/aio/src/app/app.component.ts index 71efaa41df..a574afdacf 100644 --- a/aio/src/app/app.component.ts +++ b/aio/src/app/app.component.ts @@ -268,6 +268,7 @@ export class AppComponent implements OnInit { // escape key if (this.showSearchResults) { this.hideSearchResults(); + this.focusSearchBox(); } } }