Skip to content

Commit

Permalink
fix: Allow same option to be reselected right after it's deselected (#…
Browse files Browse the repository at this point in the history
…129) 🐛

Closes #128
  • Loading branch information
mrchief authored Jun 30, 2018
1 parent c801e60 commit 7c34430
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/bundle.js

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/tree-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,18 @@ class TreeManager {

togglePreviousChecked(id) {
const prevChecked = this.currentChecked
if (prevChecked) this.getNodeById(prevChecked).checked = false

// if id is same as previously selected node, then do nothing (since it's state is already set correctly by setNodeCheckedState)
// but if they ar not same, then toggle the previous one
if (prevChecked && prevChecked !== id) this.getNodeById(prevChecked).checked = false

this.currentChecked = id

console.table({ id, prevChecked, currentChecked: this.currentChecked })
}

setNodeCheckedState(id, checked) {
console.table({ id, checked })
const node = this.getNodeById(id)
node.checked = checked

Expand Down
19 changes: 19 additions & 0 deletions src/tree-manager/tests/simpleSelect.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import test from 'ava'
import TreeManager from '..'

test('should select the same node if it is selected and then deselected right again', t => {
const tree = [{ id: 'nodeA' }, { id: 'nodeB' }, { id: 'nodeC' }]

const manager = new TreeManager(tree, true)

// first select a node
manager.setNodeCheckedState('nodeA', true)

// then deselect the same node
manager.setNodeCheckedState('nodeA', false)

// then reselect the same node right away
manager.setNodeCheckedState('nodeA', true)

t.true(manager.getNodeById('nodeA').checked)
})

0 comments on commit 7c34430

Please sign in to comment.