Skip to content

Commit d109648

Browse files
committed
Make the usage section more detailed and adjust for stateless change
1 parent 395d31b commit d109648

File tree

1 file changed

+65
-20
lines changed

1 file changed

+65
-20
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. |

0 commit comments

Comments
 (0)