# 树形视图

- 页面: https://jiguang132.com/zh/resources/chakra-ui/docs/tree-view
- 资源: Chakra UI (https://jiguang132.com/zh/resources/chakra-ui.md)
- 全部指南: https://jiguang132.com/zh/resources/chakra-ui/docs.md

展示层级项目并支持展开与选择。节点 ID 必须稳定且唯一，展开与选中状态分开管理；异步节点、过滤和复选逻辑需要明确加载及错误反馈。

## 随附图标许可

源码查看器包含 ICON_ATTRIBUTION.md，以及本组示例实际使用的图标集合原始许可。再分发示例时请保留；react-icons 封装包的许可证不能替代各图标集合自身的条款。

- Lucide: ISC
- Radix Icons: MIT

## 安装与初始化

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0
```

渲染前配置 ChakraProvider、defaultSystem 和 Emotion。保留宿主项目的 React 版本并检查 peer 兼容。官方 npm 安装提供组件包，不会复制以下示例源码。locale、next-themes 和框架专属 SSR 配置见资源安装指南；npm 安装之外还需单独复制所选示例源码。

## 完整原生使用示例

这是固定版本的真实示例，不是通用 API 伪代码。复制完整源码依赖并保留命名导出；附加依赖逐例列在下方。

```tsx
"use client"

import { TreeView, createTreeCollection } from "@chakra-ui/react"
import { LuFile, LuFolder } from "react-icons/lu"

export const TreeViewBasic = () => {
  return (
    <TreeView.Root collection={collection} maxW="sm">
      <TreeView.Label>Tree</TreeView.Label>
      <TreeView.Tree>
        <TreeView.Node
          indentGuide={<TreeView.BranchIndentGuide />}
          render={({ node, nodeState }) =>
            nodeState.isBranch ? (
              <TreeView.BranchControl>
                <LuFolder />
                <TreeView.BranchText>{node.name}</TreeView.BranchText>
              </TreeView.BranchControl>
            ) : (
              <TreeView.Item>
                <LuFile />
                <TreeView.ItemText>{node.name}</TreeView.ItemText>
              </TreeView.Item>
            )
          }
        />
      </TreeView.Tree>
    </TreeView.Root>
  )
}

interface Node {
  id: string
  name: string
  children?: Node[]
}

const collection = createTreeCollection<Node>({
  nodeToValue: (node) => node.id,
  nodeToString: (node) => node.name,
  rootNode: {
    id: "ROOT",
    name: "",
    children: [
      {
        id: "node_modules",
        name: "node_modules",
        children: [
          { id: "node_modules/zag-js", name: "zag-js" },
          { id: "node_modules/pandacss", name: "panda" },
          {
            id: "node_modules/@types",
            name: "@types",
            children: [
              { id: "node_modules/@types/react", name: "react" },
              { id: "node_modules/@types/react-dom", name: "react-dom" },
            ],
          },
        ],
      },
      {
        id: "src",
        name: "src",
        children: [
          { id: "src/app.tsx", name: "app.tsx" },
          { id: "src/index.ts", name: "index.ts" },
        ],
      },
      { id: "panda.config", name: "panda.config.ts" },
      { id: "package.json", name: "package.json" },
      { id: "renovate.json", name: "renovate.json" },
      { id: "readme.md", name: "README.md" },
    ],
  },
})
```

## 全部示例与复现要求

示例名称用于选择演示，不是组件通用 variant 属性。复制所选示例全部 sourceFiles；compositions 别名表示本地文件，不是 npm 包。保留相对导入或映射到宿主真实目录。React peer 单独检查；编辑器示例保留一致的固定 Tiptap 扩展版本。

### 基础用法

`tree-view-basic` · 命名导出 `TreeViewBasic` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-basic.tsx`

### 不同尺寸

`tree-view-with-sizes` · 命名导出 `TreeViewWithSizes` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-with-sizes.tsx`

### 不同变体

`tree-view-with-variants` · 命名导出 `TreeViewWithVariants` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-with-variants.tsx`

### 不同颜色

`tree-view-with-colors` · 命名导出 `TreeViewWithColors` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-with-colors.tsx`
- `vendor/apps/compositions/src/lib/color-palettes.ts`

### 禁用节点

`tree-view-disabled-node` · 命名导出 `TreeViewDisabledNode` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-disabled-node.tsx`

### 受控展开

`tree-view-controlled-expansion` · 命名导出 `TreeViewControlledExpansion` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-controlled-expansion.tsx`

### 显式展开

`tree-view-explicit-expand` · 命名导出 `TreeViewExplicitExpand` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-explicit-expand.tsx`

### 展开图标

`tree-view-expand-icon` · 命名导出 `TreeViewExpandIcon` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-expand-icon.tsx`

### 移除缩进

`tree-view-remove-indentation` · 命名导出 `TreeViewRemoveIndentation` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-remove-indentation.tsx`

### 异步

`tree-view-async` · 命名导出 `TreeViewAsync` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-async.tsx`

### 筛选

`tree-view-with-filter` · 命名导出 `TreeViewWithFilter` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-with-filter.tsx`

### 收起动画

`tree-view-collapse-animation` · 命名导出 `TreeViewCollapseAnimation` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-collapse-animation.tsx`

### 展开收起全部

`tree-view-expand-collapse-all` · 命名导出 `TreeViewExpandCollapseAll` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 es-toolkit@1.47.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-expand-collapse-all.tsx`

### 外部状态仓库

`tree-view-with-store` · 命名导出 `TreeViewWithStore` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-with-store.tsx`

### 链接

`tree-view-with-links` · 命名导出 `TreeViewWithLinks` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-with-links.tsx`

### 多项选择

`tree-view-multi-select` · 命名导出 `TreeViewMultiSelect` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-multi-select.tsx`

### 复选框

`tree-view-checkbox` · 命名导出 `TreeViewCheckbox` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-checkbox.tsx`

### 修改

`tree-view-mutation` · 命名导出 `TreeViewMutation` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-mutation.tsx`

### 自定义图标

`tree-view-custom-icon` · 命名导出 `TreeViewCustomIcon` · 上游原始示例

```bash
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0
```

- `vendor/apps/compositions/src/examples/tree-view-custom-icon.tsx`

## 原生 API 与源码参考

源码查看器保留原始声明、import 与继承关系。Ark UI 事件契约、Chakra 样式属性和 recipe 类型仍以原生包为准；本指南不平铺或编造默认值。代码注释保留原文，不冒充正文翻译。

- `npm/@chakra-ui/react@3.37.0/dist/types/components/tree-view/index.d.ts`
- `npm/@chakra-ui/react@3.37.0/dist/types/components/tree-view/namespace.d.ts`
- `npm/@chakra-ui/react@3.37.0/dist/types/components/tree-view/tree-view.d.ts`

## 状态与安装后检查

执行严格类型检查、目标项目生产构建和实际操作。检查名称／焦点／键盘、深浅色、窄屏、原生支持的空／加载／错误状态以及卸载清理。预览限制远程接口、上传与下载，业务服务需自行配置。复制命令或预览就绪不代表安装完成或后端成功。

## 来源与许可边界

https://github.com/chakra-ui/chakra-ui/tree/2e7517745cff2fcde0b8012f136cf99611ff262a

本页是 JGuang 双语实用说明，不声称全文翻译上游 MDX。原始文档与示例字节保留在 vendor。保留仓库 MIT、compositions 的 ISC 元数据和独立依赖声明；原始许可元数据差异与缺失完整声明仍作为显式审查事项，不包含 Chakra Pro 付费内容。

---

JGuang 全部资源索引: https://jiguang132.com/llms.txt
