Skip to content
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

Support useTopNode option #138

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
35 changes: 30 additions & 5 deletions src/js/CheckboxTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CheckboxTree extends React.Component {
showExpandAll: PropTypes.bool,
showNodeIcon: PropTypes.bool,
showNodeTitle: PropTypes.bool,
useTopNode: PropTypes.bool,
onCheck: PropTypes.func,
onClick: PropTypes.func,
onExpand: PropTypes.func,
Expand Down Expand Up @@ -71,9 +72,10 @@ class CheckboxTree extends React.Component {
showExpandAll: false,
showNodeIcon: true,
showNodeTitle: false,
onCheck: () => {},
onCheck: () => { },
onClick: null,
onExpand: () => {},
onExpand: () => { },
useTopNode: false,
};

constructor(props) {
Expand Down Expand Up @@ -125,11 +127,11 @@ class CheckboxTree extends React.Component {
}

onCheck(nodeInfo) {
const { noCascade, onCheck } = this.props;
const { noCascade, onCheck, useTopNode } = this.props;
const model = this.state.model.clone();
const node = model.getNode(nodeInfo.value);

model.toggleChecked(nodeInfo, nodeInfo.checked, noCascade);
model.toggleChecked(nodeInfo, nodeInfo.checked, noCascade, useTopNode);
onCheck(model.serializeList('checked'), { ...node, ...nodeInfo });
}

Expand Down Expand Up @@ -169,9 +171,14 @@ class CheckboxTree extends React.Component {
}

determineShallowCheckState(node, noCascade) {
const { useTopNode } = this.props;
const flatNode = this.state.model.getNode(node.value);

if (flatNode.isLeaf || noCascade) {
if (useTopNode && this.isAnyParentChecked(flatNode)) {
return 2;
}

if (useTopNode || flatNode.isLeaf || noCascade) {
return flatNode.checked ? 1 : 0;
}

Expand All @@ -186,6 +193,17 @@ class CheckboxTree extends React.Component {
return 0;
}

isAnyParentChecked(node) {
if (Object.entries(node.parent).length === 0 && node.parent.constructor === Object) {
return false;
}
const parent = this.state.model.getNode(node.parent.value);
if (parent.checkState === 1) {
return true;
}
return this.isAnyParentChecked(parent);
}

isEveryChildChecked(node) {
return node.children.every(child => this.state.model.getNode(child.value).checkState === 1);
}
Expand All @@ -206,13 +224,19 @@ class CheckboxTree extends React.Component {
optimisticToggle,
showNodeTitle,
showNodeIcon,
useTopNode,
} = this.props;
const { id, model } = this.state;
const { icons: defaultIcons } = CheckboxTree.defaultProps;

const treeNodes = nodes.map((node) => {
const key = node.value;
const flatNode = model.getNode(node.value);

// Get checkState before traversing tree depth-first
if (useTopNode) {
flatNode.checkState = flatNode.checked ? 1 : 0;
}
const children = flatNode.isParent ? this.renderTreeNodes(node.children, node) : null;

// Determine the check state after all children check states have been determined
Expand Down Expand Up @@ -254,6 +278,7 @@ class CheckboxTree extends React.Component {
onCheck={this.onCheck}
onClick={onClick && this.onNodeClick}
onExpand={this.onExpand}
useTopNode={useTopNode}
>
{children}
</TreeNode>
Expand Down
7 changes: 5 additions & 2 deletions src/js/NodeModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class NodeModel {
return this;
}

toggleChecked(node, isChecked, noCascade) {
toggleChecked(node, isChecked, noCascade, useTopNode) {
const flatNode = this.flatNodes[node.value];

if (flatNode.isLeaf || noCascade) {
Expand All @@ -120,9 +120,12 @@ class NodeModel {
// Set the check status of a leaf node or an uncoupled parent
this.toggleNode(node.value, 'checked', isChecked);
} else {
if (useTopNode) {
this.toggleNode(node.value, 'checked', isChecked);
}
// Percolate check status down to all children
flatNode.children.forEach((child) => {
this.toggleChecked(child, isChecked, noCascade);
this.toggleChecked(child, useTopNode ? false : isChecked, noCascade, useTopNode);
});
}

Expand Down
16 changes: 13 additions & 3 deletions src/js/TreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TreeNode extends React.Component {
icon: PropTypes.node,
showCheckbox: PropTypes.bool,
title: PropTypes.string,
useTopNode: PropTypes.bool,
onClick: PropTypes.func,
};

Expand All @@ -44,7 +45,8 @@ class TreeNode extends React.Component {
icon: null,
showCheckbox: true,
title: null,
onClick: () => {},
useTopNode: false,
onClick: () => { },
};

constructor(props) {
Expand All @@ -56,8 +58,16 @@ class TreeNode extends React.Component {
}

onCheck() {
const { value, onCheck } = this.props;
const {
value,
onCheck,
checked,
useTopNode,
} = this.props;

if (checked === 2 && useTopNode) {
return;
}
onCheck({ value, checked: this.getCheckState({ toggle: true }) });
}

Expand Down Expand Up @@ -215,7 +225,7 @@ class TreeNode extends React.Component {
id={inputId}
indeterminate={checked === 2}
onClick={this.onCheck}
onChange={() => {}}
onChange={() => { }}
/>
<span className="rct-checkbox">
{this.renderCheckboxIcon()}
Expand Down