1.0.0 ā€¢ Published 2 years ago

react-tree-vis v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Allows you to store and manage information in different tree data structures.

:video_game: Code Sandbox

āœØ Storybook

:gift: NPM

Features

  • :package: Only 4kb minified & gzipped / no dependencies
  • :hammer_and_wrench: Perform tree operations with a simple function call
  • :rocket: Components made with only JSX and CSS
  • :nail_care: Make the components of your own with styling options

Data Structures Covered

  • Binary Search Tree

Trie, AVL Tree and more coming soon :see_no_evil:

Who is this library for?

If you are looking for a way to not just only display your data in a tree format but also interact with it, react-tree-vis might be for you. You can simply pass an array of numbers to display it or use our API to insert, delete, search and much more. With react-tree-vis, you can style your tree component with props or override with CSS. Everything is documented below! Also, I would recommend checking out other similar libraries too.

Documentation

Installation

# Yarn
yarn add react-tree-vis

# NPM
npm install react-tree-vis

Quick start

We are displaying data in BST and interacting with it using useTree. Play around with this example here.

import { BinarySearchTree, useTree } from 'react-tree-vis'
import { useState } from 'react'

export default function App() {
  const { ref, insert, remove } = useTree()

  const [insertValue, setInsertValue] = useState(0)
  const [removeValue, setRemoveValue] = useState(0)

  return (
    <div className="App">
      <input
        type="number"
        onChange={(elem) => setInsertValue(parseInt(elem.currentTarget.value))}
      />
      <button onClick={() => insert(insertValue)}>Insert</button>
      <br />
      <input
        type="number"
        onChange={(elem) => setRemoveValue(parseInt(elem.currentTarget.value))}
      />
      <button onClick={() => remove(removeValue)}>Remove</button>

      <BinarySearchTree data={[2, 1, 3]} ref={ref} />
    </div>
  )
}

BinarySearchTree

It organizes numbers in a binary search tree and exposes various styling options.

Props

PropTypeRequiredDescription
refReact.MutableRefObject<any>:x:Allows interaction with BST component. ref object passed, is obtained from useTree().
datanumber[]:x:Elements in the array are inserted into the tree on mount.
treeStylesobject:x:Allows overriding default style of the component.

treeStyles object

An object with properties described below can be passed to treeStyles prop to override default styles.

PropertyTypeDescriptionDefault
lineColorstringColor of the line connecting nodes#ccc
lineHoverColorstringHover color of the line connecting nodes#5f6674
lineRadiusstringRadius of curves in the line5px
nodeBorderstringBorder style of the nodes. Syntax of short-hand CSS border property is accepted here.none
nodeBorderRadiusstringBorder radius of the nodes200px
nodeBackgroundColorstringBackground color of the nodes#fff
nodeShadowstringShadow property of the nodes. Syntax of short-hand CSS shadow property accepted here.-5px -5px 20px #fff, 5px 5px 20px #babecc
nodeFontColorstringFont color of the text inside the nodes#666
nodeTextShadowstringFont shadow of text inside the nodes.none
nodeHighlightBorderstringBorder style of the highlighted nodes*. Syntax of short-hand CSS border property is accepted here.none
nodeHighlightBackgroundColorstringBackground color of the highlighted nodes#fff
nodeHighlightShadowstringShadow property of the highlighted nodes. Syntax of short-hand CSS shadow property accepted here.-5px -5px 20px #fff, 5px 5px 20px #babecc
nodeHighlightFontColorstringFont color of the text inside the highlighted nodes#fff
nodeHighlightTextShadowstringFont shadow of text inside the highlighted nodes.0 0 5px #fff, 0 0 10px #fff, 0 0 15px #00ff15, 0 0 20px #00ff15, 0 0 25px #00ff15, 0 0 30px #00ff15, 0 0 35px #00ff15
nodeHoverBorderstringBorder style of the hovered nodes. Syntax of short-hand CSS border property is accepted here.none
nodeHoverBackgroundColorstringBackground color of the hovered nodes#fff
nodeHoverShadowstringShadow property of the hovered nodes. Syntax of short-hand CSS shadow property accepted here.-1px -1px 5px #fff, 1px 1px 5px #babecc
nodeHoverFontColorstringFont color of the text inside the hovered nodes#002574
nodeHoverTextShadowstringFont shadow of text inside the hovered nodes.none
nodeNullFontColorstringFont color of the null nodes#7c7c7c2f
nodeNullHoverFontColorstringFont color of the hovered null nodes#ff0000b9
transitionDurationstringCSS transition duration0.5s
* Nodes searched successfully in the tree are highlighted.

This story allows you to play around with styles! (Refresh to apply styles. Working on improving UX here. Here is a rabbit for inconvenience caused. :rabbit:)

Feel styling options are limited? You can always override them with CSS. All the tree components are given id react-tree-vis. Refer to this CSS file for selectors. Also check out this codesandbox example.

useTree()

This hook allows you to interact with your tree. Insert, remove, search and so much more!

Usage

import { useTree } from 'react-tree-vis'

const tree = useTree()

It returns an object with the following properties.

PropertyTypeDescription
refReact.MutableRefObject<any>Pass this ref object to your tree component. It binds the functions returned by this hook to that component.
insert(value: number) => voidInserts the value
remove(value: number) => booleanremoves the value
search(value: number) => booleanSearches the value and return true if found. Also, node found is highlighted
getData(traversalOrder: TraversalOrderType) => number[]*Return traversal of the tree
clear() => voidRemoves all nodes
balance() => voidBalances the tree
generateRandomTree(countOfNodes: number) => voidRemoves all nodes and inserts countOfNodes random values.
checkTreeType() => BinaryTreeCheckType[]**Checks whether the current tree is balanced, complete, perfect or full
* TraversalOrderType = 'inorder' | 'postorder' | 'preorder'
** BinaryTreeCheckType = 'balanced' | 'complete' | 'perfect' | 'full'

Development

Install

yarn install

Develop

yarn storybook

Run tests

yarn test

Author

šŸ‘¤ Vandan Rogheliya

Show your support

Give a ā­ļø if this project helped you!

šŸ“ License

Copyright Ā© 2021 Vandan Rogheliya. This project is MIT licensed.