@asup/tree-of-nodes

中文文档
3.1.1发布于 2026/10/13MITTypeScriptESM + CJS近 30 天下载 1795

REACT Typescript Treeview component

原始使用文档
文档目录7 节
  1. Installation
  2. Tree-of-nodes Usage
  3. Properties
  4. TreeNodeData
  5. iNodeUpdate
  6. Single item selection
  7. Multiple item selection

npm size npm bundle size License: MIT

@asup/tree-of-notes

REACT treeview, because I couldn't quite find what I wanted.

Works with/needs bootstrap, react-bootstrap, react-bootstrap-icons. Uses a context menu to facilitate Add/Rename/Remove actions.

Installation

# with npm
npm install @asup/tree-of-nodes

Tree-of-nodes Usage

import { iNodeUpdate, TreeNodeData, TreeOfNodes } from '@asup/tree-of-nodes';
import '@asup/tree-of-nodes/dist/style.css';

... inside REACT component

<TreeOfNodes<T>
  id: string;
  nodeList: TreeNodeData<T>[];
  roots: Key[];
  multiSelect?: boolean;
  selectedIds?: Key | Key[];
  handleSelect?: (ret: Key | Key []) => void;
  onAdd?: (parentId: Key, newName: string) => Promise<iNodeUpdate>;
  onRemove?: (childId: Key) => Promise<iNodeUpdate>;
  onRename?: (childId: Key, newName: string) => Promise<iNodeUpdate>;
  canRemoveRoot?: boolean;
  canRenameRoot?: boolean;
  canAddChildren?: boolean;
  canRemoveChildren?: boolean;
  canRenameChildren?: boolean;
  nodeHighlight?: string;
  textHighlight?: string;
  />

The component expects a list of nodes of type TreeNodeData<T> with unique React.Keys.

Properties

PropDescriptionDefault
idHTML id attribute
nodeListArray of node data
rootsOne or more node Keys to use as the root of the tree
multiSelectAllow selection of more than 1 idfalse
selectedIdsCurrently selected list
handleSelectFunction called when a node is clicked (selected), this should be used to update the selectedId() => {return;}
onAddFunction called when a new node is added, this should be used to update the nodeList
onRemoveFunction called when a node is removed, this should be used to update the nodeList
onRenameFunction called when a node is renamed, this should be used to update the nodeList
canRemoveRootAllows removal of a root node in combination with specification of onRemove functionfalse
canRenameRootAllows renaming of a root node in combination with specification of onRename functionfalse
canAddChildrenAllows addition of children in combination with specification of onAdd functionfalse
canRemoveChildrenAllows renaming of non-root nodes in combination with specification of onRemove functionfalse
canRenameChildrenAllows renaming of non-root nodes in combination with specification of onRename functionfalse
nodeHighlightSelected node highlight colourred
textHighlightSelected text highlight colourrgba(255,0,0,0.2
spellCheckUse/disable browser spell check'true'

TreeNodeData

Required format for list of nodes, includes a holder for any type of data, which is there specified in the TreeOfNodes specification.

export type TreeNodeData<T> = {
  id: Key;
  label: string;
  parentId?: Key;
  data: T;
};

iNodeUpdate

Use this format to return from update functions.

export interface iNodeUpdate {
  success: boolean;
  ErrorText?: string;
}

Handling clicks

Context menu clicks on a node will result in a context menu being displayed with "Add", "Rename" and "Delete" options being displayed.

Clicks on an expander will either expand or close the node's clidren

Clicks on a name will select that item

Clicks on a checkbox will select that item and all its descendents.

Single item selection

To handle one (and only one) item being selected, it is suggested to use these base options

  <TreeOfNodes<T>
    id={'...'}
    nodeList={[...]}
    roots={[...]}
    selected={selected}
    handleSelect={async (i) => {
      setSelected([i]);
    }}
  />

Multiple item selection

To handle multiple selections, it is suggested to use these base options

  <TreeOfNodes<T>
    id={'...'}
    nodeList={[...]}
    roots={[...]}
    showCheckBox
    selected={selected}
    handleSelect={async (i) => {
      if (Array.isArray(i)) {
        setSelected(
          selected.includes(i[0])
            ? selected.filter((s) => !i.includes(s))
            : [...selected, ...i.filter((n) => !selected.includes(n))],
        );
      } else {
        setSelected(
          selected.includes(i) ? selected.filter((s) => s !== i) : [...selected, i],
        );
      }
    }}
  />

版本与发布记录

@asup/tree-of-nodes 共发布 21 个版本,首次发布于 2022/11/07;过去 12 个月发布了 3 个版本。

版本发布日期距今
3.1.1latest2026/10/1326 天前
3.1.02025/12/0811 个月前
3.0.02025/12/0111 个月前
2.0.62025/06/151.4 年前
2.0.52025/06/141.4 年前
2.0.42025/06/141.4 年前

关于 @asup/tree-of-nodes 的常见问题

@asup/tree-of-nodes 是什么?

REACT Typescript Treeview component

如何安装 @asup/tree-of-nodes?

在项目目录运行 npm install @asup/tree-of-nodes,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^19.0.0)、react-dom(^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@asup/tree-of-nodes 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@asup/tree-of-nodes 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@asup/tree-of-nodes 可以商用吗?

@asup/tree-of-nodes 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@asup/tree-of-nodes 的下载量有多少?

根据 npm 官方下载统计,@asup/tree-of-nodes 在 2026/10/07 – 2026/11/05 这 30 天内被下载 1,795 次,日均约 60 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@asup/tree-of-nodes 还在维护吗?

最新版本 3.1.1 发布于 2026/10/13(26 天前);至今共发布 21 个版本,其中过去 12 个月发布了 3 个;首次发布于 2022/11/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。