1.0.0 • Published 6 years ago

react-simple-tree-menu v1.0.0

Weekly downloads
6,720
License
MIT
Repository
github
Last release
6 years ago

React Simple Tree Menu

npm version CircleCI

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

Check Storybook Demo.

Usage

Install with the following command in your React app:

npm i react-simple-tree-menu
// or
yarn add react-simple-tree-menu

To generate a TreeMenu, you need to provide data in the following structure.

// as an array
const treeData = [
  {
    key: 'first-level-node-1',
    label: 'Node 1 at the first level',
    ..., // any other props you need, e.g. url
    nodes: [
      {
        key: 'second-level-node-1',
        label: 'Node 1 at the second level',
        nodes: [
          {
            key: 'third-level-node-1',
            label: 'Last node of the branch',
            nodes: [] // you can remove the nodes property or leave it as an empty array
          },
        ],
      },
    ],
  },
  {
    key: 'first-level-node-2',
    label: 'Node 2 at the first level',
  },
];
// or as an object
const treeData = {
  'first-level-node-1': {               // key
    label: 'Node 1 at the first level',
    index: 0, // decide the rendering order on the same level
    ...,      // any other props you need, e.g. url
    nodes: {
      'second-level-node-1': {
        label: 'Node 1 at the second level',
        index: 0,
        nodes: {
          'third-level-node-1': {
            label: 'Node 1 at the third level',
            index: 0,
            nodes: {} // you can remove the nodes property or leave it as an empty array
          },
        },
      },
    },
  },
  'first-level-node-2': {
    label: 'Node 2 at the first level',
    index: 1,
  },
};

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.

import TreeMenu from 'react-simple-tree-menu'
...
// Use the default minimal UI
<TreeMenu data={treeData} />

// Use any third-party UI framework
<TreeViewMenu
  data={treeData}
  onClickItem={({ key, label, ...props }) => {
    this.navigate(props.url); // user defined prop
  }}
  debounceTime={125}>
    {({ search, items }) => (
        <>
          <Input onChange={e => search(e.target.value)} placeholder="Type and search" />
          <ListGroup>
            {items.map(props => (
              // You might need to wrap the third-party component to consume the props
              // check the story as an example
              // https://github.com/iannbing/react-simple-tree-menu/blob/master/stories/index.stories.js
              <ListItem {...props} />
            ))}
          </ListGroup>
        </>
    )}
</TreeViewMenu>

API

TreeViewMenu

propsdescriptiontypedefault
dataData 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[]-
activeKeythe node matching this key will be highlightedstring''
onClickItemA callback function that defines the behavior when user clicks on an node(Item): voidconsole.warn
debounceTimedebounce time for searchingnumber125
openNodesyou can pass an array of node names to make the branches openstring[]null
childrena render props that provdes two props: search and items(ChildrenProps) => React.ReactNode-

TreeNode

propsdescriptiontypedefault
keyNode namestring-
labelthe rendered text of a Nodestring | React.ReactNode''
indexa number that defines the rendering order of this node on the same level; this is not needed if data is TreeNode[]number-
nodesa node without this property means that it is the last child of its branch{string:TreeNode} | TreeNode[]-

Item

propsdescriptiontypedefault
hasNodesif a TreeNode is the last node of its branchbooleanfalse
isOpenif it is showing its childrenbooleanfalse
levelthe level of the current node (root is zero)number0
keykey of a TreeNodestring-
labelTreeNode labelstring | React.ReactNode-
...otherUser defined props{string: any}-

ChildrenProps

propsdescriptiontypedefault
searchA function that takes a string to filter the label of the item(value: string) => void-
itemsAn array of TreeMenuItemTreeMenuItem[][]

TreeMenuItem

propsdescriptiontypedefault
hasNodesif a TreeNode is the last node of its branchbooleanfalse
isOpenif it is showing its childrenbooleanfalse
levelthe level of the current node (root is zero)number0
keykey of a TreeNodestring-
labelTreeNode labelstring | React.ReactNode-
activeif current node is being selectedboolean-
onClicka callback function that is run when the node is clickedFunction-
...otherUser defined props{string: any}-

Dependencies

  • lodash: some light-weight utility functions are used, e.g. get, isEmpty, and merge. babel-plugin-import is used for only importing used modules to reduce bundle size; only the used functions are bundled.

Code conventions

1.1.18

5 years ago

1.1.18-rc.1

5 years ago

1.1.17

5 years ago

1.1.17-rc.0

5 years ago

1.1.16

5 years ago

1.1.15

5 years ago

1.1.14

5 years ago

1.1.13

5 years ago

1.1.12

6 years ago

1.1.11

6 years ago

1.1.10-rc.2

6 years ago

1.1.10-rc.1

6 years ago

1.1.10-rc.0

6 years ago

1.1.11-rc.0

6 years ago

1.1.10

6 years ago

1.1.9

6 years ago

1.1.9-rc.1

6 years ago

1.1.8-rc.2

6 years ago

1.1.8

6 years ago

1.1.8-rc.1

6 years ago

1.1.7

6 years ago

1.1.7-rc.2

6 years ago

1.1.7-rc.1

6 years ago

1.1.6-rc.3

6 years ago

1.1.6-rc.2

6 years ago

1.1.6

6 years ago

1.1.6-rc.1

6 years ago

1.1.5

6 years ago

1.1.5-rc.1

6 years ago

1.1.4-rc.5

6 years ago

1.1.4

6 years ago

1.1.4-rc.4

6 years ago

1.1.4-rc.3

6 years ago

1.1.4-rc.2

6 years ago

1.1.4-rc.1

6 years ago

1.1.3

6 years ago

1.1.3-rc.2

6 years ago

1.1.3-rc.1

6 years ago

1.1.2

6 years ago

1.1.2-rc.2

6 years ago

1.1.2-rc.1

6 years ago

1.1.1

6 years ago

1.1.1-rc.2

6 years ago

1.1.1-rc.1

6 years ago

1.1.0

6 years ago

1.1.0-rc.5

6 years ago

1.1.0-rc.4

6 years ago

1.1.0-rc.3

6 years ago

1.1.0-rc.2

6 years ago

1.1.0-rc.1

6 years ago

1.0.7

6 years ago

1.0.7-rc.7

6 years ago

1.0.7-rc.6

6 years ago

1.0.7-rc.5

6 years ago

1.0.7-rc.4

6 years ago

1.0.7-rc.3

6 years ago

1.0.7-rc.2

6 years ago

1.0.7-rc.1

6 years ago

1.0.6-rc.5

6 years ago

1.0.6

6 years ago

1.0.6-rc.4

6 years ago

1.0.6-rc.3

6 years ago

1.0.6-rc.2

6 years ago

1.0.6-rc.1

6 years ago

1.0.5

6 years ago

1.0.5-rc.2

6 years ago

1.0.5-rc.1

6 years ago

1.0.4-rc.5

6 years ago

1.0.4-rc.4

6 years ago

1.0.4

6 years ago

1.0.4-rc.3

6 years ago

1.0.4-rc.2

6 years ago

1.0.4-rc.1

6 years ago

1.0.3-rc.2

6 years ago

1.0.3

6 years ago

1.0.3-rc.1

6 years ago

1.0.2

6 years ago

1.0.2-rc.2

6 years ago

1.0.2-rc.1

6 years ago

1.0.1-rc.4

6 years ago

1.0.1-rc.3

6 years ago

1.0.1

6 years ago

1.0.1-rc.2

6 years ago

1.0.1-rc.1

6 years ago

1.0.0

6 years ago

1.0.0-rc.3

6 years ago

1.0.0-rc.2

6 years ago

1.0.0-rc.1

6 years ago

0.1.4-rc.7

6 years ago

0.1.4-rc.6

6 years ago

0.1.4-rc.5

6 years ago

0.1.4-rc.4

6 years ago

0.1.4-rc.3

6 years ago

0.1.4-rc.2

6 years ago

0.1.4-rc.1

6 years ago

0.1.3

6 years ago

0.1.2-rc.1

6 years ago

0.1.2

6 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.5

7 years ago

0.0.5-beta3

7 years ago

0.0.5-beta2

7 years ago

0.0.5-beta1

7 years ago

0.0.4

7 years ago

0.0.4-beta6

7 years ago

0.0.4-beta5

7 years ago

0.0.4-beta4

7 years ago

0.0.4-beta3

7 years ago

0.0.4-beta2

7 years ago

0.0.4-beta1

7 years ago

0.0.3

7 years ago

0.0.3-beta01

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago