Skip to content

Commit 0a4977d

Browse files
authored
Merge pull request #60909 from ishakakkad/59682-search-scroll-to-right
2 parents 7be407b + b07785f commit 0a4977d

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

src/components/Search/SearchRouter/SearchRouter.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {findFocusedRoute, useNavigationState} from '@react-navigation/native';
22
import isEqual from 'lodash/isEqual';
33
import React, {forwardRef, useCallback, useEffect, useRef, useState} from 'react';
4-
import {View} from 'react-native';
4+
import {InteractionManager, View} from 'react-native';
55
import type {TextInputProps} from 'react-native';
66
import {useOnyx} from 'react-native-onyx';
77
import 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

Comments
 (0)