@cw-devops-fe/vue-tree-vue3 0.0.0-stage → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,413 @@
1
- # Temporary Holding Version
1
+ # @cw-devops-fe/vue-tree-vue3
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 本分支为 npmjs 公网发行版,包名使用 `@cw-devops-fe/` scope,发布源为 `https://registry.npmjs.org/`。
4
+
5
+ Vue 3 树组件,版本 `1.1.0`,要求 Vue `^3.3.0`。支持节点选择、复选框、过滤、懒加载、拖拽和全层级节点虚拟滚动。
6
+
7
+ ## 安装与引入
8
+
9
+ ```sh
10
+ pnpm add @cw-devops-fe/vue-tree-vue3 --registry=https://registry.npmjs.org/
11
+ ```
12
+
13
+ 普通树只需引入 VueTree 的样式。VueTree 的运行时依赖会安装 VirtualList。开启虚拟滚动的项目如需从应用代码直接导入 VirtualList 的样式,可声明 `pnpm add @cw-devops-fe/virtual-list-vue3 --registry=https://registry.npmjs.org/`。
14
+
15
+ ```ts
16
+ import VueTree from '@cw-devops-fe/vue-tree-vue3'
17
+ // 也可以使用 import { VueTree } from '@cw-devops-fe/vue-tree-vue3'
18
+ import '@cw-devops-fe/vue-tree-vue3/style.css'
19
+ ```
20
+
21
+ ## 基础用法
22
+
23
+ ```vue
24
+ <script setup lang="ts">
25
+ import { VueTree } from '@cw-devops-fe/vue-tree-vue3'
26
+ import '@cw-devops-fe/vue-tree-vue3/style.css'
27
+
28
+ const data = [{ id: 1, label: 'Root', children: [{ id: 2, label: 'Child' }] }]
29
+ </script>
30
+
31
+ <template>
32
+ <VueTree :data="data" node-key="id" show-checkbox />
33
+ </template>
34
+ ```
35
+
36
+ 开启虚拟滚动时,在已有树样式之外导入 `@cw-devops-fe/virtual-list-vue3/style.css`,再给容器设置可计算的高度。
37
+
38
+ ```vue
39
+ <VueTree :data="data" node-key="id" show-checkbox virtual style="height: 400px" />
40
+ ```
41
+
42
+ `nodeKey` 指定业务数据中的唯一键字段,所有层级的键都应唯一。使用默认勾选、默认展开、按 key 查找或虚拟定位等功能时必须配置它,建议统一使用字符串或数字作为键。
43
+
44
+ ## Props
45
+
46
+ 下面使用 JavaScript 属性名;在模板中可写成 `node-key`、`show-checkbox` 等短横线形式。所有 props 都是可选项,使用特定功能时再满足对应的依赖条件。
47
+
48
+ ### 数据与展示
49
+
50
+ | 属性 | 类型 | 默认值 | 说明 |
51
+ | --- | --- | --- | --- |
52
+ | `data` | `object[]` | `[]` | 树的根节点数组,默认用 `label` 显示文本、用 `children` 读取子节点 |
53
+ | `nodeKey` | `string` | 未设置 | 原始数据中的唯一键字段名,例如 `'id'`,不是节点的具体 id 值 |
54
+ | `props` | `object` | `{ children: 'children', label: 'label', disabled: 'disabled' }` | 节点字段映射,具体配置见下表 |
55
+ | `emptyText` | `string` | `'暂无数据'` | 无可见节点时的提示,可由 `empty` 插槽替换 |
56
+ | `renderAfterExpand` | `boolean` | `true` | 普通树中子节点首次展开后才创建其 DOM,设为 `false` 时提前渲染已加载的子节点。虚拟模式由滚动窗口决定挂载范围 |
57
+ | `renderContent` | `function` | 未设置 | 自定义节点内容的渲染函数,优先于默认插槽,签名见“回调函数” |
58
+ | `highlightCurrent` | `boolean` | `false` | 是否高亮当前节点,与复选框勾选是两种独立状态 |
59
+ | `currentNodeKey` | `string / number` | 未设置 | 当前节点的 key,需要配合 `nodeKey`;主动清空当前节点建议调用 `setCurrentKey(null)` |
60
+ | `indent` | `number` | `18` | 相邻层级的横向缩进,单位为 px |
61
+ | `iconClass` | `string` | 未设置 | 替换展开图标的 CSS 类名;图标样式由使用方提供,未设置时使用内置图标 |
62
+ | `virtual` | `boolean` | `false` | 开启全层级虚拟滚动。模板中写 `virtual` 或 `:virtual="true"`,并给树设置可计算的高度。详见[虚拟滚动与使用限制](#虚拟滚动与使用限制) |
63
+ | `deferContent` | `boolean` | `false` | 虚拟树分帧渲染 `renderContent` 和默认插槽,例如 `<VueTree virtual defer-content />`。文件名、展开和勾选可立即使用,插槽内的菜单等控件会随自定义内容显示 |
64
+
65
+ ### 展开与勾选
66
+
67
+ | 属性 | 类型 | 默认值 | 说明 |
68
+ | --- | --- | --- | --- |
69
+ | `defaultExpandAll` | `boolean` | `false` | 创建节点时是否默认展开;不是动态控制所有节点展开状态的开关 |
70
+ | `defaultExpandedKeys` | `Array<string / number>` | 未设置 | 默认展开的节点 key,需要 `nodeKey`;更新此数组会展开相应节点,不会自动收起原来已展开的节点 |
71
+ | `autoExpandParent` | `boolean` | `true` | 根据默认展开 key 展开目标节点时,是否同时展开其祖先 |
72
+ | `expandOnClickNode` | `boolean` | `true` | 点击节点内容时是否切换展开;设为 `false` 后仍可点击展开图标 |
73
+ | `accordion` | `boolean` | `false` | 开启展开互斥。普通树向已注册节点广播折叠。虚拟树展开时保留该节点及其祖先,收起其余节点 |
74
+ | `showCheckbox` | `boolean` | `false` | 是否显示节点复选框 |
75
+ | `checkStrictly` | `boolean` | `false` | 为 `true` 时父子节点勾选互不影响;默认进行父子联动和半选计算 |
76
+ | `checkOnClickNode` | `boolean` | `false` | 点击节点内容时是否同时切换勾选状态;禁用节点不会通过点击内容切换勾选 |
77
+ | `checkDescendants` | `boolean` | `false` | 懒加载且非严格勾选时,是否在勾选处理中加载尚未加载的后代并参与联动 |
78
+ | `defaultCheckedKeys` | `Array<string / number>` | 未设置 | 默认勾选的节点 key,需要 `nodeKey`;若需完整替换当前勾选集合,使用 `setCheckedKeys` |
79
+
80
+ ### 懒加载、过滤与拖拽
81
+
82
+ | 属性 | 类型 | 默认值 | 说明 |
83
+ | --- | --- | --- | --- |
84
+ | `lazy` | `boolean` | `false` | 是否使用 `load` 回调按需加载节点;建议在创建树时确定,不作为运行时切换数据模式的开关 |
85
+ | `load` | `function` | 未设置 | 懒加载回调,必须通过 `resolve(children)` 完成本次加载 |
86
+ | `loadingKey` | `string` | 未设置 | 调用方自行加载子节点时,在虚拟树上指定加载状态字段。例如 `loading-key="pending"` 读取节点的 `data.pending`。加载动画显示在展开箭头的位置,点击仍可展开或收起 |
87
+ | `filterNodeMethod` | `function` | 未设置 | 调用 ref 的 `filter(value)` 时执行,返回当前节点是否匹配 |
88
+ | `draggable` | `boolean` | `false` | 是否开启基于浏览器原生拖拽事件的节点拖动 |
89
+ | `allowDrag` | `function` | 未设置 | 判断节点能否开始拖拽,未设置时不额外限制 |
90
+ | `allowDrop` | `function` | 未设置 | 判断能否放置到目标节点之前、内部或之后,未设置时由树内部规则判断 |
91
+
92
+ ### 节点字段映射
93
+
94
+ 组件 prop 名称是 `props`,其对象字段如下:
95
+
96
+ | 字段 | 类型 | 默认映射 | 说明 |
97
+ | --- | --- | --- | --- |
98
+ | `label` | `string / (data, node) => string` | `'label'` | 显示文本,可指定字段名或计算函数 |
99
+ | `children` | `string` | `'children'` | 子节点数组的字段名。增删节点也会使用该字段,建议只传字符串 |
100
+ | `disabled` | `string / (data, node) => boolean` | `'disabled'` | 控制复选框禁用,不等于禁止点击、选中或展开节点 |
101
+ | `isLeaf` | `string / (data, node) => boolean` | 未设置 | 懒加载时标记节点是否为叶子,需显式配置映射后才读取 |
102
+
103
+ ```vue
104
+ <VueTree
105
+ :data="[{ id: 'a', name: '业务目录', items: [], locked: true }]"
106
+ node-key="id"
107
+ show-checkbox
108
+ :props="{ label: 'name', children: 'items', disabled: 'locked' }"
109
+ />
110
+ ```
111
+
112
+ ### 回调函数
113
+
114
+ 下表中的 `TreeNode` 表示树内部节点模型,不是原始业务数据,也不是本包导出的公共 TypeScript 类型。可通过事件参数或 `getNode()` 获取;`data` 才是 `data` 数组中的业务记录。
115
+
116
+ | 回调 | 签名 | 返回值或行为 |
117
+ | --- | --- | --- |
118
+ | `renderContent` | `(h, { _self, node, data, store }) => VNodeChild` | 返回节点内容,`h` 为 Vue 渲染函数,`_self` 为内部树上下文 |
119
+ | `load` | `(node: TreeNode, resolve: (children: object[]) => void) => void` | 通过 `resolve` 提交子节点,返回 Promise 本身不会自动结束加载 |
120
+ | `filterNodeMethod` | `(value, data, node: TreeNode) => boolean` | 返回 `true` 表示节点匹配;祖先还会根据可见子节点保留显示 |
121
+ | `allowDrag` | `(node: TreeNode) => boolean` | 返回 `false` 阻止拖拽 |
122
+ | `allowDrop` | `(draggingNode: TreeNode, dropNode: TreeNode, type: 'prev' / 'inner' / 'next') => boolean` | 返回是否允许放置,分别对应目标之前、内部和之后 |
123
+
124
+ ## 事件
125
+
126
+ 事件中的 `data` 是原始业务数据,`node` 是内部节点模型。`view` 是包含节点 DOM 的对象 `{ $el }`,不是完整的 Vue 组件实例。
127
+
128
+ | 事件 | 参数 | 说明 |
129
+ | --- | --- | --- |
130
+ | `node-click` | `(data, node, view)` | 点击节点内容时触发 |
131
+ | `node-contextmenu` | `(event: MouseEvent, data, node, view)` | 在节点上触发右键菜单时触发;需要自定义菜单时自行调用 `event.preventDefault()` |
132
+ | `current-change` | `(data, node)` | 点击节点并将其设为当前节点时触发,不代表复选框状态改变 |
133
+ | `node-expand` | `(data, node, view)` | 用户触发展开时通知;懒加载时不要将此事件等同于数据加载完成 |
134
+ | `node-collapse` | `(data, node, view)` | 用户触发收起时通知 |
135
+ | `check-change` | `(data, checked: boolean, indeterminate: boolean)` | 某节点选中或半选状态变化时触发,父子联动可能触发多次 |
136
+ | `check` | `(data, state)` | 通过复选框或点击节点触发勾选处理后通知,`state` 结构见下方 |
137
+ | `node-drag-start` | `(draggingNode, event: DragEvent)` | 节点开始拖动 |
138
+ | `node-drag-enter` | `(draggingNode, dropNode, event: DragEvent)` | 进入一个允许放置的目标节点 |
139
+ | `node-drag-leave` | `(draggingNode, dropNode, event: DragEvent)` | 离开上一个允许放置的目标节点 |
140
+ | `node-drag-over` | `(draggingNode, dropNode, event: DragEvent)` | 拖动经过目标节点并计算放置位置 |
141
+ | `node-drag-end` | `(draggingNode, dropNode / null, dropType, event: DragEvent)` | 拖动结束,`dropType` 通常为 `'before'`、`'inner'`、`'after'` 或 `'none'`,未形成目标时也可能尚未赋值 |
142
+ | `node-drop` | `(draggingNode, dropNode, dropType, event: DragEvent)` | 有有效放置位置并完成节点移动时触发 |
143
+
144
+ `check` 事件中的 `state`:
145
+
146
+ ```ts
147
+ {
148
+ checkedNodes: object[]
149
+ checkedKeys: Array<string | number>
150
+ halfCheckedNodes: object[]
151
+ halfCheckedKeys: Array<string | number>
152
+ }
153
+ ```
154
+
155
+ `allowDrop` 回调的位置值为 `prev/inner/next`,拖拽事件的位置值为 `before/inner/after`,不要混用。使用 ref 方法修改状态,不保证触发与用户点击完全相同的一组事件;如需读取结果,应调用相应的查询方法。
156
+
157
+ ### 普通模式与虚拟模式
158
+
159
+ 上表中的事件在两种模式下具有相同的参数顺序和字段结构,切换 `virtual` 时可以复用现有监听函数。
160
+
161
+ - `check-change` 的触发次数可能不同。普通模式由已挂载的节点组件监听,虚拟模式由树组件监听所有已加载的节点。虚拟模式会分别比较勾选和半选状态,仅改变半选状态也会触发事件。例如勾选目录时,虚拟模式下尚未挂载的子节点如果被联动勾选,也会发出 `check-change`。
162
+ - `view.$el` 在两种模式下都指向触发事件的节点元素。普通模式的节点元素可能包含递归渲染的子节点,虚拟模式的子节点在列表中各自占一行。需要访问子节点时,可以读取 `node.childNodes`。
163
+
164
+ ## 插槽
165
+
166
+ | 名称 | 参数 | 说明 |
167
+ | --- | --- | --- |
168
+ | `default` | `{ node, data }` | 自定义节点内容,`node` 为内部模型,`data` 为原始业务数据。设置 `renderContent` 时优先使用渲染函数 |
169
+ | `node-loading` | 无 | 虚拟行的加载图标,显示在展开箭头的位置。可用此插槽沿用调用方的加载样式 |
170
+ | `empty` | 无 | 无可见节点时的空态内容,包括空数据或过滤后所有节点不可见的情况 |
171
+
172
+ ```vue
173
+ <VueTree :data="data" node-key="id">
174
+ <template #default="{ node, data }">
175
+ <span>{{ node.label }}({{ data.id }})</span>
176
+ </template>
177
+ <template #empty>没有匹配的节点</template>
178
+ </VueTree>
179
+ ```
180
+
181
+ ## Ref 方法
182
+
183
+ 以下方法通过树组件的模板 ref 调用。`Key` 表示 `string / number`,`NodeData` 表示业务数据对象,`TreeNode` 表示内部节点模型;这些名称仅用于说明签名。
184
+
185
+ ### 查询与当前节点
186
+
187
+ | 方法 | 签名 | 说明 |
188
+ | --- | --- | --- |
189
+ | `getNode` | `(keyOrDataOrNode) => TreeNode / null` | 按 key、业务数据或已有内部模型查询节点 |
190
+ | `getNodePath` | `(keyOrData) => NodeData[]` | 返回从顶层到目标节点的数据路径,包含目标自身,需要 `nodeKey`;未找到返回 `[]` |
191
+ | `getCurrentNode` | `() => NodeData / null` | 获取当前节点的原始业务数据 |
192
+ | `getCurrentKey` | `() => Key / null` | 获取当前节点 key,需要 `nodeKey` |
193
+ | `setCurrentNode` | `(data: NodeData) => void` | 根据原始业务数据设置当前节点,需要 `nodeKey` 且节点存在 |
194
+ | `setCurrentKey` | `(key: Key / null) => void` | 根据 key 设置当前节点,需要 `nodeKey`;传 `null` 清空当前节点 |
195
+
196
+ #### 刷新和删除时的当前项
197
+
198
+ 假设设置了 `node-key="id"` 和 `highlight-current`,点击目录 `A` 下的 `A2` 后,`A2` 成为当前项。
199
+
200
+ | 操作 | 普通模式 | 虚拟模式 |
201
+ | --- | --- | --- |
202
+ | 点击 `A2` | 记录当前节点对象。 | 同时记录当前节点对象和 key。 |
203
+ | 用新数组替换 `data`,新数据仍有 `A2` | 这次点击的 key 不会自动用于恢复当前项。 | 创建同 key 的节点时,可以恢复当前项。 |
204
+ | 调用 `remove('A2')`,再添加同 key 的节点 | 删除时清空当前节点引用。 | 删除时清空当前节点引用和用于恢复的 key。重新添加的节点不会因为上次点击而自动成为当前项。 |
205
+ | 调用 `remove('A')` 删除包含 `A2` 的分支 | 递归清理分支索引。当前项的显式清理只检查传给 `remove()` 的节点本身。 | 递归清理分支索引时,也会清空分支内的当前项。 |
206
+
207
+ 虚拟模式清理旧节点索引时,会检查索引是否仍指向该旧节点。如果新 `A2` 已用同一个 key 登记,清理旧 `A2` 后,`getNode('A2')` 仍能找到新节点。上表描述的是点击产生的当前项。如果还传入了 `current-node-key`,替换 `data` 时组件也会尝试按这个属性重新设置当前项。
208
+
209
+ ### 勾选与过滤
210
+
211
+ | 方法 | 签名 | 说明 |
212
+ | --- | --- | --- |
213
+ | `getCheckedNodes` | `(leafOnly = false, includeHalfChecked = false) => NodeData[]` | 获取已勾选数据,可只返回叶子或包含半选节点 |
214
+ | `getCheckedKeys` | `(leafOnly = false) => Key[]` | 获取已勾选 key,应配置 `nodeKey` |
215
+ | `getHalfCheckedNodes` | `() => NodeData[]` | 获取半选节点数据 |
216
+ | `getHalfCheckedKeys` | `() => Key[]` | 获取半选 key,应配置 `nodeKey` |
217
+ | `setCheckedNodes` | `(nodes: NodeData[], leafOnly = false) => void` | 用业务数据数组替换勾选集合,需要 `nodeKey` |
218
+ | `setCheckedKeys` | `(keys: Key[], leafOnly = false) => void` | 用 key 数组替换勾选集合,需要 `nodeKey`;传 `[]` 清空 |
219
+ | `setChecked` | `(keyOrDataOrNode, checked: boolean, deep = false) => void` | 设置一个节点的勾选状态,`deep` 控制是否向后代传播,严格模式下不做父子联动 |
220
+ | `filter` | `(value) => void` | 重新计算节点可见性,必须先提供 `filterNodeMethod`,否则抛出错误 |
221
+
222
+ ### 修改节点与虚拟定位
223
+
224
+ | 方法 | 签名 | 说明 |
225
+ | --- | --- | --- |
226
+ | `append` | `(data: NodeData, parentKeyOrDataOrNode?) => void` | 添加子节点,省略父节点时添加到根层 |
227
+ | `remove` | `(keyOrDataOrNode) => void` | 删除节点及其子树 |
228
+ | `insertBefore` | `(data: NodeData, referenceKeyOrDataOrNode) => void` | 在已有参考节点之前插入同级节点 |
229
+ | `insertAfter` | `(data: NodeData, referenceKeyOrDataOrNode) => void` | 在已有参考节点之后插入同级节点 |
230
+ | `updateKeyChildren` | `(key: string, children: NodeData[]) => void` | 替换指定节点的子节点,需要 `nodeKey` |
231
+ | `isKeyVisible` | `(key: Key) => boolean` | 需要 `virtual` 和 `nodeKey`。判断节点所在行是否在列表的可视区间内,缓冲区内已挂载的行也可能返回 `false` |
232
+ | `scrollToKey` | `(key: Key) => Promise<void>` | 需要 `virtual` 和 `nodeKey`。滚动到目标节点所在行,由调用方先加载节点并展开祖先 |
233
+
234
+ 另外暴露了 `store`,它是内部树状态管理实例,不建议业务代码直接修改内部字段。增删和拖拽可能修改传入数据的子节点数组,若业务需要保持不可变数据,应在传入前自行复制或同步维护外部状态。
235
+
236
+ ## 勾选与过滤示例
237
+
238
+ 下面的 `TreeApi` 是示例所需的局部接口,不需要从包中导入:
239
+
240
+ ```vue
241
+ <script setup lang="ts">
242
+ import { ref } from 'vue'
243
+ import { VueTree } from '@cw-devops-fe/vue-tree-vue3'
244
+ import '@cw-devops-fe/vue-tree-vue3/style.css'
245
+
246
+ interface TreeApi {
247
+ filter(value: string): void
248
+ setCheckedKeys(keys: Array<string | number>): void
249
+ getCheckedKeys(leafOnly?: boolean): Array<string | number>
250
+ }
251
+
252
+ const treeRef = ref<TreeApi | null>(null)
253
+ const keyword = ref('')
254
+ const data = [{ id: 'root', label: '目录', children: [{ id: 'child', label: '文件' }] }]
255
+ const matches = (value: string, record: { label: string }) => !value || record.label.includes(value)
256
+
257
+ function printChecked(): void {
258
+ console.log(treeRef.value?.getCheckedKeys())
259
+ }
260
+ </script>
261
+
262
+ <template>
263
+ <input v-model="keyword" @input="treeRef?.filter(keyword)" />
264
+ <button @click="treeRef?.setCheckedKeys(['child'])">勾选文件</button>
265
+ <button @click="printChecked">读取勾选结果</button>
266
+ <VueTree
267
+ ref="treeRef"
268
+ :data="data"
269
+ node-key="id"
270
+ show-checkbox
271
+ :filter-node-method="matches"
272
+ />
273
+ </template>
274
+ ```
275
+
276
+ 过滤不会请求新数据,只对当前已加载的节点执行回调。需要清除过滤时可调用 `filter('')`,并在回调中将空查询处理为 `true`。
277
+
278
+ ## 懒加载示例
279
+
280
+ `load` 首次会接收到 `level === 0` 的内部根节点,用于取得第一层数据。非叶子节点首次展开时,再加载其子节点。业务接口失败时应自行处理错误,并决定本次如何结束;当前没有独立的 `reject` 回调。
281
+
282
+ ```vue
283
+ <script setup lang="ts">
284
+ import { VueTree } from '@cw-devops-fe/vue-tree-vue3'
285
+ import '@cw-devops-fe/vue-tree-vue3/style.css'
286
+
287
+ interface Row {
288
+ id: string
289
+ label: string
290
+ leaf?: boolean
291
+ }
292
+
293
+ function load(node: { level: number; data: Row }, resolve: (children: Row[]) => void): void {
294
+ if (node.level === 0) {
295
+ resolve([{ id: 'folder', label: '目录' }])
296
+ } else {
297
+ resolve([{ id: `${node.data.id}-file`, label: '文件', leaf: true }])
298
+ }
299
+ }
300
+ </script>
301
+
302
+ <template>
303
+ <VueTree node-key="id" lazy :load="load" :props="{ isLeaf: 'leaf' }" />
304
+ </template>
305
+ ```
306
+
307
+ 对于真正的异步接口,也必须在结果取得后调用 `resolve(rows)`;仅从 `load` 返回数组或 Promise 不会自动提交子节点。调用 `resolve([])` 会将该节点标记为已经加载的叶子节点。
308
+
309
+ ## 虚拟滚动与使用限制
310
+
311
+ ### 开启方式与搭配参数
312
+
313
+ 省略 `virtual` 或在创建时传入 `:virtual="false"`,组件沿用 `VueTreeNode` 的递归渲染。普通模式沿用原有的展开、懒加载、勾选和键盘处理。直接写 `virtual` 可启用全层级虚拟滚动。根节点的初始化时机取决于初始模式,需要切换模式时可重新挂载组件。
314
+
315
+ 旧版已开启 `virtual` 的页面从仅虚拟化第一层升级为各层共用一个滚动容器。虚拟行在 DOM 中是同级元素,业务若通过嵌套 DOM 查找子节点,应改用 `getNode(key).childNodes`。事件参数沿用原有结构,虚拟模式的事件次数与当前项恢复行为见[事件说明](#普通模式与虚拟模式)和[当前项说明](#刷新和删除时的当前项)。
316
+
317
+ | 配置 | 虚拟模式下的行为 |
318
+ | --- | --- |
319
+ | 容器高度 | 给 `VueTree` 设置可计算的 CSS 高度,例如 `style="height: 400px"`。用 `height: 100%` 时,上层容器也需要提供可计算的高度。 |
320
+ | `nodeKey` | 仅开启虚拟滚动可以省略。使用 `scrollToKey`、`isKeyVisible` 或 `defaultExpandedKeys` 时需要配置,且整个树内的 key 应唯一并保持稳定。 |
321
+ | `defaultExpandedKeys` / `autoExpandParent` | 前者指定要默认展开的节点。后者默认 `true`,同时展开这些节点已存在的祖先。使用懒加载时,由业务按路径加载尚未创建的节点。 |
322
+ | `lazy` / `load` | 可以和 `virtual` 一起开启。首次 `load` 用于取得第一层节点,展开尚未加载的目录时再加载子项。加载过程中收起目录,返回的子项会保存到模型,目录仍保持收起。 |
323
+ | `renderAfterExpand` | 用于普通树。虚拟模式下,展开状态决定子项是否进入行列表,视口和缓冲区决定哪些行实际挂载。 |
324
+
325
+ VueTree 向内部 VirtualList 传入稳定行键、`28px` 的预估行高和 `immediateScrollUpdate`。挂载后使用实测高度,插槽内容可以换行或使用不同行高。行键优先使用 `nodeKey` 指定的业务值,缺少业务键时使用内部节点 ID。展开上方目录只会改变后续行的下标,仍可按相同的键复用这些行。`itemKey`、`estimatedItemSize`、`estimatedItemSizes`、`bufferScale` 和 `immediateScrollUpdate` 由 VirtualList 管理。
326
+
327
+ `deferContent` 默认关闭。开启后先显示文件名和复选框,再分帧补上自定义内容。行首帧就需要完整的图标、菜单或自定义布局时,使用默认设置。业务自行请求目录数据时,可以把请求状态通过 `loadingKey` 传给虚拟行,用 `node-loading` 插槽提供转圈图标,等待期间图标占用展开箭头的位置。
328
+
329
+ ### 定位深层节点
330
+
331
+ 下面的例子默认展开 `docs` 目录。`guide` 排在 40 个文件之后,点击按钮可以按业务 key 滚动到它。
332
+
333
+ ```vue
334
+ <script setup lang="ts">
335
+ import { ref } from 'vue'
336
+ import { VueTree } from '@cw-devops-fe/vue-tree-vue3'
337
+ import '@cw-devops-fe/vue-tree-vue3/style.css'
338
+ import '@cw-devops-fe/virtual-list-vue3/style.css'
339
+
340
+ interface TreeApi {
341
+ scrollToKey(key: string | number): Promise<void>
342
+ }
343
+
344
+ const treeRef = ref<TreeApi | null>(null)
345
+ const data = [{
346
+ id: 'docs',
347
+ label: '文档',
348
+ children: [
349
+ ...Array.from({ length: 40 }, (_, index) => ({
350
+ id: `file-${index + 1}`,
351
+ label: `文件 ${index + 1}`,
352
+ })),
353
+ { id: 'guide', label: '使用指南' },
354
+ ],
355
+ }]
356
+ </script>
357
+
358
+ <template>
359
+ <button @click="treeRef?.scrollToKey('guide')">定位到使用指南</button>
360
+ <VueTree
361
+ ref="treeRef"
362
+ :data="data"
363
+ node-key="id"
364
+ :default-expanded-keys="['docs']"
365
+ virtual
366
+ style="height: 400px"
367
+ />
368
+ </template>
369
+ ```
370
+
371
+ 若目标尚未加载、祖先已收起,或目标及其祖先被过滤隐藏,就没有可定位的行。此时 `scrollToKey()` 直接结束,`isKeyVisible()` 返回 `false`。业务需要先加载目标并打开祖先路径,再调用定位方法。`scrollToKey()` 等待本轮 Vue 更新后读取最新行号,把它换算成内部列表容器的 `scrollTop`。目标行与树内滚动条一起移动,目标已在视口内时也可以先用 `isKeyVisible()` 判断,再决定是否滚动。
372
+
373
+ ### 渲染范围
374
+
375
+ 例如展开目录 `A` 后,虚拟列表的行顺序从 `[A, B]` 变成 `[A, A1, A2, B]`。列表只挂载视口及附近缓冲区的行。已加载的节点、展开和勾选状态仍保存在树模型中。相关数据变动时,树级监听会扫描已加载节点,同步子项和发出勾选事件。
376
+
377
+ - 当前节点与勾选节点是独立状态,不支持用单个 `v-model` 统一控制。勾选结果通过事件或 ref 方法获取。
378
+ - `defaultExpandAll`、字段映射、懒加载及部分策略用于初始化内部模型,不能假设运行中替换任意配置都会自动完整重建树。
379
+ - `nodeKey` 一经确定不应在同一树实例上随意替换。外部数据替换建议使用新的数组引用。
380
+ - 这不是 Element UI 的直接替代包,方法与行为以本页和当前实现为准,不能直接套用其他树组件的全部 API。
381
+
382
+ VirtualList 通过 `workspace:*` 关联,在发布安装包时会转为实际版本依赖。仓库内执行 VueTree 的构建命令会先构建 VirtualList。
383
+
384
+ ## 开发与出包
385
+
386
+ 构建配置、类型配置及开发依赖均在本包目录维护。先在 workspace 根目录执行 `pnpm install`,再在本目录运行以下命令:
387
+
388
+ ```sh
389
+ pnpm dev
390
+ pnpm typecheck
391
+ pnpm build
392
+ pnpm check
393
+ pnpm pack
394
+ ```
395
+
396
+ | 命令 | 作用 |
397
+ | --- | --- |
398
+ | `pnpm dev` | 自动构建工作台和 VirtualList,再启动 Vite 预览本包源码 |
399
+ | `pnpm build:deps` | 只构建依赖的 VirtualList |
400
+ | `pnpm typecheck` | 检查源码和 Vite 配置的类型,不生成产物;首次运行需先执行 `build:deps` |
401
+ | `pnpm build` | 自动构建 VirtualList,再将本包 ES Module、CommonJS、CSS 和类型声明输出到 `dist/` |
402
+ | `pnpm check` | 完成依赖构建、本包构建和类型检查 |
403
+ | `pnpm pack` | 自动执行 `check`,通过后在当前目录生成 `.tgz` |
404
+
405
+ 打包时会将 VirtualList 的 `workspace:*` 转为实际版本。发布本包前需先发布对应版本的 VirtualList。
406
+
407
+ 调试页默认地址为 `http://127.0.0.1:3200/__component-workbench`。同时调试多个包时,可以使用 `pnpm dev --port 3201` 指定其他端口。
408
+
409
+ 在 workspace 根目录可以只打包本包:`pnpm --dir vue-tree-vue3 pack`。编译产物位于本包的 `dist/`,生成的 `.tgz` 位于本包目录。打包会构建依赖,但只生成 VueTree 的安装包,不会自动发布或创建 Git 提交。
410
+
411
+ 工作台扫描 `src/**/*.vue`,包含 VueTree、VueTreeNode 和 CollapseTransition。VueTreeNode 依赖 VueTree 提供的上下文及带有方法的节点模型;扫描发现不代表普通 JSON props 足以独立挂载,工作台目前尚未提供该内部组件的上下文适配。
412
+
413
+ 根目录的 `pnpm pack:public` 会将本包压缩包输出到根 `artifacts/`。发布时先发布 `@cw-devops-fe/virtual-list-vue3` 的对应版本,再发布本包。完整 npmjs 发布步骤见[仓库发布文档](../README.MD#手动发布)。
@@ -0,0 +1,4 @@
1
+ declare const _default: import('vue').DefineComponent<{}, () => import('vue').VNode<import('vue').RendererNode, import('vue').RendererElement, {
2
+ [key: string]: any;
3
+ }>, {}, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {}, string, import('vue').PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import('vue').ComponentProvideOptions, true, {}, any>;
4
+ export default _default;
@@ -0,0 +1,206 @@
1
+ import { default as Node } from './model/node';
2
+ type __VLS_Props = {
3
+ rows: Node[];
4
+ getRowKey: (node: Node) => string | number;
5
+ showCheckbox?: boolean;
6
+ renderContent?: Function;
7
+ deferContent?: boolean;
8
+ };
9
+ declare function __VLS_template(): {
10
+ attrs: Partial<{}>;
11
+ slots: {
12
+ 'node-loading'?(_: {}): any;
13
+ };
14
+ refs: {
15
+ list: ({
16
+ $: import('vue').ComponentInternalInstance;
17
+ $data: {};
18
+ $props: {
19
+ readonly listData?: unknown[] | undefined;
20
+ readonly itemKey?: ((item: unknown) => string | number) | undefined;
21
+ readonly immediateScrollUpdate?: boolean | undefined;
22
+ readonly estimatedItemSize: number;
23
+ readonly estimatedItemSizes?: number[] | undefined;
24
+ readonly bufferScale?: number | undefined;
25
+ readonly height?: string | undefined;
26
+ readonly showLoading?: boolean | undefined;
27
+ readonly "onLoading-change"?: ((value: boolean) => any) | undefined | undefined;
28
+ } & import('vue').VNodeProps & import('vue').AllowedComponentProps & import('vue').ComponentCustomProps;
29
+ $attrs: import('vue').Attrs;
30
+ $refs: {
31
+ [x: string]: unknown;
32
+ } & {
33
+ listRef: HTMLDivElement;
34
+ phantomRef: HTMLDivElement;
35
+ contentRef: HTMLDivElement;
36
+ };
37
+ $slots: Readonly<{
38
+ [name: string]: import('vue').Slot<any> | undefined;
39
+ }>;
40
+ $root: import('vue').ComponentPublicInstance | null;
41
+ $parent: import('vue').ComponentPublicInstance | null;
42
+ $host: Element | null;
43
+ $emit: (event: "loading-change", value: boolean) => void;
44
+ $el: HTMLDivElement;
45
+ $options: import('vue').ComponentOptionsBase<Readonly<import('@cw-devops-fe/virtual-list-vue3').VirtualListProps> & Readonly<{
46
+ "onLoading-change"?: ((value: boolean) => any) | undefined;
47
+ }>, {
48
+ isIndexVisible: (index: number) => boolean;
49
+ scrollToIndex: (index?: number) => Promise<void>;
50
+ }, {}, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {
51
+ "loading-change": (value: boolean) => any;
52
+ }, string, {
53
+ listData: unknown[];
54
+ immediateScrollUpdate: boolean;
55
+ estimatedItemSizes: number[];
56
+ bufferScale: number;
57
+ height: string;
58
+ showLoading: boolean;
59
+ }, {}, string, {}, import('vue').GlobalComponents, import('vue').GlobalDirectives, string, import('vue').ComponentProvideOptions> & {
60
+ beforeCreate?: (() => void) | (() => void)[];
61
+ created?: (() => void) | (() => void)[];
62
+ beforeMount?: (() => void) | (() => void)[];
63
+ mounted?: (() => void) | (() => void)[];
64
+ beforeUpdate?: (() => void) | (() => void)[];
65
+ updated?: (() => void) | (() => void)[];
66
+ activated?: (() => void) | (() => void)[];
67
+ deactivated?: (() => void) | (() => void)[];
68
+ beforeDestroy?: (() => void) | (() => void)[];
69
+ beforeUnmount?: (() => void) | (() => void)[];
70
+ destroyed?: (() => void) | (() => void)[];
71
+ unmounted?: (() => void) | (() => void)[];
72
+ renderTracked?: ((e: import('vue').DebuggerEvent) => void) | ((e: import('vue').DebuggerEvent) => void)[];
73
+ renderTriggered?: ((e: import('vue').DebuggerEvent) => void) | ((e: import('vue').DebuggerEvent) => void)[];
74
+ errorCaptured?: ((err: unknown, instance: import('vue').ComponentPublicInstance | null, info: string) => boolean | void) | ((err: unknown, instance: import('vue').ComponentPublicInstance | null, info: string) => boolean | void)[];
75
+ };
76
+ $forceUpdate: () => void;
77
+ $nextTick: typeof import('vue').nextTick;
78
+ $watch<T extends string | ((...args: any) => any)>(source: T, cb: T extends (...args: any) => infer R ? (...args: [R, R, import('@vue/reactivity').OnCleanup]) => any : (...args: [any, any, import('@vue/reactivity').OnCleanup]) => any, options?: import('vue').WatchOptions): import('vue').WatchStopHandle;
79
+ } & Readonly<{
80
+ listData: unknown[];
81
+ immediateScrollUpdate: boolean;
82
+ estimatedItemSizes: number[];
83
+ bufferScale: number;
84
+ height: string;
85
+ showLoading: boolean;
86
+ }> & Omit<Readonly<import('@cw-devops-fe/virtual-list-vue3').VirtualListProps> & Readonly<{
87
+ "onLoading-change"?: ((value: boolean) => any) | undefined;
88
+ }>, "listData" | "immediateScrollUpdate" | "estimatedItemSizes" | "bufferScale" | "height" | "showLoading" | "isIndexVisible" | "scrollToIndex"> & {
89
+ isIndexVisible: (index: number) => boolean;
90
+ scrollToIndex: (index?: number) => Promise<void>;
91
+ } & {} & import('vue').ComponentCustomProperties & {} & {
92
+ $slots: {
93
+ loading?(_: {}): any;
94
+ default?(_: {
95
+ item: {
96
+ item: Record<string, unknown>;
97
+ index: string;
98
+ top: number;
99
+ };
100
+ }): any;
101
+ };
102
+ }) | null;
103
+ };
104
+ rootEl: any;
105
+ };
106
+ type __VLS_TemplateResult = ReturnType<typeof __VLS_template>;
107
+ declare const __VLS_component: import('vue').DefineComponent<__VLS_Props, {
108
+ isIndexVisible: (index: number) => boolean;
109
+ scrollToIndex: (index: number) => Promise<void> | undefined;
110
+ }, {}, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {}, string, import('vue').PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import('vue').ComponentProvideOptions, false, {
111
+ list: ({
112
+ $: import('vue').ComponentInternalInstance;
113
+ $data: {};
114
+ $props: {
115
+ readonly listData?: unknown[] | undefined;
116
+ readonly itemKey?: ((item: unknown) => string | number) | undefined;
117
+ readonly immediateScrollUpdate?: boolean | undefined;
118
+ readonly estimatedItemSize: number;
119
+ readonly estimatedItemSizes?: number[] | undefined;
120
+ readonly bufferScale?: number | undefined;
121
+ readonly height?: string | undefined;
122
+ readonly showLoading?: boolean | undefined;
123
+ readonly "onLoading-change"?: ((value: boolean) => any) | undefined | undefined;
124
+ } & import('vue').VNodeProps & import('vue').AllowedComponentProps & import('vue').ComponentCustomProps;
125
+ $attrs: import('vue').Attrs;
126
+ $refs: {
127
+ [x: string]: unknown;
128
+ } & {
129
+ listRef: HTMLDivElement;
130
+ phantomRef: HTMLDivElement;
131
+ contentRef: HTMLDivElement;
132
+ };
133
+ $slots: Readonly<{
134
+ [name: string]: import('vue').Slot<any> | undefined;
135
+ }>;
136
+ $root: import('vue').ComponentPublicInstance | null;
137
+ $parent: import('vue').ComponentPublicInstance | null;
138
+ $host: Element | null;
139
+ $emit: (event: "loading-change", value: boolean) => void;
140
+ $el: HTMLDivElement;
141
+ $options: import('vue').ComponentOptionsBase<Readonly<import('@cw-devops-fe/virtual-list-vue3').VirtualListProps> & Readonly<{
142
+ "onLoading-change"?: ((value: boolean) => any) | undefined;
143
+ }>, {
144
+ isIndexVisible: (index: number) => boolean;
145
+ scrollToIndex: (index?: number) => Promise<void>;
146
+ }, {}, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {
147
+ "loading-change": (value: boolean) => any;
148
+ }, string, {
149
+ listData: unknown[];
150
+ immediateScrollUpdate: boolean;
151
+ estimatedItemSizes: number[];
152
+ bufferScale: number;
153
+ height: string;
154
+ showLoading: boolean;
155
+ }, {}, string, {}, import('vue').GlobalComponents, import('vue').GlobalDirectives, string, import('vue').ComponentProvideOptions> & {
156
+ beforeCreate?: (() => void) | (() => void)[];
157
+ created?: (() => void) | (() => void)[];
158
+ beforeMount?: (() => void) | (() => void)[];
159
+ mounted?: (() => void) | (() => void)[];
160
+ beforeUpdate?: (() => void) | (() => void)[];
161
+ updated?: (() => void) | (() => void)[];
162
+ activated?: (() => void) | (() => void)[];
163
+ deactivated?: (() => void) | (() => void)[];
164
+ beforeDestroy?: (() => void) | (() => void)[];
165
+ beforeUnmount?: (() => void) | (() => void)[];
166
+ destroyed?: (() => void) | (() => void)[];
167
+ unmounted?: (() => void) | (() => void)[];
168
+ renderTracked?: ((e: import('vue').DebuggerEvent) => void) | ((e: import('vue').DebuggerEvent) => void)[];
169
+ renderTriggered?: ((e: import('vue').DebuggerEvent) => void) | ((e: import('vue').DebuggerEvent) => void)[];
170
+ errorCaptured?: ((err: unknown, instance: import('vue').ComponentPublicInstance | null, info: string) => boolean | void) | ((err: unknown, instance: import('vue').ComponentPublicInstance | null, info: string) => boolean | void)[];
171
+ };
172
+ $forceUpdate: () => void;
173
+ $nextTick: typeof import('vue').nextTick;
174
+ $watch<T extends string | ((...args: any) => any)>(source: T, cb: T extends (...args: any) => infer R ? (...args: [R, R, import('@vue/reactivity').OnCleanup]) => any : (...args: [any, any, import('@vue/reactivity').OnCleanup]) => any, options?: import('vue').WatchOptions): import('vue').WatchStopHandle;
175
+ } & Readonly<{
176
+ listData: unknown[];
177
+ immediateScrollUpdate: boolean;
178
+ estimatedItemSizes: number[];
179
+ bufferScale: number;
180
+ height: string;
181
+ showLoading: boolean;
182
+ }> & Omit<Readonly<import('@cw-devops-fe/virtual-list-vue3').VirtualListProps> & Readonly<{
183
+ "onLoading-change"?: ((value: boolean) => any) | undefined;
184
+ }>, "listData" | "immediateScrollUpdate" | "estimatedItemSizes" | "bufferScale" | "height" | "showLoading" | "isIndexVisible" | "scrollToIndex"> & {
185
+ isIndexVisible: (index: number) => boolean;
186
+ scrollToIndex: (index?: number) => Promise<void>;
187
+ } & {} & import('vue').ComponentCustomProperties & {} & {
188
+ $slots: {
189
+ loading?(_: {}): any;
190
+ default?(_: {
191
+ item: {
192
+ item: Record<string, unknown>;
193
+ index: string;
194
+ top: number;
195
+ };
196
+ }): any;
197
+ };
198
+ }) | null;
199
+ }, any>;
200
+ declare const _default: __VLS_WithTemplateSlots<typeof __VLS_component, __VLS_TemplateResult["slots"]>;
201
+ export default _default;
202
+ type __VLS_WithTemplateSlots<T, S> = T & {
203
+ new (): {
204
+ $slots: S;
205
+ };
206
+ };