Skip to content

Commit

Permalink
fix: Do not hide ancestor nodes if they match 🐛 (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchief committed Sep 29, 2019
1 parent 100e623 commit 3b80d50
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach by Process ID",
"processId": "${command:PickProcess}"
},
{
"type": "node",
"request": "launch",
"name": "webpack-dev-server",
"program": "${workspaceRoot}/node_modules/webpack-dev-server/bin/webpack-dev-server.js",
"cwd": "${workspaceFolder}/docs"
},
{
"type": "node",
"request": "launch",
"name": "Run AVA test",
"program": "${workspaceFolder}/node_modules/ava/cli.js",
"autoAttachChildProcesses": true,
//"args": ["${file}", "--serial"],
// "args": ["${file}"],
"args": ["${workspaceFolder}\\src\\index.test.js", "--serial"],
"skipFiles": ["<node_internals>/**/*.js"]
}
]
}
6 changes: 5 additions & 1 deletion src/tree-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class TreeManager {
if (id !== undefined) {
const node = this.getNodeById(id)
this.addParentsToTree(node._parent, tree)
node.hide = true
node.hide = node._isMatch ? node.hide : true
node.matchInChildren = true
tree.set(id, node)
}
Expand Down Expand Up @@ -96,6 +96,10 @@ class TreeManager {
matches.forEach(m => {
const node = this.getNodeById(m)
node.hide = false

// add a marker to tell `addParentsToTree` to not hide this node; even if it's an ancestor node
node._isMatch = true

if (keepTreeOnSearch) {
// add parent nodes first or else the tree won't be rendered in correct hierarchy
this.addParentsToTree(node._parent, matchTree)
Expand Down
36 changes: 36 additions & 0 deletions src/tree-manager/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,39 @@ test('should return children when search with `keepChildrenOnSearch`', t => {
const nodes = ['i1', 'c1', 'c2']
nodes.forEach(n => t.not(matchTree.get(n), undefined))
})

test('should not hide parent nodes if they match search term with `keepTreeOnSearch`', t => {
const tree = [
{
id: 'i1',
label: 'A top level item',
value: 'v1',
children: [
{
id: 'c1',
label: 'SeaRch me too',
value: 'l1v1',
children: [
{
id: 'c2',
label: 'search me, please!',
value: 'l2v1',
},
],
},
],
},
]
const manager = new TreeManager({ data: tree })
const keepTreeOnSearch = true
const { allNodesHidden, tree: matchTree } = manager.filterTree('search me', keepTreeOnSearch)
t.false(allNodesHidden)
const nodes = ['c1', 'c2']
nodes.forEach(n => {
t.not(matchTree.get(n), undefined, `${n} should not be undefined.`)
t.false(manager.getNodeById(n).hide, `${n} should not be hidden.`)
})

const hiddenNodes = ['i1']
hiddenNodes.forEach(n => t.true(manager.getNodeById(n).hide, `${n} should not be visible.`))
})

0 comments on commit 3b80d50

Please sign in to comment.