Blur search field on escape if empty

This commit is contained in:
shamoon 2024-04-04 23:31:31 -07:00
parent 9d28183e76
commit 8919482511
2 changed files with 13 additions and 1 deletions

View File

@ -232,11 +232,19 @@ describe('GlobalSearchComponent', () => {
component.searchInputKeyDown(new KeyboardEvent('keydown', { key: 'Enter' })) component.searchInputKeyDown(new KeyboardEvent('keydown', { key: 'Enter' }))
expect(primaryActionSpy).toHaveBeenCalled() expect(primaryActionSpy).toHaveBeenCalled()
component.query = 'test'
const resetSpy = jest.spyOn(GlobalSearchComponent.prototype as any, 'reset') const resetSpy = jest.spyOn(GlobalSearchComponent.prototype as any, 'reset')
component.searchInputKeyDown( component.searchInputKeyDown(
new KeyboardEvent('keydown', { key: 'Escape' }) new KeyboardEvent('keydown', { key: 'Escape' })
) )
expect(resetSpy).toHaveBeenCalled() expect(resetSpy).toHaveBeenCalled()
component.query = ''
const blurSpy = jest.spyOn(component.searchInput.nativeElement, 'blur')
component.searchInputKeyDown(
new KeyboardEvent('keydown', { key: 'Escape' })
)
expect(blurSpy).toHaveBeenCalled()
}) })
it('should search on query debounce', fakeAsync(() => { it('should search on query debounce', fakeAsync(() => {

View File

@ -265,7 +265,11 @@ export class GlobalSearchComponent implements OnInit {
) { ) {
this.primaryButtons.first.nativeElement.click() this.primaryButtons.first.nativeElement.click()
} else if (event.key === 'Escape' && !this.resultsDropdown.isOpen()) { } else if (event.key === 'Escape' && !this.resultsDropdown.isOpen()) {
this.reset(true) if (this.query?.length) {
this.reset(true)
} else {
this.searchInput.nativeElement.blur()
}
} }
} }