@@ -876,16 +876,10 @@ export function find({
876876 typeof node . children !== 'function' &&
877877 node . children . length > 0 ;
878878
879- let childIndex = currentIndex ;
880- const newNode = { ...node } ;
881-
882879 // 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 } ) ) {
884881 if ( matchCount === searchFocusOffset ) {
885882 hasFocusMatch = true ;
886- if ( ( expandAllMatchPaths || expandFocusMatchPaths ) && hasChildren ) {
887- newNode . expanded = true ;
888- }
889883 }
890884
891885 // Keep track of the number of matching nodes, so we know when the searchFocusOffset
@@ -899,6 +893,8 @@ export function find({
899893 isSelfMatch = true ;
900894 }
901895
896+ let childIndex = currentIndex ;
897+ const newNode = { ...node } ;
902898 if ( hasChildren ) {
903899 // Get all descendants
904900 newNode . children = newNode . children . map ( ( child ) => {
@@ -938,12 +934,26 @@ export function find({
938934 } ) ;
939935 }
940936
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 = [
944949 { ...extraInfo , node : newNode } ,
945950 ...matches ,
946- ] ,
951+ ] ;
952+ }
953+
954+ return {
955+ node : matches . length > 0 ? newNode : node ,
956+ matches,
947957 hasFocusMatch,
948958 treeIndex : childIndex ,
949959 } ;
0 commit comments