diff --git a/src/components/Searchbar/Searchbar.tsx b/src/components/Searchbar/Searchbar.tsx index 70c66e362a..3998f01af4 100644 --- a/src/components/Searchbar/Searchbar.tsx +++ b/src/components/Searchbar/Searchbar.tsx @@ -298,6 +298,7 @@ const Searchbar = ({ const handleClearPress = (e: any) => { root.current?.clear(); + root.current?.focus(); rest.onChangeText?.(''); onClearIconPress?.(e); }; @@ -465,10 +466,8 @@ const Searchbar = ({ {loading ? ( ) : ( - // Clear icon should be always rendered within Searchbar – it's transparent, - // without touch events, when there is no value. It's done to avoid issues - // with the abruptly stopping ripple effect and changing bar width on web, - // when clearing the value. + // Keep the clear button mounted so its ripple can finish and clearing + // the value does not cause a layout shift on web. { + const { rerender } = await render(); + + expect(screen.queryByRole('button', { name: 'clear' })).toBeNull(); + expect( + screen.getByLabelText('clear', { includeHiddenElements: true }) + ).toHaveProp('aria-hidden', true); + + await rerender(); + + const clearButton = screen.getByRole('button', { name: 'clear' }); + expect(clearButton).toBeEnabled(); + + await rerender(); + + expect(screen.queryByRole('button', { name: 'clear' })).toBeNull(); + const hiddenClearButton = screen.getByLabelText('clear', { + includeHiddenElements: true, + }); + expect(hiddenClearButton).toBe(clearButton); + expect(hiddenClearButton).toHaveProp('focusable', false); + expect(hiddenClearButton).toHaveProp('tabIndex', -1); +}); + +it('returns focus to the search input after clearing', async () => { + const focusSpy = jest.spyOn(NativeTextInput.prototype, 'focus'); + const onChangeText = jest.fn(); + try { + await render(); + await userEvent.press(screen.getByRole('button', { name: 'clear' })); + + expect(onChangeText).toHaveBeenCalledWith(''); + expect(focusSpy).toHaveBeenCalledTimes(1); + } finally { + focusSpy.mockRestore(); + } +}); + it('defines onClearIconPress action and checks if it is called when close button is pressed', async () => { const onClearIconPressMock = jest.fn(); await render( diff --git a/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap b/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap index 678654f738..9c49cabe32 100644 --- a/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap @@ -745,9 +745,10 @@ exports[`fills a custom vertical layout {"flex":1} 1`] = ` } } accessible={true} + aria-hidden={true} centered={true} collapsable={false} - focusable={true} + focusable={false} hitSlop={ { "bottom": 4, @@ -787,6 +788,7 @@ exports[`fills a custom vertical layout {"flex":1} 1`] = ` ], ] } + tabIndex={-1} >