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

@henliwu1491/react-tree

v0.0.10

Published

[![npm](https://img.shields.io/npm/v/@henliwu1491/react-tree.svg?style=flat-square)](https://www.npmjs.com/package/@henliwu1491/react-tree) [![Build Status](https://img.shields.io/github/actions/workflow/status/@henliwu1491/react-tree/main.yml?branch=main

Downloads

73

Readme

React Tree

npm Build Status GitHub license

React Tree is a tree data structure ui library designed for easily building tree components.

Demo

Note: This project is for testing purposes only and is not intended for production use.

Features

  • 🪝  Hook provided: useTreeView hooks provide tree expanded & selected state for easily render tree component

  • 🖼️  Custom Icons: Supports custom icons using React nodes for enhanced visual appeal.

Installation

Using npm, Yarn, or PNPM:

  1. Install the package with npm:
npm install @henliwu1491/react-tree
  1. Install the package with Yarn:
yarn add @henliwu1491/react-tree
  1. Install the package with PNPM:
pnpm add @henliwu1491/react-tree

Choose the package manager that you prefer for installation.

Include CSS

import "@henliwu1491/react-tree/dist/style.css"

Usage

Example:

<TreeView />

import { TreeView } from '@henliwu1491/react-tree';

export default function Tree() {
  const [selectedId] = React.useState(['12']);
  const [expandedId] = React.useState(['2', '21', '221']);

  return <TreeView initialState={{ selectedId, expandedId }} data={data} />;
}
import { TreeView } from '@henliwu1491/react-tree';

export default function Tree() {
  const [selectedId] = React.useState(['12']);
  const [expandedId] = React.useState(['2', '21', '221']);

  return (
    <TreeView
      initialState={{ selectedId, expandedId }}
      data={data}
      getLabel={(item) => {
        if (item.type === 'leaf') {
          return (
            <div className="flex">
              <div>Leaf: {item.label}</div>
            </div>
          );
        }
        return item.label;
      }}
    />
  );
}
import { TreeView } from '@henliwu1491/react-tree';

export default function Tree() {
  const [selectedId] = React.useState(['12']);
  const [expandedId] = React.useState(['2', '21', '221']);

  return (
    <TreeView
      initialState={{ selectedId, expandedId }}
      data={data}
      icon={{
        expand: '▲',
        collapse: '▼',
        leaf: '🌱',
        checked: '☑',
        unchecked: '☐',
        indeterminate: '-', // Or <span>your custom React component</span>
      }}
    />
  );
}
export default function Tree() {
  const [selectedId, setSelectedId] = React.useState(['12']);
  const [expandedId, setExpandedId] = React.useState(['2', '21', '221']);

  return (
    <TreeView
      value={{ selectedId, expandedId }}
      data={data}
      onExpand={(item) => {
        setExpandedId((prev) =>
          prev.length === 0
            ? [item.value]
            : prev.indexOf(item.value) === -1
              ? [...prev, item.value]
              : prev.filter((id) => id !== item.value)
        );
      }}
      onSelect={(item) => {
        setSelectedId((prev) =>
          prev.length === 0
            ? [item.value]
            : prev.indexOf(item.value) === -1
              ? [...prev, item.value]
              : prev.filter((id) => id !== item.value)
        );
      }}
    />
  );
}

useTreeView Props

Options

Below are the available configuration options for the hook:

| Name | Type | Description | Default | | -------------- | ------------------ | ------------------------------------------------------------------------------------ | ------- | | initialState | TreeInitialState | Optional. | | | data | TreeRawData[] | Required. Your raw tree structure data. (must contain id, label and value key) | | | onExpand | function | Optional. Callback function you can get node item from the parameter. | | | onSelect | function | Optional. Callback function you can get node item from the parameter. | | | leafKey | string | Optional. Customized leaf node. | 'leaf' |

Instance

| Name | Type | Description | Default | | ---------------------- | ---------------------------- | ----------- | ------- | | expandedId | string[] | | | | selectedId | string[] | | | | data | TreeRawData[] | | | | onExpand | function | | | | onSelect | function | | | | setExpand | (string) => void | | | | checkNodeAndChildren | (string) => void | | | | checkSingleNode | (string) => void | | | | setInitialState | (TreeInitialState) => void | | |

<TreeView /> Props

Options

Below are the available configuration options for the component:

| Name | Type | Description | Default | | -------------- | ------------------ | ------------------------------------------------------------------------------------ | ------- | | initialState | TreeInitialState | Optional. | | | data | TreeRawData[] | Required. Your raw tree structure data. (must contain id, label and value key) | | | onExpand | function | Optional. Callback function you can get node item from the parameter. | | | onSelect | function | Optional. Callback function you can get node item from the parameter. | | | value | TreeInitialState | Optional. Control your own state. | | | icon | IconConfig | Optional. Provide your custom icon, React.ReactNode only. | | | getLabel | function | Optional. Your own label render function. | | | leafKey | string | Optional. Customized leaf node. | 'leaf' |

initialState

export type TreeInitialState = {
  expandedId: string[];
  selectedId: string[];
};

data

id, label, value are required. And if nested data is provided, children is also required.

export type TreeRawData = {
  id: string;
  label: string;
  value: string;
  children?: TreeRawData[];
  [key: string]: any;
};

Helper functions

| Name | Type | Description | Default | | --------------------------- | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------- | ------- | | getLeafNodes | function | (data: TreeData[], leafkey?: string) => TreeData[] | | | getNormalizedNodes | function | (data: TreeRawData[], level = 0) => TreeRawData[] | | | getFlattenNodes | function | (data: TreeRawData[]) => TreeRawData[] | | | getExpandedNodes | function | (nodes: TreeData[], expandedId: TreeData['value'][]) => TreeData[] | | | getSelectedNodes | function | (nodes: TreeData[], selectedId: TreeData['value'][]) => TreeData[] | | | getSelectedIdWithChildren | function | (nodes: TreeData[], selectedIds: TreeData['value'][], checkId: string, set: Set<string or number> = new Set(selectedIds)) => TreeData[] | |

🤝Contributing

We welcome contributions! If you find a bug or have an idea for improvement, please open an issue or submit a pull request on Github.

  1. Fork it
  2. Create your feature branch (git checkout -b new-feature)
  3. Commit your changes (git commit -am 'Add feature')
  4. Push to the branch (git push origin new-feature)
  5. Create a new Pull Request

Author ✨

💻   Henry Wu(吳亨利)

Licence

This project is licensed under the MIT License.