Skip to content

Commit 2d2555b

Browse files
authored
Merge pull request #52606 from wildan-m/wildan/fix/50562-fix-list-not-scrolled-up-when-query-empty
Fix: User not scrolled to top of chats list when scrolling down and deleting search
2 parents 1b2643f + 6284cb8 commit 2d2555b

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/components/Search/SearchRouter/SearchRouter.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {useNavigationState} from '@react-navigation/native';
22
import {Str} from 'expensify-common';
3+
import isEmpty from 'lodash/isEmpty';
34
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
45
import {View} from 'react-native';
56
import type {TextInputProps} from 'react-native';
@@ -323,6 +324,7 @@ function SearchRouter({onRouterClose, shouldHideInputCaret}: SearchRouterProps)
323324
],
324325
);
325326

327+
const prevUserQueryRef = useRef<string | null>(null);
326328
useEffect(() => {
327329
Report.searchInServer(debouncedInputValue.trim());
328330
}, [debouncedInputValue]);
@@ -340,11 +342,14 @@ function SearchRouter({onRouterClose, shouldHideInputCaret}: SearchRouterProps)
340342
const updatedSubstitutionsMap = getUpdatedSubstitutionsMap(userQuery, autocompleteSubstitutions);
341343
setAutocompleteSubstitutions(updatedSubstitutionsMap);
342344

343-
if (newUserQuery) {
345+
if (newUserQuery || !isEmpty(prevUserQueryRef.current)) {
344346
listRef.current?.updateAndScrollToFocusedIndex(0);
345347
} else {
346348
listRef.current?.updateAndScrollToFocusedIndex(-1);
347349
}
350+
351+
// Store the previous newUserQuery
352+
prevUserQueryRef.current = newUserQuery;
348353
},
349354
[autocompleteSubstitutions, autocompleteSuggestions, setTextInputValue, updateAutocomplete],
350355
);

src/components/SelectionList/BaseSelectionList.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,13 @@ function BaseSelectionList<TItem extends ListItem>(
645645
) {
646646
return;
647647
}
648-
// Remove the focus if the search input is empty or selected options length is changed (and allOptions length remains the same)
648+
// Remove the focus if the search input is empty and prev search input not empty or selected options length is changed (and allOptions length remains the same)
649649
// else focus on the first non disabled item
650650
const newSelectedIndex =
651-
textInputValue === '' || (flattenedSections.selectedOptions.length !== prevSelectedOptionsLength && prevAllOptionsLength === flattenedSections.allOptions.length) ? -1 : 0;
651+
(isEmpty(prevTextInputValue) && textInputValue === '') ||
652+
(flattenedSections.selectedOptions.length !== prevSelectedOptionsLength && prevAllOptionsLength === flattenedSections.allOptions.length)
653+
? -1
654+
: 0;
652655

653656
// reseting the currrent page to 1 when the user types something
654657
setCurrentPage(1);

0 commit comments

Comments
 (0)