69 | );
70 | }
71 | }
72 |
73 | export default TreeView;
74 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # React-treeview [](https://www.npmjs.com/package/react-treeview) [](http://badge.fury.io/bo/react-treeview)
2 |
3 | Easy, light, flexible treeview made with [React](http://facebook.github.io/react/).
4 |
5 | [Demos](https://cdn.rawgit.com/chenglou/react-treeview/aa72ed8b9e0b31fabc09e2f8bd4084947d48bb09/demos/index.html) from the [demos folder](https://github.com/chenglou/react-treeview/tree/aa72ed8b9e0b31fabc09e2f8bd4084947d48bb09/demos).
6 |
7 | ## install
8 |
9 | Npm:
10 | ```sh
11 | npm install react-treeview
12 | ```
13 |
14 | Bower:
15 | ```sh
16 | bower install react-treeview
17 | ```
18 |
19 | The CSS file:
20 |
21 | ```html
22 |
23 | ```
24 |
25 | ## API
26 |
27 | #### <TreeView />
28 | The component accepts [these props](https://github.com/chenglou/react-treeview/blob/aa72ed8b9e0b31fabc09e2f8bd4084947d48bb09/src/react-treeview.jsx#L5-L9).
29 |
30 | - `collapsed`: whether the node is collapsed or not.
31 | - `defaultCollapsed`: the [uncontrolled](http://facebook.github.io/react/docs/forms.html#uncontrolled-components) equivalent to `collapsed`.
32 | - `nodeLabel`: the component or string (or anything renderable) that's displayed beside the TreeView arrow.
33 | - `itemClassName`: the class name of the `.tree-view_item` div.
34 | - `treeViewClassName`: the class name of the `.tree-view` div.
35 | - `childrenClassName`: the class name of the `.tree-view_children` item div.
36 |
37 | TreeViews can be naturally nested.
38 |
39 | The extra properties transferred onto the arrow, so all attributes and events naturally work on it.
40 |
41 | ## Styling
42 | The CSS is flexible, commented and made to be easily customized. Feel free to inspect the demo's classes and check the [short CSS code](https://github.com/chenglou/react-treeview/blob/aa72ed8b9e0b31fabc09e2f8bd4084947d48bb09/react-treeview.css).
43 |
44 | ## Build It Yourself/Run the Demos
45 |
46 | Build: `npm install && npm run prerelease`
47 |
48 | Demos: `npm install && npm start && open http://localhost:3000`
49 |
50 | ## License
51 |
52 | MIT.
53 |
--------------------------------------------------------------------------------
/demos/controlled.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import TreeView from '../src/react-treeview';
3 |
4 | // This example data format is totally arbitrary. No data massaging is
5 | // required and you use regular js in `render` to iterate through and
6 | // construct your nodes.
7 | const dataSource = [
8 | ['Apple', 'Orange'],
9 | ['Facebook', 'Google'],
10 | ['Celery', 'Cheeseburger'],
11 | ];
12 |
13 | // A controlled TreeView, akin to React's controlled inputs
14 | // (http://facebook.github.io/react/docs/forms.html#controlled-components), has
15 | // many benefits. Among others, you can expand/collapse everything (i.e. easily
16 | // trigger those somewhere else).
17 | class Lists extends React.Component {
18 |
19 | constructor(props) {
20 | super(props);
21 |
22 | this.state = {
23 | collapsedBookkeeping: dataSource.map(() => false)
24 | };
25 | this.handleClick = this.handleClick.bind(this);
26 | this.collapseAll = this.collapseAll.bind(this);
27 | }
28 |
29 | handleClick(i) {
30 | let [...collapsedBookkeeping] = this.state.collapsedBookkeeping;
31 | collapsedBookkeeping[i] = !collapsedBookkeeping[i];
32 | this.setState({collapsedBookkeeping: collapsedBookkeeping});
33 | }
34 |
35 | collapseAll() {
36 | this.setState({
37 | collapsedBookkeeping: this.state.collapsedBookkeeping.map(() => true),
38 | });
39 | }
40 |
41 | render() {
42 | const collapsedBookkeeping = this.state.collapsedBookkeeping;
43 | return (
44 |
45 |
46 | {dataSource.map((node, i) => {
47 | // Let's make it so that the tree also toggles when we click the
48 | // label. Controlled components make this effortless.
49 | const label =
50 |
51 | Type {i}
52 | ;
53 | return (
54 |
59 | {node.map(entry =>