Skip to content

Commit 9cd0d44

Browse files
committed
Adapt #62 to work in current codebase
1 parent a712953 commit 9cd0d44

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* [#103]: Add `title` node property and `showNodeTitle` tree property
1010
* [#108]: Add `lang` property for language customization
1111

12+
### Bug Fixes
13+
14+
* [#61]: Fix issue where disabled children would be checked if a parent node was checked
15+
1216
### Other
1317

1418
* [#91]: Prevent disconnection between Sass and Less files on build

src/js/CheckboxTree.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ class CheckboxTree extends React.Component {
173173
const flatNode = this.flatNodes[node.value];
174174

175175
if (flatNode.isLeaf || noCascade) {
176-
if (node.disabled) return;
176+
if (node.disabled) {
177+
return;
178+
}
179+
177180
// Set the check status of a leaf node or an uncoupled parent
178181
this.toggleNode(node.value, 'checked', isChecked);
179182
} else {
@@ -188,20 +191,23 @@ class CheckboxTree extends React.Component {
188191
this.flatNodes[nodeValue][key] = toggleValue;
189192
}
190193

191-
flattenNodes(nodes, parentNode = {}) {
194+
flattenNodes(nodes, parent = {}) {
192195
if (!Array.isArray(nodes) || nodes.length === 0) {
193196
return;
194197
}
195198

199+
const { disabled, noCascade } = this.props;
200+
196201
nodes.forEach((node) => {
197202
const isParent = this.nodeHasChildren(node);
198203

199204
this.flatNodes[node.value] = {
205+
self: node,
206+
parent,
200207
isParent,
201208
isLeaf: !isParent,
202-
parent: parentNode,
203-
self: node,
204209
showCheckbox: node.showCheckbox !== undefined ? node.showCheckbox : true,
210+
disabled: this.getDisabledState(node, parent, disabled, noCascade),
205211
};
206212
this.flattenNodes(node.children, node);
207213
});
@@ -247,7 +253,6 @@ class CheckboxTree extends React.Component {
247253

248254
renderTreeNodes(nodes, parent = {}) {
249255
const {
250-
disabled,
251256
expandDisabled,
252257
expandOnClick,
253258
icons,
@@ -265,7 +270,6 @@ class CheckboxTree extends React.Component {
265270
const key = `${node.value}`;
266271
const flatNode = this.flatNodes[node.value];
267272
const children = flatNode.isParent ? this.renderTreeNodes(node.children, node) : null;
268-
const nodeDisabled = this.getDisabledState(node, parent, disabled, noCascade);
269273

270274
// Get the check state after all children check states are determined
271275
flatNode.checkState = this.getShallowCheckState(node, noCascade);
@@ -285,7 +289,7 @@ class CheckboxTree extends React.Component {
285289
key={key}
286290
checked={flatNode.checkState}
287291
className={node.className}
288-
disabled={nodeDisabled}
292+
disabled={flatNode.disabled}
289293
expandDisabled={expandDisabled}
290294
expandOnClick={expandOnClick}
291295
expanded={flatNode.expanded}

test/CheckboxTree.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,58 @@ describe('<CheckboxTree />', () => {
442442
});
443443

444444
describe('onCheck', () => {
445+
it('should add all children of the checked parent to the checked array', () => {
446+
let actualChecked = null;
447+
448+
const wrapper = mount(
449+
<CheckboxTree
450+
checked={[]}
451+
nodes={[
452+
{
453+
value: 'jupiter',
454+
label: 'Jupiter',
455+
children: [
456+
{ value: 'io', label: 'Io' },
457+
{ value: 'europa', label: 'Europa' },
458+
],
459+
},
460+
]}
461+
onCheck={(checked) => {
462+
actualChecked = checked;
463+
}}
464+
/>,
465+
);
466+
467+
wrapper.find('TreeNode input[type="checkbox"]').simulate('change');
468+
assert.deepEqual(['io', 'europa'], actualChecked);
469+
});
470+
471+
it('should not add disabled children to the checked array', () => {
472+
let actualChecked = null;
473+
474+
const wrapper = mount(
475+
<CheckboxTree
476+
checked={[]}
477+
nodes={[
478+
{
479+
value: 'jupiter',
480+
label: 'Jupiter',
481+
children: [
482+
{ value: 'io', label: 'Io', disabled: true },
483+
{ value: 'europa', label: 'Europa' },
484+
],
485+
},
486+
]}
487+
onCheck={(checked) => {
488+
actualChecked = checked;
489+
}}
490+
/>,
491+
);
492+
493+
wrapper.find('TreeNode input[type="checkbox"]').simulate('change');
494+
assert.deepEqual(['europa'], actualChecked);
495+
});
496+
445497
it('should pass the node toggled as the second parameter', () => {
446498
let actualNode = null;
447499

0 commit comments

Comments
 (0)