0.1.1 • Published 7 years ago

rt-treeview v0.1.1

Weekly downloads
6
License
MIT
Repository
github
Last release
7 years ago

rt-treeview

npm GitHub tag

React TreeView component based on a great react-toolbox UI framework.

Demo

HERE

Status

This package is currently under an active development and API might change in the future. The package is used in an active project so adding a new features and bug fixing is mainly driven by the input from the project.

ToDo

  • Configurable search behaviour
  • Custom styling
  • Improved animation

Overview

This package was developed as part of a couple of internal projects where react-toolbox is used as a main UI framework and we weren't able to find a suitable TreeView component for our needs.

Main focus is on

  • Performance - Reconsolidate and update only nodes which really need to be.
  • Maps - Taking an advantage of Map which defines tree structure.
  • Material UI - This package is proudly based and dependent on react-toolbox.
  • Interactive - Make the TreeView interactive and as flexible as possible.

Features & Limitations

  • Only one node can be selected at the time. As soon as a new node is selected the current selection is deselected.
  • TreeView is defined by nodes which must be given as input in form of Map which may not be supported by older browsers. See status here. You can use babel-polyfill or core-js with your application in order to support older browsers.
  • The package will only work with react-toolbox-beta8 and higher

Search

Searching functionality can be enabled via a search property. Searching is disabled by default due to its impact on the performance as the whole tree must be searched and modified.

At the moment all the siblings (collapsed) along with matched node's parent structure (expanded) are displayed. This is behaviour is expected to become configurable in the future.

Initial selection

Initial selection can be defined by providing a key value of initially selected node via selectedNode property. The parent structure of the initially selected node will be expanded.

Sizing

The TreeView supports four different sizing options provided via size option which accepts following values

  • xs
  • sm
  • md
  • lg

Installation

This package can be embedded into your application requiring following peerDependencies

  • React
  • ReactDOM
  • react-toolbox

You can get the component from NPM

npm install rt-treeview

UMD coming soon.

Usage

There are two components exposed by the package - TreeView and TreeViewDialog.

Similar to react-toolbox the components are written using React, PostCSS(http://postcss.org/) and CSSNext(http://cssnext.io/). That means in order to use the components you should use package bundler (webpack) for your application along with PostCSS - postcss-cssnext and postcss-nesting in particular.

TreeView

import {TreeView} from 'rt-treeview'

TreeView component can be embedded directly into your application. The tree is defined and constructed by nodes.

TreeView.PropTypes

nametypeisRequireddescription
nodesMapOf(Node)yesMap of nodes used to construct the tree.
selectedNodestringnoInitially selected node on a first render
searchboolnoDisplay search input
errorstringnoError message displayed above the tree
onNodeSelectfuncnoCallback called on a node selection with (nodeKey, node)
onNodeDeselectfuncnoCallback called when a node is de-selected with (nodeKey, node)
sizestringnoxs, sm, md, lg. Defaults to sm.
onlyLeafsSelectableboolnoMakes only leaf nodes selectable and doesn't trigger onNodeSelect for non-leaf nodes. Defaults to false.

Node.PropTypes

nametypeisRequireddescription
namestringyes
parentstringnoKey of parent node from the Map
childrenarraynoKeys of children nodes from the Map

Example

import React from 'react';
import {TreeView} from 'rt-treeview';

class TreeViewExample extends React.Component {
    constructor(props) {
        super(props);
        
        const nodeArray = [
            ['N1', {name: 'Node #1', children: ['N2', 'N3']}],
            ['N2', {name: 'Node #2', parent: 'N1'}],
            ['N3', {name: 'Node #3', parent: 'N1', children: ['N4']}],
            ['N4', {name: 'Node #4', parent: 'N3'}],
            ['N5', {name: 'Node #5', children: ['N6']}],
            ['N6', {name: 'Node #6', parent: 'N5'}]
        ];
        
        this.nodes = new Map(nodeArray);
        this.onNodeSelect = this.onNodeSelect.bind(this);
    }
    
    onNodeSelect(nodeKey, node) {
        // Do Stuff
    }
    
    onNodeDeselect(nodeKey, node) {
        // Do stuff
    }
    
    render() {
        return (
            <TreeView
                nodes={this.nodes} // Map of nodes
                search={true} // Search enabled/disabled
                onNodeSelect={this.onNodeSelect} // Event called on node selection
            />
        )
    }
}

TreeViewDialog

import {TreeViewDialog} from 'rt-treeview'

TreeViewDialog is embedded only as a read-only Input which triggers a dialog with the TreeView component. This is useful when you don't want to embed the TreeView directly into the main view and only display selected value as a regular input value.

Please note that setting the value of Input is not handled by the rt-treeview and must be taken care of by user ie. as shown in the example below.

TreeViewDialog.PropTypes

nametypeisRequireddescription
inputLabelstringnoLabel for the Input component
inputValuestringnoValue for the Input component
errorstringnoError for the Input component
dialogTitlestringnoDialog title
childrenelementyesTreeView

Example

import React from 'react';
import {TreeView, TreeViewDialog} from 'rt-treeview';

class TreeViewDialogExample extends React.Component {
    constructor(props) {
        super(props);
        
        const nodeArray = [
            ['N1', {name: 'Node #1', children: ['N2', 'N3']}],
            ['N2', {name: 'Node #2', parent: 'N1'}],
            ['N3', {name: 'Node #3', parent: 'N1', children: ['N4']}],
            ['N4', {name: 'Node #4', parent: 'N3'}],
            ['N5', {name: 'Node #5', children: ['N6']}],
            ['N6', {name: 'Node #6', parent: 'N5'}]
        ];
        
        this.state = {
            selectedKey : 'N1'
        };
        
        this.nodes = new Map(nodeArray);
        this.onNodeSelect = this.onNodeSelect.bind(this);
    }
    
    onNodeSelect(nodeKey, node) {
        this.setState({
            selectedKey: nodeKey
        });
    }
    
    render() {
        const node = this.nodes.get(this.state.selectedKey);
        return (
            <TreeViewDialog
                inputValue={node ? node.name : "Unknown"}
                inputLabel="Selected Node"
                dialogTitle="Select random node"
            >
                <TreeView
                    nodes={this.nodes}
                    search={true} 
                    onNodeSelect={this.onNodeSelect}
                />
            </TreeViewDialog>
        )
    }
}

Examples

git clone https://github.com/razakj/rt-treeview.git
cd rt-treeview
npm install
npm run examples

You should be able to access the examples via web browser at http://localhost:9876. Please note that currently there is a bug with styles not loading properly in the provided examples.

The examples can be found under ./examples/index.js

LICENSE

MIT

0.1.1

7 years ago

0.1.0

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago