11import { findFocusedRoute , useNavigationState } from '@react-navigation/native' ;
22import isEqual from 'lodash/isEqual' ;
33import React , { forwardRef , useCallback , useEffect , useRef , useState } from 'react' ;
4- import { View } from 'react-native' ;
4+ import { InteractionManager , View } from 'react-native' ;
55import type { TextInputProps } from 'react-native' ;
66import { useOnyx } from 'react-native-onyx' ;
77import type { ValueOf } from 'type-fest' ;
@@ -188,7 +188,7 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
188188
189189 scrollToRight ( textInputRef . current ) ;
190190 shouldScrollRef . current = false ;
191- } , [ ] ) ;
191+ } , [ textInputValue ] ) ;
192192
193193 const onSearchQueryChange = useCallback (
194194 ( userQuery : string , autoScrollToRight = false ) => {
@@ -234,13 +234,24 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
234234 const setTextAndUpdateSelection = useCallback (
235235 ( text : string ) => {
236236 setTextInputValue ( text ) ;
237+ shouldScrollRef . current = true ;
237238 setSelection ( { start : text . length , end : text . length } ) ;
238239 } ,
239240 [ setSelection , setTextInputValue ] ,
240241 ) ;
241242
242243 const onListItemPress = useCallback (
243244 ( item : OptionData | SearchQueryItem ) => {
245+ const setFocusAndScrollToRight = ( ) => {
246+ InteractionManager . runAfterInteractions ( ( ) => {
247+ if ( ! textInputRef . current ) {
248+ return ;
249+ }
250+ textInputRef . current . focus ( ) ;
251+ scrollToRight ( textInputRef . current ) ;
252+ } ) ;
253+ } ;
254+
244255 if ( isSearchQueryItem ( item ) ) {
245256 if ( ! item . searchQuery ) {
246257 return ;
@@ -258,10 +269,11 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
258269
259270 setAutocompleteSubstitutions ( substitutions ) ;
260271 }
272+ setFocusAndScrollToRight ( ) ;
261273 } else if ( item . searchItemType === CONST . SEARCH . SEARCH_ROUTER_ITEM_TYPE . AUTOCOMPLETE_SUGGESTION && textInputValue ) {
262274 const trimmedUserSearchQuery = getQueryWithoutAutocompletedPart ( textInputValue ) ;
263275 const newSearchQuery = `${ trimmedUserSearchQuery } ${ sanitizeSearchValue ( item . searchQuery ) } \u00A0` ;
264- onSearchQueryChange ( newSearchQuery ) ;
276+ onSearchQueryChange ( newSearchQuery , true ) ;
265277 setSelection ( { start : newSearchQuery . length , end : newSearchQuery . length } ) ;
266278
267279 if ( item . mapKey && item . autocompleteID ) {
@@ -270,7 +282,7 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
270282 setAutocompleteSubstitutions ( substitutions ) ;
271283 }
272284 // needed for android mWeb
273- textInputRef . current ?. focus ( ) ;
285+ setFocusAndScrollToRight ( ) ;
274286 } else {
275287 submitSearch ( item . searchQuery ) ;
276288 }
0 commit comments