# react-treebeard

> React Tree View Component

Latest version **3.2.4** (published 2019-06-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-treebeard
pnpm add react-treebeard
yarn add react-treebeard
bun add react-treebeard
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.4 |
| Published | 2019-06-20 |
| First published | 2015-11-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 45.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1680 |
| Author | Alex Curtis |
| Maintainers | alexcurtis, michaeldeboey, ndelangen, pksunkara, shilman |
| Keywords | react, treeview, data-driven, customisable, fast |

## Links

- npm: https://www.npmjs.com/package/react-treebeard
- Repository: https://github.com/storybooks/react-treebeard
- Homepage: https://github.com/storybooks/react-treebeard#readme
- Issues: https://github.com/storybooks/react-treebeard/issues
- npm.io page: https://npm.io/package/react-treebeard

## Dependencies (5)

- [deep-equal](https://npm.io/package/deep-equal.md) ^1.0.1
- [shallowequal](https://npm.io/package/shallowequal.md) ^1.1.0
- [@emotion/core](https://npm.io/package/@emotion/core.md) ^10.0.10
- [velocity-react](https://npm.io/package/velocity-react.md) ^1.4.1
- [@emotion/styled](https://npm.io/package/@emotion/styled.md) ^10.0.10

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 3.2.4 (latest) — 2019-06-20
- 4.2.4-beta.0 (beta) — 2019-09-16
- 3.2.3 — 2019-05-01
- 3.2.2 — 2019-04-29
- 3.2.1 — 2019-04-29
- 3.2.0 — 2019-04-11
- 3.1.0 — 2018-09-25
- 2.1.0 — 2017-11-01
- 2.0.3 — 2017-07-18
- 2.0.1 — 2017-07-17
- 2.0.0 — 2017-07-17
- 2.0.2 — 2017-07-17
- 1.1.4 — 2016-09-12
- 1.1.3 — 2016-09-07
- 1.1.2 — 2016-05-19
- … 16 more at https://npm.io/package/react-treebeard/versions

## README

# react-treebeard

[![Build Status](https://travis-ci.org/storybookjs/react-treebeard.svg?branch=master)](https://travis-ci.org/storybookjs/react-treebeard) [![Coverage Status](https://coveralls.io/repos/storybookjs/react-treebeard/badge.svg?branch=master&service=github)](https://coveralls.io/github/storybookjs/react-treebeard?branch=master)

React Tree View Component. Data-Driven, Fast, Efficient and Customisable.

### Install

```
npm install react-treebeard --save
```

### [Example](http://storybookjs.github.io/react-treebeard/)

An online example from the `/example` directory can be found here: [Here](http://storybookjs.github.io/react-treebeard/)

### Quick Start
```javascript
import React, {PureComponent} from 'react';
import ReactDOM from 'react-dom';
import {Treebeard} from 'react-treebeard';

const data = {
    name: 'root',
    toggled: true,
    children: [
        {
            name: 'parent',
            children: [
                { name: 'child1' },
                { name: 'child2' }
            ]
        },
        {
            name: 'loading parent',
            loading: true,
            children: []
        },
        {
            name: 'parent',
            children: [
                {
                    name: 'nested parent',
                    children: [
                        { name: 'nested child 1' },
                        { name: 'nested child 2' }
                    ]
                }
            ]
        }
    ]
};

class TreeExample extends PureComponent {
    constructor(props){
        super(props);
        this.state = {data};
    }
    
    onToggle(node, toggled){
        const {cursor, data} = this.state;
        if (cursor) {
            this.setState(() => ({cursor, active: false}));
        }
        node.active = true;
        if (node.children) { 
            node.toggled = toggled; 
        }
        this.setState(() => ({cursor: node, data: Object.assign({}, data)}));
    }
    
    render(){
        const {data} = this.state;
        return (
            <Treebeard
                data={data}
                onToggle={this.onToggle}
            />
        );
    }
}

const content = document.getElementById('content');
ReactDOM.render(<TreeExample/>, content);
```

If you use react-hooks you should do something like this:
```javascript
import React, {useState} from 'react';
const TreeExample = () => {
    const [data, setData] = useState(data);
    const [cursor, setCursor] = useState(false);
    
    const onToggle = (node, toggled) => {
        if (cursor) {
            cursor.active = false;
        }
        node.active = true;
        if (node.children) {
            node.toggled = toggled;
        }
        setCursor(node);
        setData(Object.assign({}, data))
    }
    
    return (
       <Treebeard data={data} onToggle={onToggle}/>
    )
}

const content = document.getElementById('content');
ReactDOM.render(<TreeExample/>, content);
```

### Prop Values

#### data
`PropTypes.oneOfType([PropTypes.object,PropTypes.array]).isRequired`

Data that drives the tree view. State-driven effects can be built by manipulating the attributes in this object. Also supports an array for multiple nodes at the root level. An example can be found in `example/data.js`

#### onToggle
`PropTypes.func`

Callback function when a node is toggled / clicked. Passes 2 attributes: the data node and it's toggled boolean state.

#### style
`PropTypes.object`

Sets the treeview styling. Defaults to `src/themes/default`.

#### animations
`PropTypes.oneOfType([PropTypes.object, PropTypes.bool])`

Sets the treeview animations. Set to `false` if you want to turn off animations. See [velocity-react](https://github.com/twitter-fabric/velocity-react) for more details. Defaults to `src/themes/animations`.

#### decorators
`PropTypes.object`

Decorates the treeview. Here you can use your own Container, Header, Toggle and Loading components. Defaults to `src/decorators`. See example below:

```javascript
const decorators = {
    Loading: (props) => {
        return (
            <div style={props.style}>
                loading...
            </div>
        );
    },
    Toggle: (props) => {
        return (
            <div style={props.style}>
                <svg height={props.height} width={props.width}>
                    // Vector Toggle Here
                </svg>
            </div>
        );
    },
    Header: (props) => {
        return (
            <div style={props.style}>
                {props.node.name}
            </div>
        );
    },
    Container: (props) => {
        return (
            <div onClick={this.props.onClick}>
                // Hide Toggle When Terminal Here
                <this.props.decorators.Toggle/>
                <this.props.decorators.Header/>
            </div>
        );
    }
};

<Treebeard data={...} decorators={decorators}/>
```

### Data Attributes

```javascript
{
    id: '[optional] string',
    name: 'string',
    children: '[optional] array',
    toggled: '[optional] boolean',
    active: '[optional] boolean',
    loading: '[optional] boolean',
    decorators: '[optional] object',
    animations: '[optional] object'
},
```
#### id
The component key. If not defined, an auto-generated index is used.

#### name
The name prop passed into the Header component.

#### children
The children attached to the node. This value populates the subtree at the specific node. Each child is built from the same basic data structure. Tip: Make this an empty array, if you want to asynchronously load a potential parent.

#### toggled
Toggled flag. Sets the visibility of a node's children. It also sets the state for the toggle decorator.

#### active
Active flag. If active, the node will be highlighted. The highlight is derived from the `node.activeLink` style object in the theme.

#### loading
Loading flag. It will populate the treeview with the loading component. Useful when asynchronously pulling the data into the treeview.

#### decorators / animations
Attach specific decorators / animations to a node. Provides the low level functionality to create visuals on a node-by-node basis. These structures are the same as the top level props, described above.

---
_Source: https://npm.io/package/react-treebeard · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
