Feature: tweak search behaviour (#353)

Merge PR #353 from @fanny

This change changes search beahvior to initially select the result nearest the currently selected element (rather than selecting the first result in the set).
This commit is contained in:
Fanny
2019-07-27 09:04:20 -07:00
committed by Brian Vaughn
parent 3b2905b690
commit 29a6bf2a7b
+14 -9
View File
@@ -277,7 +277,6 @@ function reduceSearchState(store: Store, state: State, action: Action): State {
selectedElementIndex,
} = state;
const prevSearchIndex = searchIndex;
const prevSearchText = searchText;
const numPrevSearchResults = searchResults.length;
@@ -380,15 +379,11 @@ function reduceSearchState(store: Store, state: State, action: Action): State {
store.roots.forEach(rootID => {
recursivelySearchTree(store, rootID, regExp, searchResults);
});
if (searchResults.length > 0) {
if (prevSearchIndex === null) {
searchIndex = 0;
if (selectedElementID !== null) {
searchIndex = getNearestResult(searchResults, selectedElementID);
} else {
searchIndex = Math.min(
((prevSearchIndex: any): number),
searchResults.length - 1
);
searchIndex = 0;
}
}
}
@@ -760,7 +755,6 @@ function TreeContextController({
</TreeStateContext.Provider>
);
}
function recursivelySearchTree(
store: Store,
elementID: number,
@@ -780,4 +774,15 @@ function recursivelySearchTree(
);
}
function getNearestResult(
searchResults: Array<number>,
selectedElementID: number | null
) {
const result = searchResults.findIndex(
value => value >= ((selectedElementID: any): number)
);
return result === -1 ? 0 : result;
}
export { TreeDispatcherContext, TreeStateContext, TreeContextController };