Skip to content

Commit 91d373c

Browse files
committed
Merge branch 'feature-stateless' of github.com:jakezatecky/react-checkbox-tree into feature-stateless
# Conflicts: # src/js/Tree.js
2 parents b6715c0 + 5cd9a42 commit 91d373c

File tree

4 files changed

+76
-37
lines changed

4 files changed

+76
-37
lines changed

README.md

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,79 @@
1010
![Demo](demo.gif)
1111

1212

13-
# Usage
13+
## Installation
1414

15-
Install the library:
15+
The easiest way to use react-checkbox-tree is to install from NPM and include it in your own React build process (e.g. using [Webpack](http://webpack.github.io/docs/what-is-webpack.html)):
1616

17-
``` shell
17+
```
1818
npm install react-checkbox-tree --save
1919
```
2020

21-
Then render the component:
21+
## Usage
22+
23+
A quick usage example is included below. Note that the react-checkbox-tree component is [controlled](https://facebook.github.io/react/docs/forms.html#controlled-components). In other words, it is stateless. You must update its `checked` and `expanded` properties whenever a change occurs.
2224

2325
``` javascript
2426
import CheckboxTree from 'react-checkbox-tree';
2527

26-
...
27-
28-
render() {
29-
const nodes = [{
30-
value: 'node-1',
31-
title: 'Parent Node 1',
32-
children: [{
33-
value: 'node-1-1',
34-
title: 'Leaf Node 1-1',
35-
}, {
36-
value: 'node-1-2',
37-
title: 'Leaf Node 1-2'
38-
}],
39-
}];
40-
41-
return <CheckboxTree nodes={nodes} />;
28+
const nodes = [{
29+
value: 'node-1',
30+
title: 'Parent Node 1',
31+
children: [{
32+
value: 'node-1-1',
33+
title: 'Leaf Node 1-1',
34+
}, {
35+
value: 'node-1-2',
36+
title: 'Leaf Node 1-2'
37+
}],
38+
}];
39+
40+
class Widget extends React.Component {
41+
constructor() {
42+
super();
43+
44+
this.state = {
45+
checked: [],
46+
expanded: [],
47+
};
48+
49+
this.onCheck = this.onCheck.bind(this);
50+
this.onExpand = this.onExpand.bind(this);
51+
}
52+
53+
onCheck(checked) {
54+
this.setState({ checked });
55+
}
56+
57+
onExpand(expanded) {
58+
this.setState({ expanded });
59+
}
60+
61+
render() {
62+
const { checked, expanded } = this.state;
63+
64+
return (
65+
<Tree
66+
name="tree_nodes"
67+
nodes={nodes}
68+
checked={checked}
69+
expanded={expanded}
70+
onCheck={this.onCheck}
71+
onExpand={this.onExpand}
72+
/>
73+
);
74+
}
4275
}
4376
```
77+
78+
### Properties
79+
80+
| Property | Type | Description |
81+
| ------------- | -------- | --------------------------------------------------------------------------------------------- |
82+
| `nodes` | array | **Required**. Specifies the tree nodes and their children. |
83+
| `checked` | array | **Required**. An array of checked node values. |
84+
| `expanded` | array | **Required**. An array of expanded node values. |
85+
| `onCheck` | function | onCheck handler: `function(checked) {}` |
86+
| `onExpand` | function | onExpand handler: `function(expanded) {}` |
87+
| `name` | string | Optional name for the hidden `<input>` element. |
88+
| `nameAsArray` | bool | If true, the hidden `<input>` will encode its values as an array rather than a joined string. |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-checkbox-tree",
3-
"version": "0.2.0",
3+
"version": "0.3.0-rc.1",
44
"description": "React component for checkbox trees.",
55
"author": "Jake Zatecky",
66
"license": "MIT",

src/js/Tree.js

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

75-
if (this.hasChildren(node)) {
75+
if (Array.isArray(node.children) && node.children.length > 0) {
7676
formatted.children = this.getFormattedNodes(formatted.children);
77+
} else {
78+
formatted.children = null;
7779
}
7880

7981
return formatted;
8082
});
8183
}
8284

8385
getCheckState(node) {
84-
if (this.hasChildren(node) === false) {
86+
if (node.children === null) {
8587
return node.checked ? 1 : 0;
8688
}
8789

@@ -97,7 +99,7 @@ class Tree extends React.Component {
9799
}
98100

99101
setCheckState(checked, node, isChecked) {
100-
if (this.hasChildren(node)) {
102+
if (node.children !== null) {
101103
// Percolate check status down to all children
102104
node.children.forEach((child) => {
103105
this.setCheckState(checked, child, isChecked);
@@ -118,7 +120,7 @@ class Tree extends React.Component {
118120

119121
isEveryChildChecked(node) {
120122
return node.children.every((child) => {
121-
if (this.hasChildren(child)) {
123+
if (child.children !== null) {
122124
return this.isEveryChildChecked(child);
123125
}
124126

@@ -128,22 +130,14 @@ class Tree extends React.Component {
128130

129131
isSomeChildChecked(node) {
130132
return node.children.some((child) => {
131-
if (this.hasChildren(child)) {
133+
if (child.children !== null) {
132134
return this.isSomeChildChecked(child);
133135
}
134136

135137
return child.checked;
136138
});
137139
}
138140

139-
hasChildren(node) {
140-
if (typeof node.children !== 'object') {
141-
return false;
142-
}
143-
144-
return node.children.length > 0;
145-
}
146-
147141
renderTreeNodes(nodes) {
148142
const treeNodes = nodes.map((node, index) => {
149143
const key = `${index}-${node.value}`;
@@ -174,7 +168,7 @@ class Tree extends React.Component {
174168
}
175169

176170
renderChildNodes(node) {
177-
if (this.hasChildren(node)) {
171+
if (node.children !== null && node.expanded) {
178172
return this.renderTreeNodes(node.children);
179173
}
180174

src/js/TreeNode.js

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

5252
renderCollapseIcon() {
53-
if (this.props.children === null) {
53+
if (this.props.rawChildren === null) {
5454
return <i className="fa" />;
5555
}
5656

@@ -74,7 +74,7 @@ class TreeNode extends React.Component {
7474
}
7575

7676
renderNodeIcon() {
77-
if (this.props.children !== null) {
77+
if (this.props.rawChildren !== null) {
7878
if (!this.props.expanded) {
7979
return <i className="fa fa-folder-o" />;
8080
}

0 commit comments

Comments
 (0)