0.0.2 • Published 2 years ago

react-menu-tree v0.0.2

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

React Menu Tree

Highly customizable and simple tab menu tree component

npm.io

Installation

Install the library using your favorite dependency manager:

  npm install react-menu-tree --save

Or using yarn:

  yarn add react-menu-tree

Usage/Examples

import React, { useEffect, useState } from "react";
import { MenuTree, nodeClickHandler } from "react-menu-tree";

function App() {
  const [rows, setRows] = useState([]);

  const data = [
    {
      name: "Edna Ray",
      children: [
        { name: "Jim Spencer" },
        {
          name: "Lee Carter",
        },
        {
          name: "Frankie Santiago",
          children: [
            {
              name: "Oscar Hicks",
            },
            {
              name: "Willis Warren",
            },
          ],
        },
        {
          name: "Philip Massey",
        },
      ],
    },
    {
      name: "Jennifer Mason",
    },
    {
      name: "Jimmie Potter",
      children: [
        {
          name: "Isabel Blake",
        },
        { name: "Daryl Banks" },
      ],
    },
    {
      name: "Claudia Moore",
    },
  ];

  const CustomNodeComponent = ({ rowNumber, nodeData, activeNodeIndex }) => {
    return (
      <div
        onClick={() => {
          setRows((oldData) => {
            return nodeClickHandler({
              rowNumber,
              nodeData,
              activeNodeIndex,
              rowData: oldData,
            });
          });
        }}
        style={{
          padding: "10px",
          borderBottom: nodeData?.isNodeActive ? "1px solid red" : "none",
        }}
        key={"row-node-" + activeNodeIndex}
      >
        {nodeData?.name}
      </div>
    );
  };

  // for first time data mount in the component 
  useEffect(() => {
    setRows((oldData) => {
      return nodeClickHandler({
        rowNumber: -1,
        nodeData: { children: data },
        activeNodeIndex: 0,
        rowData: oldData,
      });
    });
  }, []);

  return (
    <div className="App">
      <MenuTree rows={rows} CustomNodeComponent={CustomNodeComponent} />
    </div>
  );
}

export default App;

API Reference

CustomNodeComponent

make your custom node component, you will get some necessary details in props

ParameterTypeDescription
nodeData{ isNodeActive: boolean }from nodeData prop we will get isNodeActive status also will other details which are defined in data
rowNumbernumberclicked row number
activeNodeIndexnumberthis active node index will give the index of clicked node