Skip to content

Commit

Permalink
Merge pull request #52606 from wildan-m/wildan/fix/50562-fix-list-not…
Browse files Browse the repository at this point in the history
…-scrolled-up-when-query-empty

Fix: User not scrolled to top of chats list when scrolling down and deleting search
  • Loading branch information
rlinoz authored Nov 20, 2024
2 parents 1b2643f + 6284cb8 commit 2d2555b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/components/Search/SearchRouter/SearchRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useNavigationState} from '@react-navigation/native';
import {Str} from 'expensify-common';
import isEmpty from 'lodash/isEmpty';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {View} from 'react-native';
import type {TextInputProps} from 'react-native';
Expand Down Expand Up @@ -323,6 +324,7 @@ function SearchRouter({onRouterClose, shouldHideInputCaret}: SearchRouterProps)
],
);

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

if (newUserQuery) {
if (newUserQuery || !isEmpty(prevUserQueryRef.current)) {
listRef.current?.updateAndScrollToFocusedIndex(0);
} else {
listRef.current?.updateAndScrollToFocusedIndex(-1);
}

// Store the previous newUserQuery
prevUserQueryRef.current = newUserQuery;
},
[autocompleteSubstitutions, autocompleteSuggestions, setTextInputValue, updateAutocomplete],
);
Expand Down
7 changes: 5 additions & 2 deletions src/components/SelectionList/BaseSelectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -645,10 +645,13 @@ function BaseSelectionList<TItem extends ListItem>(
) {
return;
}
// Remove the focus if the search input is empty or selected options length is changed (and allOptions length remains the same)
// 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)
// else focus on the first non disabled item
const newSelectedIndex =
textInputValue === '' || (flattenedSections.selectedOptions.length !== prevSelectedOptionsLength && prevAllOptionsLength === flattenedSections.allOptions.length) ? -1 : 0;
(isEmpty(prevTextInputValue) && textInputValue === '') ||
(flattenedSections.selectedOptions.length !== prevSelectedOptionsLength && prevAllOptionsLength === flattenedSections.allOptions.length)
? -1
: 0;

// reseting the currrent page to 1 when the user types something
setCurrentPage(1);
Expand Down

0 comments on commit 2d2555b

Please sign in to comment.