Skip to content

Commit 85aa0b4

Browse files
committed
Merge branch 'v1.8-dev'
2 parents 784b747 + 4f69c32 commit 85aa0b4

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## v1.8.0 (TBA)
4+
5+
### Bug Fixes
6+
7+
* [#258]: Fix check behavior on nodes with an empty `children` array
8+
39
## [v1.7.2](https://github.com/jakezatecky/react-checkbox-tree/compare/v1.7.1...v1.7.2) (2021-08-09)
410

511
### Bug Fixes

src/js/CheckboxTree.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ class CheckboxTree extends React.Component {
183183
determineShallowCheckState(node, noCascade) {
184184
const flatNode = this.state.model.getNode(node.value);
185185

186-
if (flatNode.isLeaf || noCascade) {
186+
if (flatNode.isLeaf || noCascade || node.children.length === 0) {
187+
// Note that an empty parent node tracks its own state
187188
return flatNode.checked ? 1 : 0;
188189
}
189190

src/js/NodeModel.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ class NodeModel {
138138

139139
this.toggleNode(node.value, 'checked', isChecked);
140140
} else {
141-
if (modelHasParents) {
141+
// Toggle parent check status if the model tracks this OR if it is an empty parent
142+
if (modelHasParents || flatNode.children.length === 0) {
142143
this.toggleNode(node.value, 'checked', isChecked);
143144
}
144145

test/CheckboxTree.js

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ describe('<CheckboxTree />', () => {
336336
);
337337
});
338338

339-
it('should render a node with no "children" array as a leaf', () => {
339+
it('should render a node with no `children` array as a leaf', () => {
340340
const wrapper = shallow(
341341
<CheckboxTree
342342
nodes={[
@@ -349,7 +349,7 @@ describe('<CheckboxTree />', () => {
349349
assert.equal(true, wrapper.find(TreeNode).prop('isLeaf'));
350350
});
351351

352-
it('should render a node with an empty "children" array as a parent', () => {
352+
it('should render a node with an empty `children` array as a parent', () => {
353353
const wrapper = shallow(
354354
<CheckboxTree
355355
nodes={[
@@ -366,7 +366,24 @@ describe('<CheckboxTree />', () => {
366366
assert.equal(false, wrapper.find(TreeNode).prop('isLeaf'));
367367
});
368368

369-
it('should render a node with a non-empty "children" array as a parent', () => {
369+
// https://github.com/jakezatecky/react-checkbox-tree/issues/258
370+
it('should render a node with an empty `children` array as unchecked by default', () => {
371+
const wrapper = shallow(
372+
<CheckboxTree
373+
nodes={[
374+
{
375+
value: 'jupiter',
376+
label: 'Jupiter',
377+
children: [],
378+
},
379+
]}
380+
/>,
381+
);
382+
383+
assert.equal(false, wrapper.find(TreeNode).prop('checked'));
384+
});
385+
386+
it('should render a node with a non-empty `children` array as a parent', () => {
370387
const wrapper = shallow(
371388
<CheckboxTree
372389
nodes={[
@@ -734,6 +751,38 @@ describe('<CheckboxTree />', () => {
734751
assert.deepEqual(['io', 'europa'], actualChecked);
735752
});
736753

754+
// https://github.com/jakezatecky/react-checkbox-tree/issues/258
755+
it('should toggle a node with an empty `children` array', () => {
756+
let actualChecked = {};
757+
const makeEmptyParentNode = (checked) => (
758+
mount(
759+
<CheckboxTree
760+
nodes={[
761+
{
762+
value: 'jupiter',
763+
label: 'Jupiter',
764+
children: [],
765+
},
766+
]}
767+
checked={checked}
768+
onCheck={(node) => {
769+
actualChecked = node;
770+
}}
771+
/>,
772+
)
773+
);
774+
775+
// Unchecked to checked
776+
let wrapper = makeEmptyParentNode([]);
777+
wrapper.find('TreeNode input[type="checkbox"]').simulate('click');
778+
assert.deepEqual(['jupiter'], actualChecked);
779+
780+
// Checked to unchecked
781+
wrapper = makeEmptyParentNode(['jupiter']);
782+
wrapper.find('TreeNode input[type="checkbox"]').simulate('click');
783+
assert.deepEqual([], actualChecked);
784+
});
785+
737786
it('should not add disabled children to the checked array', () => {
738787
let actualChecked = null;
739788

0 commit comments

Comments
 (0)