File Tree
通过展开分支和键盘选择浏览嵌套文件与文件夹。
适用场景
- 当界面需要以下行为时使用 File Tree:通过展开分支和键盘选择浏览嵌套文件与文件夹。
安装
选择安装方式,用法示例将同步更新导入路径。
$ bunx --bun shadcn-vue@2.8.2 add https://orvui.efu.me/r/file-tree.json
首次使用时,将安装的 src/components/orvui/styles.css 导入项目样式入口。
示例
预览
packages/vue/src/motion/file-tree/demo.vue
<script setup lang="ts">
import { h } from "vue";
import { Braces, FileCode2, FileJson2, FileText, Palette } from "lucide-vue-next";
import { FileTree, FileTreeFile, FileTreeFolder } from "@/components/orvui/motion/file-tree";
const source = () => h(Braces, { class: "size-4 text-sky-500" });
const entries = [
{ value: "app", name: "App.vue", icon: () => h(FileCode2, { class: "size-4 text-sky-500" }) },
{ value: "styles", name: "style.css", icon: () => h(Palette, { class: "size-4 text-violet-500" }) },
];
const documents = [
{ value: "package", name: "package.json", icon: () => h(FileJson2, { class: "size-4 text-amber-500" }) },
{ value: "readme", name: "README.md", icon: () => h(FileText, { class: "size-4 text-muted-foreground" }) },
];
</script>
<template>
<div class="flex min-h-[420px] w-full items-start justify-center px-4 pt-10"><div class="w-full max-w-xs p-2">
<FileTree default-value="file-tree" :default-expanded-ids="['src', 'components']" aria-label="Project files">
<FileTreeFolder value="src" name="src"><FileTreeFolder value="components" name="components"><FileTreeFile v-for="file in ['file-tree', 'button']" :key="file" :value="file" :name="`${file}.vue`" :icon="source" /></FileTreeFolder><FileTreeFile v-for="entry in entries" :key="entry.value" v-bind="entry" /></FileTreeFolder>
<FileTreeFolder value="public" name="public"><FileTreeFile v-for="file in ['logo', 'grid']" :key="file" :value="file" :name="`${file}.svg`" /></FileTreeFolder>
<FileTreeFile v-for="entry in documents" :key="entry.value" v-bind="entry" />
</FileTree>
</div></div>
</template>
packages/vue/src/motion/file-tree/src/FileTree.vue
状态与事件
需要由表单、路由或父组件管理时,绑定下列状态并接收对应更新。v-model 是属性与 update 事件的简写;值的类型应与 API 一致。
| 组件 | Vue 绑定 | 更新事件 |
|---|---|---|
FileTree | v-modelnull | string | update:modelValue |
FileTree | v-model:expandedIdsstring[] | update:expandedIds |
可选受控属性未传入时,按组件文档的默认行为工作;不要用固定的 false 或空值代替未传入。异步请求和持久化在应用中处理,再把结果传回组件。
API 参考
FileTree
modelValue?null | string—defaultValue?null | stringnullexpandedIds?string[]—defaultExpandedIds?string[]() => []ariaLabel?string"Files"indent?number18classNames?FileTreeClassNames—事件
update:modelValue—@update:modelValuechange—@changeupdate:expandedIds—@update:expandedIdsexpandedChange—@expandedChangeFileTreeFile
valuestring—namestring—icon?any—disabled?booleanfalseFileTreeFolder
valuestring—namestring—icon?any—disabled?booleanfalse无障碍
为交互控件提供明确的无障碍名称。保留文档中的键盘路径、可见焦点与减少动态效果状态;自定义插槽应继续保留所替代部件的语义。
常见问题
- 如何配置 File Tree?
- 通过 modelValue、defaultValue、expandedIds、defaultExpandedIds、ariaLabel、indent、classNames 配置 File Tree;API 表列出其类型与默认值。
- 如何连接到应用状态?
- 监听 update:modelValue、change、update:expandedIds、expandedChange,将组件连接到应用状态。
- 可以在服务端渲染吗?
- 可以在 Vue SSR 应用中正常渲染。服务端与客户端的初始 props 应保持一致;浏览器测量与交互资源在挂载后启动。
更新于
来源与许可 ↗MIT · 0.1.1