Skip to content

Commit 3b80d50

Browse files
committed
fix: Do not hide ancestor nodes if they match 🐛 (dowjones#294)
1 parent 100e623 commit 3b80d50

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

.vscode/launch.json

+17
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,29 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "attach",
10+
"name": "Attach by Process ID",
11+
"processId": "${command:PickProcess}"
12+
},
713
{
814
"type": "node",
915
"request": "launch",
1016
"name": "webpack-dev-server",
1117
"program": "${workspaceRoot}/node_modules/webpack-dev-server/bin/webpack-dev-server.js",
1218
"cwd": "${workspaceFolder}/docs"
19+
},
20+
{
21+
"type": "node",
22+
"request": "launch",
23+
"name": "Run AVA test",
24+
"program": "${workspaceFolder}/node_modules/ava/cli.js",
25+
"autoAttachChildProcesses": true,
26+
//"args": ["${file}", "--serial"],
27+
// "args": ["${file}"],
28+
"args": ["${workspaceFolder}\\src\\index.test.js", "--serial"],
29+
"skipFiles": ["<node_internals>/**/*.js"]
1330
}
1431
]
1532
}

src/tree-manager/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class TreeManager {
6767
if (id !== undefined) {
6868
const node = this.getNodeById(id)
6969
this.addParentsToTree(node._parent, tree)
70-
node.hide = true
70+
node.hide = node._isMatch ? node.hide : true
7171
node.matchInChildren = true
7272
tree.set(id, node)
7373
}
@@ -96,6 +96,10 @@ class TreeManager {
9696
matches.forEach(m => {
9797
const node = this.getNodeById(m)
9898
node.hide = false
99+
100+
// add a marker to tell `addParentsToTree` to not hide this node; even if it's an ancestor node
101+
node._isMatch = true
102+
99103
if (keepTreeOnSearch) {
100104
// add parent nodes first or else the tree won't be rendered in correct hierarchy
101105
this.addParentsToTree(node._parent, matchTree)

src/tree-manager/tests/index.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,39 @@ test('should return children when search with `keepChildrenOnSearch`', t => {
604604
const nodes = ['i1', 'c1', 'c2']
605605
nodes.forEach(n => t.not(matchTree.get(n), undefined))
606606
})
607+
608+
test('should not hide parent nodes if they match search term with `keepTreeOnSearch`', t => {
609+
const tree = [
610+
{
611+
id: 'i1',
612+
label: 'A top level item',
613+
value: 'v1',
614+
children: [
615+
{
616+
id: 'c1',
617+
label: 'SeaRch me too',
618+
value: 'l1v1',
619+
children: [
620+
{
621+
id: 'c2',
622+
label: 'search me, please!',
623+
value: 'l2v1',
624+
},
625+
],
626+
},
627+
],
628+
},
629+
]
630+
const manager = new TreeManager({ data: tree })
631+
const keepTreeOnSearch = true
632+
const { allNodesHidden, tree: matchTree } = manager.filterTree('search me', keepTreeOnSearch)
633+
t.false(allNodesHidden)
634+
const nodes = ['c1', 'c2']
635+
nodes.forEach(n => {
636+
t.not(matchTree.get(n), undefined, `${n} should not be undefined.`)
637+
t.false(manager.getNodeById(n).hide, `${n} should not be hidden.`)
638+
})
639+
640+
const hiddenNodes = ['i1']
641+
hiddenNodes.forEach(n => t.true(manager.getNodeById(n).hide, `${n} should not be visible.`))
642+
})

0 commit comments

Comments
 (0)