diff --git a/plugins/search/src/events/formHandlers.js b/plugins/search/src/events/formHandlers.js index f16ebff2..87912340 100755 --- a/plugins/search/src/events/formHandlers.js +++ b/plugins/search/src/events/formHandlers.js @@ -60,9 +60,7 @@ export const createFormHandlers = ({ } if (newSuggestions.length) { - if (appState.interfaceType === 'keyboard') { - viewportRef.current?.focus() - } + viewportRef.current?.focus() if (appState.breakpoint === 'mobile') { dispatch({ type: 'TOGGLE_EXPANDED', payload: false }) services.eventBus.emit('search:close') diff --git a/plugins/search/src/events/formHandlers.test.js b/plugins/search/src/events/formHandlers.test.js index 6a36b928..bb01bca4 100644 --- a/plugins/search/src/events/formHandlers.test.js +++ b/plugins/search/src/events/formHandlers.test.js @@ -122,7 +122,7 @@ describe('createFormHandlers', () => { expect(updateMap).not.toHaveBeenCalled() }) - test('handleSubmit fetches suggestions and updates map (keyboard)', async () => { + test('handleSubmit fetches suggestions and updates map', async () => { fetchSuggestions.mockResolvedValueOnce({ sanitisedValue: 'rome', results: [ @@ -132,7 +132,7 @@ describe('createFormHandlers', () => { await handlers.handleSubmit( { preventDefault: jest.fn() }, - { interfaceType: 'keyboard' }, + {}, { suggestions: [], selectedIndex: -1, diff --git a/plugins/search/src/events/suggestionHandlers.js b/plugins/search/src/events/suggestionHandlers.js index 4ced5845..35c3a383 100755 --- a/plugins/search/src/events/suggestionHandlers.js +++ b/plugins/search/src/events/suggestionHandlers.js @@ -1,6 +1,6 @@ import { updateMap } from '../utils/updateMap.js' -export const createSuggestionHandlers = ({ dispatch, services, mapProvider, markers, showMarker, markerOptions }) => { +export const createSuggestionHandlers = ({ dispatch, services, mapProvider, markers, showMarker, markerOptions, viewportRef }) => { const selectionMessage = (suggestions, index) => index >= 0 ? `${suggestions[index]?.text}. ${index + 1} of ${suggestions.length} is highlighted` @@ -17,6 +17,7 @@ export const createSuggestionHandlers = ({ dispatch, services, mapProvider, mark services.eventBus.emit('search:close') } + viewportRef.current?.focus() updateMap({ mapProvider, bounds: suggestion.bounds, point: suggestion.point, markers, showMarker, markerOptions }) services.eventBus.emit('search:match', { query: suggestion.text, ...suggestion }) }, diff --git a/plugins/search/src/events/suggestionHandlers.test.js b/plugins/search/src/events/suggestionHandlers.test.js index 4e5c7ae2..8065078b 100644 --- a/plugins/search/src/events/suggestionHandlers.test.js +++ b/plugins/search/src/events/suggestionHandlers.test.js @@ -9,6 +9,7 @@ jest.mock('../utils/updateMap.js') describe('createSuggestionHandlers', () => { let dispatch let services + let viewportRef let handlers beforeEach(() => { @@ -19,13 +20,16 @@ describe('createSuggestionHandlers', () => { announce: jest.fn() } + viewportRef = { current: { focus: jest.fn() } } + handlers = createSuggestionHandlers({ dispatch, services, mapProvider: 'map', markers: 'markers', showMarker: true, - markerOptions: { backgroundColor: 'blue' } + markerOptions: { backgroundColor: 'blue' }, + viewportRef }) jest.clearAllMocks() @@ -41,6 +45,7 @@ describe('createSuggestionHandlers', () => { expect(dispatch).toHaveBeenCalledWith({ type: 'SET_VALUE', payload: 'Paris' }) expect(dispatch).toHaveBeenCalledWith({ type: 'HIDE_SUGGESTIONS' }) expect(dispatch).toHaveBeenCalledWith({ type: 'SET_SELECTED', payload: -1 }) + expect(viewportRef.current.focus).toHaveBeenCalled() expect(updateMap).toHaveBeenCalledWith(expect.objectContaining({ bounds: 'b', point: 'p' })) expect(services.eventBus.emit).toHaveBeenCalledWith( 'search:match', @@ -55,6 +60,7 @@ describe('createSuggestionHandlers', () => { expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_EXPANDED', payload: false }) expect(services.eventBus.emit).toHaveBeenCalledWith('search:close') + expect(viewportRef.current.focus).toHaveBeenCalled() }) // ---------- ArrowDown ----------