Skip to content

Commit d19f52b

Browse files
committed
Fix bug in find function
1 parent ab9e53c commit d19f52b

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

src/utils/tree-data-utils.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -851,15 +851,15 @@ export function find({
851851
expandAllMatchPaths = false,
852852
expandFocusMatchPaths = true,
853853
}) {
854-
const matches = [];
855-
854+
let matchCount = 0;
856855
const trav = ({
857856
isPseudoRoot = false,
858857
node,
859858
currentIndex,
860859
path = [],
861860
}) => {
862-
let hasMatch = false;
861+
let matches = [];
862+
let isSelfMatch = false;
863863
let hasFocusMatch = false;
864864
// The pseudo-root is not considered in the path
865865
const selfPath = isPseudoRoot ? [] : [
@@ -881,15 +881,22 @@ export function find({
881881

882882
// Examine the current node to see if it is a match
883883
if (!isPseudoRoot && searchMethod({ ...extraInfo, node: newNode, searchQuery })) {
884-
hasMatch = true;
885-
if (matches.length === searchFocusOffset) {
884+
if (matchCount === searchFocusOffset) {
886885
hasFocusMatch = true;
887886
if ((expandAllMatchPaths || expandFocusMatchPaths) && hasChildren) {
888887
newNode.expanded = true;
889888
}
890889
}
891890

892-
matches.push({ ...extraInfo, node: newNode });
891+
// Keep track of the number of matching nodes, so we know when the searchFocusOffset
892+
// is reached
893+
matchCount++;
894+
895+
// We cannot add this node to the matches right away, as it may be changed
896+
// during the search of the descendants. The entire node is used in
897+
// comparisons between nodes inside the `matches` and `treeData` results
898+
// of this method (`find`)
899+
isSelfMatch = true;
893900
}
894901

895902
if (hasChildren) {
@@ -912,16 +919,15 @@ export function find({
912919
childIndex += 1;
913920
}
914921

915-
if (mapResult.hasMatch || mapResult.hasFocusMatch) {
916-
hasMatch = true;
917-
922+
if (mapResult.matches.length > 0 || mapResult.hasFocusMatch) {
923+
matches = [ ...matches, ...mapResult.matches ];
918924
if (mapResult.hasFocusMatch) {
919925
hasFocusMatch = true;
920926
}
921927

922928
// Expand the current node if it has descendants matching the search
923929
// and the settings are set to do so.
924-
if ((expandAllMatchPaths && mapResult.hasMatch) ||
930+
if ((expandAllMatchPaths && mapResult.matches.length > 0) ||
925931
((expandAllMatchPaths || expandFocusMatchPaths) && mapResult.hasFocusMatch)
926932
) {
927933
newNode.expanded = true;
@@ -930,13 +936,14 @@ export function find({
930936

931937
return mapResult.node;
932938
});
933-
} else {
934-
childIndex += 1;
935939
}
936940

937941
return {
938-
node: hasMatch ? newNode : node,
939-
hasMatch,
942+
node: matches.length > 0 ? newNode : node,
943+
matches: !isSelfMatch ? matches : [
944+
{ ...extraInfo, node: newNode },
945+
...matches,
946+
],
940947
hasFocusMatch,
941948
treeIndex: childIndex,
942949
};
@@ -949,7 +956,7 @@ export function find({
949956
});
950957

951958
return {
952-
matches,
959+
matches: result.matches,
953960
treeData: result.node.children,
954961
};
955962
}

src/utils/tree-data-utils.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,30 @@ describe('find', () => {
18131813
{ node: { key: 42 }, treeIndex: 1, path: [42] },
18141814
]);
18151815
expect(result.matches[commonArgs.searchFocusOffset].treeIndex).toEqual(0);
1816+
1817+
result = find({
1818+
...commonArgs,
1819+
searchFocusOffset: 3,
1820+
treeData: [
1821+
{ key: 1 },
1822+
{ key: 42 },
1823+
{ key: 3 },
1824+
{ key: 3 },
1825+
{ key: 3 },
1826+
{ key: 4 },
1827+
{ key: 42 },
1828+
{ key: 42 },
1829+
{ key: 4 },
1830+
{ key: 42 },
1831+
],
1832+
});
1833+
expect(result.matches).toEqual([
1834+
{ node: { key: 42 }, treeIndex: 1, path: [42] },
1835+
{ node: { key: 42 }, treeIndex: 6, path: [42] },
1836+
{ node: { key: 42 }, treeIndex: 7, path: [42] },
1837+
{ node: { key: 42 }, treeIndex: 9, path: [42] },
1838+
]);
1839+
expect(result.matches[3].treeIndex).toEqual(9);
18161840
});
18171841

18181842
it('should work with nested data', () => {

0 commit comments

Comments
 (0)