• This repository has been archived on 16/Jan/2023
  • Stars
    star
    1,688
  • Rank 27,647 (Top 0.6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

react-treebeard

Build Status Coverage Status

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

Install

npm install react-treebeard --save

Example

An online example from the /example directory can be found here: Here

Quick Start

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};
        this.onToggle = this.onToggle.bind(this);
    }
    
    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:

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 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:

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

{
    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.

More Repositories

1

storybook-deployer

Deploy your storybook as a static site
JavaScript
894
star
2

react-cdk

under development - React Component Development Kit with Storybook
JavaScript
576
star
3

react-native-storybook

REPO/PACKAGE MOVED - UI Component Dev Environment for React Native
JavaScript
486
star
4

storyshots

REPO/PACKAGE MOVED - Jest Snapshot testing for React Storybook
JavaScript
351
star
5

addon-smart-knobs

🧠 This Storybook plugin uses @storybook/addon-knobs but creates the knobs automatically based on PropTypes.
JavaScript
217
star
6

storybook-addon-knobs

REPO/PACKAGE MOVED - Edit React props dynamically using the Storybook UI
JavaScript
189
star
7

storybook-addon-jest

REPO/PACKAGE MOVED - React storybook addon that show component jest report
JavaScript
172
star
8

react-storybook-addon-info

REPO/PACKAGE MOVED - A storybook addon to show additional information for your stories.
JavaScript
154
star
9

getstorybook

REPO/PACKAGE MOVED - Easiest way to add Storybook support to your project
JavaScript
95
star
10

addon-backgrounds

REPO/PACKAGE MOVED - A Storybook addon to customize the background of your preview
JavaScript
74
star
11

storybook-ui

REPO/PACKAGE MOVED - Core Storybook UI
JavaScript
52
star
12

storybook-addon-a11y

REPO/PACKAGE MOVED - Storybook addon to help, improving accessibility within you're react components.
JavaScript
37
star
13

storybook-addon-notes

DEPRECATED - Write notes for your Storybook stories
JavaScript
33
star
14

storybook-addon-options

REPO/PACKAGE MOVED - Storybook UI Options Addon
JavaScript
30
star
15

storybook-addon-graphql

REPO/PACKAGE MOVED - Storybook addon to display the GraphiQL IDE
JavaScript
20
star
16

storybook-addon-actions

REPO/PACKAGE MOVED - Action logger addon for storybook
JavaScript
19
star
17

storybook-addon-links

REPO/PACKAGE MOVED - Story links addon for storybook
JavaScript
11
star
18

storybooks.github.io

REPO MOVED: Storybook documentation site
JavaScript
10
star
19

generate-page-webpack-plugin

generate a html-page for every webpack entrypoint
JavaScript
7
star
20

getstorybook.io

REPO/DOCS MOVED - getstorybook.io website - developer's home for storybook
JavaScript
7
star
21

react-storybook-decorator-centered

REPO/PACKAGE MOVED - Storybook decorator to render the component at screen center
JavaScript
6
star
22

storybook-addon-comments

REPO/PACKAGE MOVED - Comments Addon
JavaScript
5
star
23

riot

Storybook framework support for riot
TypeScript
4
star
24

mithril

Storybook framework support for mithril
JavaScript
3
star
25

storybook-addons

REPO/PACKAGE MOVED - Storybook addons store
JavaScript
3
star
26

storybook-package

https://www.npmjs.com/package/storybook
JavaScript
3
star
27

deprecated-addons

JavaScript
2
star
28

aurelia

Storybook framework support for aurelia
TypeScript
2
star
29

rax

Storybook framework support for rax
JavaScript
1
star
30

marionette

Storybook framework support for marionette
JavaScript
1
star
31

storybook-channel-firebase

DEPRECATED - storybook firebase channel
JavaScript
1
star
32

storybook-database-local

DEPRECATED - storybook database on local machine
JavaScript
1
star
33

storybook-channel-websocket

REPO/PACKAGE MOVED - Websocket Channel
JavaScript
1
star
34

storybook-addon-devel

DEPRECATED - storybook addon developer tool
JavaScript
1
star
35

actor-js-lerna

A https://www.dependencies.io actor for updating JS monorepo dependencies using Lerna.
JavaScript
1
star