New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@henliwu1491/react-tree

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@henliwu1491/react-tree

[![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/henry-wu-1130/react-tree/main.yml?branch=mai

latest
Source
npmnpm
Version
0.0.12
Version published
Maintainers
1
Created
Source

React Tree

npm Build Status GitHub license minzipped size Coverage Status

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

Live Demo

Check out our interactive demo showcasing basic usage, custom icons, and state management examples.

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

  • 🎯  Flexible Selection: Support both single and multiple node selection

  • 🎨  Customizable Labels: Easily customize node labels with your own render function

  • 🌲  Nested Data: Handle deeply nested tree structures with ease

Installation

# Using npm
npm install @henliwu1491/react-tree

# Using yarn
yarn add @henliwu1491/react-tree

# Using pnpm
pnpm add @henliwu1491/react-tree

Include CSS

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

Usage

Example:

<TreeView />

Basic usage
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} />;
}
With custom 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}
      getLabel={(item) => {
        if (item.type === "leaf") {
          return (
            <div className="flex">
              <div>Leaf: {item.label}</div>
            </div>
          );
        }
        return item.label;
      }}
    />
  );
}
With custom icon
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>
      }}
    />
  );
}
With controlled state
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:

NameTypeDescriptionDefault
initialStateTreeInitialStateOptional.
dataTreeRawData[]Required. Your raw tree structure data. (must contain id, label and value key)
onExpandfunctionOptional. Callback function you can get node item from the parameter.
onSelectfunctionOptional. Callback function you can get node item from the parameter.
leafKeystringOptional. Customized leaf node.'leaf'

Instance

NameTypeDescriptionDefault
expandedIdstring[]Array of expanded node IDs[]
selectedIdstring[]Array of selected node IDs[]
dataTreeRawData[]Processed tree data structure[]
onExpandfunctionCallback when node is expanded/collapsed-
onSelectfunctionCallback when node is selected/deselected-
setExpand(string) => voidFunction to expand/collapse a node-
checkNodeAndChildren(string) => voidSelect a node and all its children-
checkSingleNode(string) => voidSelect only the specified node-
setInitialState(TreeInitialState) => voidSet the initial expanded and selected state-

<TreeView /> Props

Options

Below are the available configuration options for the component:

NameTypeDescriptionDefault
initialStateTreeInitialStateOptional.
dataTreeRawData[]Required. Your raw tree structure data. (must contain id, label and value key)
onExpandfunctionOptional. Callback function you can get node item from the parameter.
onSelectfunctionOptional. Callback function you can get node item from the parameter.
valueTreeInitialStateOptional. Control your own state.
iconIconConfigOptional. Provide your custom icon, React.ReactNode only.
getLabelfunctionOptional. Your own label render function.
leafKeystringOptional. 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

NameTypeDescriptionDefault
getLeafNodesfunction(data: TreeData[], leafkey?: string) => TreeData[]
getNormalizedNodesfunction(data: TreeRawData[], level = 0) => TreeRawData[]
getFlattenNodesfunction(data: TreeRawData[]) => TreeRawData[]
getExpandedNodesfunction(nodes: TreeData[], expandedId: TreeData['value'][]) => TreeData[]
getSelectedNodesfunction(nodes: TreeData[], selectedId: TreeData['value'][]) => TreeData[]
getSelectedIdWithChildrenfunction(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.

  • Fork it
  • Create your feature branch (git checkout -b new-feature)
  • Commit your changes (git commit -am 'Add feature')
  • Push to the branch (git push origin new-feature)
  • Create a new Pull Request

Author ✨

💻   Henry Wu(吳亨利)

Licence

This project is licensed under the MIT License.

Keywords

react

FAQs

Package last updated on 07 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts