Skip to content

Prevent all node to be checked when filtering tree #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/js/CheckboxTree.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import classNames from 'classnames';
import isEqual from 'lodash/isEqual';
import nanoid from 'nanoid';
import { nanoid } from 'nanoid';
import PropTypes from 'prop-types';
import React from 'react';

Expand Down Expand Up @@ -193,6 +193,10 @@ class CheckboxTree extends React.Component {
}

isEveryChildChecked(node) {
if (!node.children || node.children.length === 0) {
return this.state.model.getNode(node.value).checked;
}

return node.children.every(
(child) => this.state.model.getNode(child.value).checkState === 1,
);
Expand Down
6 changes: 5 additions & 1 deletion src/js/NodeModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class NodeModel {
}

nodeHasChildren(node) {
return Array.isArray(node.children);
return Array.isArray(node.children) && node.children.length > 0;
}

getDisabledState(node, parent, disabledProp, noCascade) {
Expand Down Expand Up @@ -163,6 +163,10 @@ class NodeModel {
}

isEveryChildChecked(node) {
if (!node.children || node.children.length === 0) {
return this.getNode(node.value).checked;
}

return node.children.every((child) => this.getNode(child.value).checked);
}

Expand Down
4 changes: 2 additions & 2 deletions test/CheckboxTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ describe('<CheckboxTree />', () => {
/>,
);

assert.equal(true, wrapper.find(TreeNode).prop('isParent'));
assert.equal(false, wrapper.find(TreeNode).prop('isLeaf'));
assert.equal(false, wrapper.find(TreeNode).prop('isParent'));
assert.equal(true, wrapper.find(TreeNode).prop('isLeaf'));
});

it('should render a node with a non-empty "children" array as a parent', () => {
Expand Down