npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-data-tree

v0.1.7

Published

highly customizable node tree for react

Downloads

3

Readme

React Data Tree

Highly customizable node tree component with flatten data structure to interact intensly and easily

Installation

Install the library using your favorite dependency manager:

  npm install react-data-tree --save

Or using yarn:

  yarn add react-data-tree

Usage/Examples

import React from "react";
import { NodeTreeView, NodeOperations } from "react-data-tree";
import { AiOutlineCheckSquare, AiOutlineMinusSquare } from "react-icons/ai";

function App() {
  const [metaData, setMetaData] = React.useState({});
  const data = [
    {
      id: "unique-id-1",
      name: "Carmelita Rose",
      children: [
        {
          id: "unique-id-1-1",
          name: "Ilmi Carlitos",
        },
        {
          id: "unique-id-1-2",
          name: "Mojgan Cateline",
          children: [
            {
              id: "unique-id-1-2-1",
              name: "Ida Erich",
              children: [
                {
                  id: "unique-id-1-2-1-1",
                  name: "Ilmi Carlitos",
                },
                {
                  id: "unique-id-1-2-1-2",
                  name: "Mojgan Cateline",
                  children: [
                    {
                      id: "unique-id-1-2-1-2-1",
                      name: "Ida Erich",
                    },
                    {
                      id: "unique-id-1-2-1-2-2",
                      name: "Olimpia Bertrand",
                    },
                  ],
                },
              ],
            },
            {
              id: "unique-id-1-2-2",
              name: "Olimpia Bertrand",
            },
          ],
        },
      ],
    },
    {
      id: "unique-id-2",
      name: "Terezija Paulus",
      children: [
        {
          id: "unique-id-2-1",
          name: "Shemu'el Sneha",
        },
      ],
    },
  ];

  
  const CustomNodeComponent = ({ nodeData, parentNodeIds, isLastNode }) => {
    return (
      <div
        style={{ display: "flex", alignItems: "center", paddingLeft: "10px" }}
      >
        <div
          style={{
            padding: "1px",
            border: "solid 1px blue",
          }}
          onClick={(e) => {
            e.preventDefault();
            e.stopPropagation();
            setMetaData((oldData) => {
              const operations = new NodeOperations(
                oldData,
                nodeData?.id,
                parentNodeIds,
                data
              );
              return operations.defaultClickOperation();
            });
          }}
        >
          {metaData[nodeData?.id]?.isChecked ? (
            metaData[nodeData?.id]?.isSemiChecked ? (
              <AiOutlineMinusSquare />
            ) : (
              <AiOutlineCheckSquare />
            )
          ) : (
            <div className="p-2"></div>
          )}
        </div>

        <div
          style={{
            padding: "5px",
            marginLeft: "5px",
          }}
        >
          {nodeData.name}
        </div>
        <div
          onClick={() => {
            setMetaData((ldData) => {
              const oldData = JSON.parse(JSON.stringify(ldData));
              return {
                ...oldData,
                [nodeData?.id]: {
                  ...oldData[nodeData?.id],
                  isNodeExpanded: !(
                    oldData && oldData[nodeData?.id]?.isNodeExpanded
                  ),
                },
              };
            });
          }}
          style={{ cursor: "pointer", marginLeft: "10px" }}
        >
          {!isLastNode ? (
            metaData[nodeData?.id]?.isNodeExpanded ? (
              <div>{"^"}</div>
            ) : (
              <div>{">"}</div>
            )
          ) : null}
        </div>
      </div>
    );
  };

  return (
    <div>
      <NodeTreeView
        metaData={metaData}
        NodeComponent={CustomNodeComponent}
        data={data}
      />
    </div>
  );
}

export default App;

API Reference

Through metaData state we can control the whole node tree, on clicking operations with the methods from NodeOperations we can get status of all nodes

metaData

| key | Type | Description | | :-------- | :------- | :------------------------- | | isChecked | boolean | current clicked node selection status | | isSemiChecked | boolean | if all nested nodes are not selected of a parent node | | isNodeExpanded | boolean | is node expanded / nested nodes are visible status | | childrenIds | [string] | children ids of a parent | | children | undefined | children is a reserved key which will be always undefined |

data

type mainDataNode = {
  id: string;
  children?: dataType;
  [key: string]: any;
};

type data = Array<mainDataNode>

NodeComponent

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

| Parameter | Type | Description | | :-------- | :------- | :------------------------- | | nodeData | mainDataNode | all node details which you gave in json such as id,name except children | | parentNodeIds | [parent node id] | this array will contain all parents id of a node in outside to inside manner | | isLastNode | boolean | is current node is the deepest node |

Class NodeOperations

new NodeOperations(metaData, id, parentNodeIds, treeData)

| Parameter | Type | Description | | :-------- | :------- | :------------------------- | | metaData | React state | { uniqueNodeKey: { state details for the perticular node }} | | id | Unique node id | id should be always unique | | parentNodeIds | [parent node id] | this array will contain all parents id of a node in outside to inside manner | | treeData | JSON | recursive json data which |

Methods

  • getFlattenedData() will convert json tree data into metaData structure we can pass custom node modifier function as a parameter getFlattenedData(treeNodeModifier) which takes nodeDetails (mainDataNode) as input

  • getIndivisualCheckBoxClickedData({}) by default it will make isChecked=true of clicked node, also we can provide metaData and custom node modifier with it getIndivisualCheckBoxClickedData({ modifiedData, clickedObjectModifier })

  • getChildrenClickedData({}) by default it will make isChecked=true for all children of current node also we can provide metaData and custom children modifier with it getChildrenClickedData({ modifiedData, childObjectsModifier })

  • getAllParentCheckedData({}) by default it will make isChecked=true for all parent of current node also it calculates and makes isSemiChecked=true conditionally, we can provide metaData and custom parent modifier with it getAllParentCheckedData({ modifiedData, parentObjectsModifier })

  • defaultClickOperation() executes a click operation from provided data in the class and using above mentiond methods in default environment