mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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:
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user