|
10 | 10 |  |
11 | 11 |
|
12 | 12 |
|
13 | | -# Usage |
| 13 | +## Installation |
14 | 14 |
|
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)): |
16 | 16 |
|
17 | | -``` shell |
| 17 | +``` |
18 | 18 | npm install react-checkbox-tree --save |
19 | 19 | ``` |
20 | 20 |
|
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. |
22 | 24 |
|
23 | 25 | ``` javascript |
24 | 26 | import CheckboxTree from 'react-checkbox-tree'; |
25 | 27 |
|
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 | + } |
42 | 75 | } |
43 | 76 | ``` |
| 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