Skip to content

Commit fd06dce

Browse files
author
Guy Elsmore-Paddock
committed
[jakezatecky#171] Allow Parent Nodes to Have an Empty children Array
In computer science terms, a node without any children is a "leaf". But in practical use, it's common to run into structures (e.g. empty folders in file systems) where a node is still a parent because it does not _yet_ have children but has the capacity to have children in the future. Closes jakezatecky#171.
1 parent 67eb973 commit fd06dce

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/js/NodeModel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class NodeModel {
5252
}
5353

5454
nodeHasChildren(node) {
55-
return Array.isArray(node.children) && node.children.length > 0;
55+
return Array.isArray(node.children);
5656
}
5757

5858
getDisabledState(node, parent, disabledProp, noCascade) {

test/CheckboxTree.js

+50
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,56 @@ describe('<CheckboxTree />', () => {
181181
{ children: [null, null] },
182182
);
183183
});
184+
185+
it('should render a node with no "children" array as a leaf', () => {
186+
const wrapper = shallow(
187+
<CheckboxTree
188+
nodes={[
189+
{ value: 'jupiter', label: 'Jupiter' },
190+
]}
191+
/>,
192+
);
193+
194+
assert.equal(false, wrapper.find(TreeNode).prop('isParent'));
195+
assert.equal(true, wrapper.find(TreeNode).prop('isLeaf'));
196+
});
197+
198+
it('should render a node with an empty "children" array as a parent', () => {
199+
const wrapper = shallow(
200+
<CheckboxTree
201+
nodes={[
202+
{
203+
value: 'jupiter',
204+
label: 'Jupiter',
205+
children: [],
206+
},
207+
]}
208+
/>,
209+
);
210+
211+
assert.equal(true, wrapper.find(TreeNode).prop('isParent'));
212+
assert.equal(false, wrapper.find(TreeNode).prop('isLeaf'));
213+
});
214+
215+
it('should render a node with a non-empty "children" array as a parent', () => {
216+
const wrapper = shallow(
217+
<CheckboxTree
218+
nodes={[
219+
{
220+
value: 'jupiter',
221+
label: 'Jupiter',
222+
children: [
223+
{ value: 'io', label: 'Io' },
224+
{ value: 'europa', label: 'Europa' },
225+
],
226+
},
227+
]}
228+
/>,
229+
);
230+
231+
assert.equal(true, wrapper.find(TreeNode).prop('isParent'));
232+
assert.equal(false, wrapper.find(TreeNode).prop('isLeaf'));
233+
});
184234
});
185235

186236
describe('noCascade', () => {

0 commit comments

Comments
 (0)