@@ -876,16 +876,10 @@ export function find({
876
876
typeof node . children !== 'function' &&
877
877
node . children . length > 0 ;
878
878
879
- let childIndex = currentIndex ;
880
- const newNode = { ...node } ;
881
-
882
879
// Examine the current node to see if it is a match
883
- if ( ! isPseudoRoot && searchMethod ( { ...extraInfo , node : newNode , searchQuery } ) ) {
880
+ if ( ! isPseudoRoot && searchMethod ( { ...extraInfo , node, searchQuery } ) ) {
884
881
if ( matchCount === searchFocusOffset ) {
885
882
hasFocusMatch = true ;
886
- if ( ( expandAllMatchPaths || expandFocusMatchPaths ) && hasChildren ) {
887
- newNode . expanded = true ;
888
- }
889
883
}
890
884
891
885
// Keep track of the number of matching nodes, so we know when the searchFocusOffset
@@ -899,6 +893,8 @@ export function find({
899
893
isSelfMatch = true ;
900
894
}
901
895
896
+ let childIndex = currentIndex ;
897
+ const newNode = { ...node } ;
902
898
if ( hasChildren ) {
903
899
// Get all descendants
904
900
newNode . children = newNode . children . map ( ( child ) => {
@@ -938,12 +934,26 @@ export function find({
938
934
} ) ;
939
935
}
940
936
941
- return {
942
- node : matches . length > 0 ? newNode : node ,
943
- matches : ! isSelfMatch ? matches : [
937
+ // Cannot assign a treeIndex to hidden nodes
938
+ if ( ! isPseudoRoot && ! newNode . expanded ) {
939
+ matches = matches . map ( match => ( {
940
+ ...match ,
941
+ treeIndex : null ,
942
+ } ) ) ;
943
+ }
944
+
945
+ // Add this node to the matches if it fits the search criteria.
946
+ // This is performed at the last minute so newNode can be sent in its final form.
947
+ if ( isSelfMatch ) {
948
+ matches = [
944
949
{ ...extraInfo , node : newNode } ,
945
950
...matches ,
946
- ] ,
951
+ ] ;
952
+ }
953
+
954
+ return {
955
+ node : matches . length > 0 ? newNode : node ,
956
+ matches,
947
957
hasFocusMatch,
948
958
treeIndex : childIndex ,
949
959
} ;
0 commit comments