Use heuristic suggested by @sophiebits

This commit is contained in:
Dan Abramov
2019-04-10 15:08:13 +01:00
parent 07299828c9
commit 3eca0bbe61
+11 -7
View File
@@ -320,16 +320,20 @@ function reduceSearchState(store: Store, state: State, action: Action): State {
}
}
// Changes in search index or typing should override the selected element.
if (searchIndex !== prevSearchIndex) {
// The user intentionally navigated between search results
didRequestSearch = true;
}
if (
// Did the user type more?
searchText.length > prevSearchText.length &&
searchText.indexOf(prevSearchText) === 0
) {
didRequestSearch = true;
if (searchText !== prevSearchText) {
if (searchResults.indexOf(selectedElementID) === -1) {
// Only move the selection if the new query
// doesn't match the current selection anymore.
didRequestSearch = true;
} else {
// Selected item still matches the new search query.
// Adjust the index to reflect its position in new results.
searchIndex = searchResults.indexOf(selectedElementID);
}
}
if (didRequestSearch) {
if (searchIndex === null) {