From 3eca0bbe6144dcb2d542766e323bf3c6f005cd08 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 10 Apr 2019 15:08:13 +0100 Subject: [PATCH] Use heuristic suggested by @sophiebits --- src/devtools/views/Components/TreeContext.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/devtools/views/Components/TreeContext.js b/src/devtools/views/Components/TreeContext.js index 41dd0eb518..92b59770cf 100644 --- a/src/devtools/views/Components/TreeContext.js +++ b/src/devtools/views/Components/TreeContext.js @@ -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) {