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-collapsible-tree-view

v1.0.1

Published

Customizable and easy-to-use collapsible tree component for showing data in nested structure where each level can be collapsed.

Downloads

2

Readme

react-collapsible-tree-view

Customizable and easy-to-use collapsible tree component for showing data in nested structure where each level can be collapsed.

NPM JavaScript Style Guide

Install

npm install --save react-collapsible-tree-view

Usage

The CollapsibleTree component contains a prop called renderItem, representing a component being used for rendering a single tree item. The properties that are passed to it are of type ITreeRenderItemProps<T>, where T is the type of the value field in the tree object.

interface ITreeRenderItemProps<T> {
    data: T,
    depth: number,
    toggle: () => void,
    isExpanded?: boolean,
    isLeaf?: boolean
}

Field data represents the value that a single tree node holds (the value field in the object you pass).

Each tree node, besides the data, has the state that can be either expanded or collapsed. You can toggle that state using the toggle function that is passed as a property. To check if the item is expanded, you can access the isExpanded property.

When building custom tree node components, you may also want to check if the node is the leaf node, such that it cannot be further expanded since it doesn't have any children. To do that, use the isLeaf boolean property.

To access the depth/level of the tree node, use the depth property.

For the best example, take a look at the custom tree item example:

import React from "react";

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faChevronUp, faChevronDown } from "@fortawesome/free-solid-svg-icons";
import { ITreeRenderItemProps } from "react-collapsible-tree-view"

export const CustomTreeItem = (props : ITreeRenderItemProps<string>) => {
    return (
        <div className="custom-tree-item-container">
            <button disabled={props.isLeaf} onClick={props.toggle}>
                <FontAwesomeIcon icon={props.isExpanded ? faChevronUp : faChevronDown} />
            </button>
            
            <span className="custom-tree-item-data">Level {props.depth} - Data: {props.data}</span>
        </div>
    );
}

Of course, it is worth mentioning that when you're working in plain JavaScript, you don't need to explicitly import and define property types.

Here's how the final code looks now:

import React from 'react'
import { CollapsibleTree } from 'react-collapsible-tree-view'
import { CustomTreeItem } from './CustomTreeItem';

const App = () => {
  const tree = {
    value: "Hello",
    children: [
      {
        value: "I am child",
        children: [
          {
            value: "Another child",
            children: []
          }
        ]
      },
      {
        value: "I am another child",
        children: []
      }
    ]
  };

  return (
    <div>
      <CollapsibleTree keyExtractor={(item) => item} renderItem={CustomTreeItem} data={tree} />
    </div>
  );
}

export default App;

Properties of the CollapsibleTree component

|Property name|Descriptipn|Required|Type|Default value| |-|-|-|-|-| |keyExtractor|Which field of the data object to be used as a list key.|Yes|(item : T) => string|undefined| |data|The tree object that should be used for rendering the tree. Must has value of the type T, and children of tree nodes (TreeData<T>)|Yes|TreeData<T>|undefined| |renderItem|Component that should be used for rendering a single tree item.|Yes|(props: ITreeRenderItemProps<T>) => ReactNode|undefined| |depth|Number representing the starting / current level of the tree node. Should not be altered unless you want to change the beginning depth level of the first node.|No|number|0|

See examples for more detailed source files.

License

MIT © Marwollo