|
| 1 | +# react-tree |
| 2 | + |
| 3 | +[](https://travis-ci.org/hckhanh/react-tree) |
| 4 | + |
| 5 | +The wrapper of jsTree (jstree.com) for React |
| 6 | + |
| 7 | +## Getting Started |
| 8 | + |
| 9 | +If you want to find a **tree view** component for React, this module is what you need. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +npm install --save react-tree |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +import/require `ReactTree` to your React source code: |
| 20 | + |
| 21 | +```js |
| 22 | +import ReactTree from 'react-tree'; |
| 23 | +``` |
| 24 | + |
| 25 | +### tree |
| 26 | + |
| 27 | +**tree** is the node object or and array of node object. This is an example of data of a node: |
| 28 | + |
| 29 | +```js |
| 30 | +{ |
| 31 | + text: 'Root node 2', |
| 32 | + state: { |
| 33 | + opened: true, |
| 34 | + selected: true |
| 35 | + }, |
| 36 | + children: [ |
| 37 | + { |
| 38 | + text: 'Child 1' |
| 39 | + }, |
| 40 | + 'Child 2' |
| 41 | + ] |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +Here is the full structure of a node: |
| 46 | + |
| 47 | +```js |
| 48 | +// Alternative format of the node (id & parent are required) |
| 49 | +{ |
| 50 | + id: 'string' // required |
| 51 | + parent: 'string' // required |
| 52 | + text: 'string' // node text |
| 53 | + icon: 'string' // string for custom |
| 54 | + state: { |
| 55 | + opened: boolean // is the node open |
| 56 | + disabled: boolean // is the node disabled |
| 57 | + selected: boolean // is the node selected |
| 58 | + }, |
| 59 | + li_attr: { } // attributes for the generated LI node |
| 60 | + a_attr: { } // attributes for the generated A node |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +You can define a tree and then parse it to `tree` property: |
| 65 | + |
| 66 | +```js |
| 67 | +const TREE = [ |
| 68 | + 'Simple root node', |
| 69 | + { |
| 70 | + text: 'Root node 2', |
| 71 | + state: { |
| 72 | + opened: true, |
| 73 | + selected: true |
| 74 | + }, |
| 75 | + children: [ |
| 76 | + { |
| 77 | + text: 'Child 1' |
| 78 | + }, |
| 79 | + 'Child 2' |
| 80 | + ] |
| 81 | + } |
| 82 | +]; |
| 83 | + |
| 84 | +class ExampleApp extends React.Component { |
| 85 | + render() { |
| 86 | + return (<ReactTree tree={TREE} />); |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### onChanged |
| 92 | + |
| 93 | +This is an event to handle when a node is clicked: |
| 94 | + |
| 95 | +```js |
| 96 | +const TREE = [ |
| 97 | + 'Simple root node', |
| 98 | + { |
| 99 | + text: 'Root node 2', |
| 100 | + state: { |
| 101 | + opened: true, |
| 102 | + selected: true |
| 103 | + }, |
| 104 | + children: [ |
| 105 | + { |
| 106 | + text: 'Child 1' |
| 107 | + }, |
| 108 | + 'Child 2' |
| 109 | + ] |
| 110 | + } |
| 111 | +]; |
| 112 | + |
| 113 | +class ExampleApp extends React.Component { |
| 114 | + constructor(props) { |
| 115 | + super(props); |
| 116 | + |
| 117 | + this.state = { items: [] }; |
| 118 | + |
| 119 | + this.handleOnChanged = this.handleOnChanged.bind(this); |
| 120 | + } |
| 121 | + |
| 122 | + handleOnChanged(changedItems) { |
| 123 | + this.setState({ |
| 124 | + items: changedItems.map(item => item.text).join(', ') |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + render() { |
| 129 | + return ( |
| 130 | + <div> |
| 131 | + <ReactTree tree={TREE} onChanged={this.handleOnChanged} /> |
| 132 | + <div>Selected items: {this.state.items}</div> |
| 133 | + </div> |
| 134 | + ); |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +If you need detailed example, go to [example](example) folder. |
| 140 | + |
| 141 | +### Themes |
| 142 | + |
| 143 | +If user want to apply css for **ReactTree**, consider to include these files: |
| 144 | + |
| 145 | +* node_modules/jstree/dist/themes/default/style.min.css |
| 146 | +* node_modules/jstree/dist/themes/default-dark/style.min.css |
0 commit comments