feat(aio): focus search when /
is pressed (#16636)
This will aid accessibility. Closes #16129
This commit is contained in:
parent
c5ce0408a1
commit
041f57cb7d
@ -56,6 +56,12 @@ describe('SearchBoxComponent', () => {
|
|||||||
input.triggerEventHandler('keyup', { target: { value: 'some query' }, which: 27 });
|
input.triggerEventHandler('keyup', { target: { value: 'some query' }, which: 27 });
|
||||||
expect(search.search).not.toHaveBeenCalled();
|
expect(search.search).not.toHaveBeenCalled();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
it('should grab focus when the / key is pressed', () => {
|
||||||
|
const input = fixture.debugElement.query(By.css('input'));
|
||||||
|
window.document.dispatchEvent(new KeyboardEvent('keyup', { 'key': '/' }));
|
||||||
|
expect(document.activeElement).toBe(input.nativeElement, 'Search box should be active element');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('on focus', () => {
|
describe('on focus', () => {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
|
import { Component, OnInit, ViewChild, ElementRef, HostListener } from '@angular/core';
|
||||||
import { SearchService } from 'app/search/search.service';
|
import { SearchService } from 'app/search/search.service';
|
||||||
import { LocationService } from 'app/shared/location.service';
|
import { LocationService } from 'app/shared/location.service';
|
||||||
|
|
||||||
@ -46,4 +46,11 @@ export class SearchBoxComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
this.searchService.search(query);
|
this.searchService.search(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@HostListener('document:keyup', ['$event'])
|
||||||
|
onKeyUp($event: KeyboardEvent) {
|
||||||
|
if ($event.key === '/') {
|
||||||
|
this.searchBox.nativeElement.focus();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user