Inspired by Downshift, a simple, data-driven, light-weight React Tree Menu component that:
- does not depend on any UI framework
- fully customizable with
render props
and control props
- allows search
Usage
Install with the following command in your React app:
To generate a TreeMenu
, you need to provide data in the following structure.
And then import TreeMenu
and use it. By default you only need to provide data
. You can have more control over the behaviors of the components using the provided API.
API
props |
description |
type |
default |
data |
Data that defines the structure of the tree. You can nest it as many levels as you want, but note that it might cause performance issue. |
{[string]:TreeNode} | TreeNode[] |
- |
activeKey |
the node matching this key will be highlighted |
string |
'' |
onClickItem |
A callback function that defines the behavior when user clicks on an node |
(Item): void |
console.warn |
debounceTime |
debounce time for searching |
number |
125 |
openNodes |
you can pass an array of node names to make the branches open |
string[] |
null |
children |
a render props that provdes two props: search and items |
(ChildrenProps) => React.ReactNode |
- |
TreeNode
props |
description |
type |
default |
key |
Node name |
string |
- |
label |
the rendered text of a Node |
string | React.ReactNode |
'' |
index |
a number that defines the rendering order of this node on the same level; this is not needed if data is TreeNode[] |
number |
- |
nodes |
a node without this property means that it is the last child of its branch |
{[string]:TreeNode} | TreeNode[] |
- |
Item
props |
description |
type |
default |
hasNodes |
if a TreeNode is the last node of its branch |
boolean |
false |
isOpen |
if it is showing its children |
boolean |
false |
level |
the level of the current node (root is zero) |
number |
0 |
key |
key of a TreeNode |
string |
- |
label |
TreeNode label |
string | React.ReactNode |
- |
...other |
User defined props |
{[string]: any} |
- |
ChildrenProps
props |
description |
type |
default |
search |
A function that takes a string to filter the label of the item |
(value: string) => void |
- |
items |
An array of TreeMenuItem |
TreeMenuItem[] |
[] |
props |
description |
type |
default |
hasNodes |
if a TreeNode is the last node of its branch |
boolean |
false |
isOpen |
if it is showing its children |
boolean |
false |
level |
the level of the current node (root is zero) |
number |
0 |
key |
key of a TreeNode |
string |
- |
label |
TreeNode label |
string | React.ReactNode |
- |
active |
if current node is being selected |
boolean |
- |
onClick |
a callback function that is run when the node is clicked |
Function |
- |
...other |
User defined props |
{[string]: any} |
- |
GitHub