# rt-treeview

> TreeView component based on react-toolbox UI framework

Latest version **0.1.1** (published 2017-08-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install rt-treeview
pnpm add rt-treeview
yarn add rt-treeview
bun add rt-treeview
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2017-08-02 |
| First published | 2017-06-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Jakub Razak |
| Maintainers | razakj |
| Keywords | treeview, material, react-toolbox, ui, nodes |

## Links

- npm: https://www.npmjs.com/package/rt-treeview
- Repository: https://github.com/razakj/rt-treeview
- Homepage: https://github.com/razakj/rt-treeview#readme
- Issues: https://github.com/razakj/rt-treeview/issues
- npm.io page: https://npm.io/package/rt-treeview

## Dependencies (2)

- [classnames](https://npm.io/package/classnames.md) ^2.2.5
- [prop-types](https://npm.io/package/prop-types.md) ^15.5.10

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.1.1 (latest) — 2017-08-02
- 0.1.0 — 2017-08-02
- 0.0.14 — 2017-06-16
- 0.0.13 — 2017-06-12
- 0.0.12 — 2017-06-10
- 0.0.11 — 2017-06-10
- 0.0.10 — 2017-06-10
- 0.0.9 — 2017-06-10
- 0.0.8 — 2017-06-09
- 0.0.7 — 2017-06-09
- 0.0.6 — 2017-06-09
- 0.0.5 — 2017-06-09

## README

# rt-treeview

[![npm](https://img.shields.io/npm/v/npm.svg?style=flat-square)](https://www.npmjs.com/package/rt-treeview)
[![GitHub tag](https://img.shields.io/github/tag/razakj/rt-treeview.svg?style=flat-square)]()

React TreeView component based on a great [react-toolbox](https://github.com/react-toolbox/react-toolbox)
UI framework.

## Demo

[HERE](https://razakj.github.io/rt-treeview/)

## 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](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map) which defines tree structure.
* **Material UI** - This package is proudly based and dependent on [react-toolbox](https://github.com/react-toolbox/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](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map).
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](https://webpack.js.org/)) for your application
along with PostCSS - `postcss-cssnext` and `postcss-nesting` in particular.

### TreeView
```javascript
import {TreeView} from 'rt-treeview'
```
TreeView component can be embedded directly into your application. The tree is defined and constructed by ```nodes```.

#### TreeView.PropTypes
| name          | type          | isRequired  | description |
|---------------|:-------------:|:-----------:|:------------|
| nodes         | MapOf(Node)   | yes         | Map of nodes used to construct the tree.
| selectedNode  | string        | no          | Initially selected node on a first render
| search        | bool          | no          | Display search input
| error         | string        | no          | Error message displayed above the tree
| onNodeSelect  | func          | no          | Callback called on a node selection with *(nodeKey, node)*
| onNodeDeselect| func          | no          | Callback called when a node is de-selected with *(nodeKey, node)*
| size          | string        | no          | xs, sm, md, lg. Defaults to sm.
| onlyLeafsSelectable | bool    | no          | Makes only leaf nodes selectable and doesn't trigger onNodeSelect for non-leaf nodes. Defaults to false.
#### Node.PropTypes
| name          | type          | isRequired  | description |
|---------------|:-------------:|:-----------:|:------------|
| name          | string        | yes         | 
| parent        | string        | no          | Key of parent node from the Map
| children      | array         | no          | Keys of children nodes from the Map

#### Example
```javascript
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
```javascript
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
| name          | type          | isRequired  | description |
|---------------|:-------------:|:-----------:|:------------|
| inputLabel    | string        | no          | Label for the Input component
| inputValue    | string        | no          | Value for the Input component
| error         | string        | no          | Error for the Input component
| dialogTitle   | string        | no          | Dialog title
| children      | element       | yes         | [TreeView](#treeview)

#### Example
```javascript
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]()

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