Skip to content

Commit fa858a0

Browse files
committed
Standardize empty children to null
1 parent dacd085 commit fa858a0

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

src/js/Tree.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,18 @@ class Tree extends React.Component {
5454
formatted.checked = checked.indexOf(node.value) > -1;
5555
formatted.expanded = expanded.indexOf(node.value) > -1;
5656

57-
if (this.hasChildren(node)) {
57+
if (Array.isArray(node.children) && node.children.length > 0) {
5858
formatted.children = this.getFormattedNodes(formatted.children);
59+
} else {
60+
formatted.children = null;
5961
}
6062

6163
return formatted;
6264
});
6365
}
6466

6567
getCheckState(node) {
66-
if (this.hasChildren(node) === false) {
68+
if (node.children === null) {
6769
return node.checked ? 1 : 0;
6870
}
6971

@@ -79,7 +81,7 @@ class Tree extends React.Component {
7981
}
8082

8183
setCheckState(checked, node, isChecked) {
82-
if (this.hasChildren(node)) {
84+
if (node.children !== null) {
8385
// Percolate check status down to all children
8486
node.children.forEach((child) => {
8587
this.setCheckState(checked, child, isChecked);
@@ -100,7 +102,7 @@ class Tree extends React.Component {
100102

101103
isEveryChildChecked(node) {
102104
return node.children.every((child) => {
103-
if (this.hasChildren(child)) {
105+
if (child.children !== null) {
104106
return this.isEveryChildChecked(child);
105107
}
106108

@@ -110,22 +112,14 @@ class Tree extends React.Component {
110112

111113
isSomeChildChecked(node) {
112114
return node.children.some((child) => {
113-
if (this.hasChildren(child)) {
115+
if (child.children !== null) {
114116
return this.isSomeChildChecked(child);
115117
}
116118

117119
return child.checked;
118120
});
119121
}
120122

121-
hasChildren(node) {
122-
if (node.children === undefined) {
123-
return false;
124-
}
125-
126-
return node.children.length > 0;
127-
}
128-
129123
renderTreeNodes(nodes) {
130124
const treeNodes = nodes.map((node, index) => {
131125
const checked = this.getCheckState(node);
@@ -155,7 +149,7 @@ class Tree extends React.Component {
155149
}
156150

157151
renderChildNodes(node) {
158-
if (this.hasChildren(node) && node.expanded) {
152+
if (node.children !== null && node.expanded) {
159153
return this.renderTreeNodes(node.children);
160154
}
161155

src/js/TreeNode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class TreeNode extends React.Component {
4242
}
4343

4444
renderCollapseIcon() {
45-
if (this.props.rawChildren === undefined) {
45+
if (this.props.rawChildren === null) {
4646
return <i className="fa" />;
4747
}
4848

@@ -66,7 +66,7 @@ class TreeNode extends React.Component {
6666
}
6767

6868
renderNodeIcon() {
69-
if (this.props.rawChildren !== undefined) {
69+
if (this.props.rawChildren !== null) {
7070
if (!this.props.expanded) {
7171
return <i className="fa fa-folder-o" />;
7272
}

0 commit comments

Comments
 (0)