@handaotech-design/bom 0.0.53 → 0.0.54

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
@@ -51,6 +51,8 @@ app.use(HdBomWorkbench);
51
51
  |------------------------------|---------------|---------------------------------------------|
52
52
  | updateTreeWithPreservedState | 更新树数据,并保持树状态 | (updateDataFn: () => Promise<void>) => void |
53
53
  | getParentKeys | 获取节点所有父节点的key | (key: string): string[] | undefined |
54
+ | getParentNodes | 获取节点所有父节点 | (key: string): TreeNodeWithMeta[] |
55
+ | getPath | 获取从根到当前节点的完整路径(包含自身) | (key: string): TreeNodeWithMeta[] |
54
56
  | getDepth | 获取节点在树中的深度 | getDepth(key: string) | number |
55
57
 
56
58
  具体交互参考 HdBomTree 组件
@@ -101,17 +101,18 @@ const getParentKeys = (key: string): string[] | undefined => {
101
101
  return treeNode?.parentKeys || []
102
102
  }
103
103
 
104
- const selectFirstSelectableNode = (preserveExpanded = false) => {
105
- if (!firstSelectableKey.value) {
106
- return
107
- }
108
- nextTick(() => {
104
+ const selectFirstSelectableNode = async (preserveExpanded = false) => {
105
+ await nextTick()
106
+ if (firstSelectableKey.value) {
109
107
  const parentKeys = getParentKeys(firstSelectableKey.value!) as string[]
110
108
  expandedKeys.value = preserveExpanded
111
- ? Array.from(new Set([...expandedKeys.value, ...parentKeys])) // 取并集
109
+ ? Array.from(new Set([...expandedKeys.value, ...parentKeys]))
112
110
  : parentKeys
113
111
  selectedKeys.value = [firstSelectableKey.value!]
114
- })
112
+ }
113
+ else {
114
+ await expandToFirstSelectable(treeData.value)
115
+ }
115
116
  }
116
117
 
117
118
  const canLoadMoreRoot = computed(() => currentRootSize.value < filteredData.value.length)
@@ -322,11 +323,36 @@ async function _loadData(node: EventDataNode) {
322
323
  })
323
324
 
324
325
  node.dataRef!.children = treeData
326
+
327
+ await expandToFirstSelectable(treeData)
325
328
  }
326
329
  else {
327
330
  node.isLeaf = true
328
331
  }
329
332
  }
333
+
334
+ async function expandToFirstSelectable(nodes: DataNode[]) {
335
+ if (_.isEmpty(nodes)) {
336
+ return
337
+ }
338
+ for (const child of nodes) {
339
+ if ((child as any).selectable) {
340
+ selectedKeys.value = [child.key as string]
341
+ const parentKeys = getParentKeys(child.key as string) || []
342
+ expandedKeys.value = Array.from(new Set([...expandedKeys.value, ...parentKeys]))
343
+ return
344
+ }
345
+ if (!(child as any).isLeaf && !(child as any).children?.length) {
346
+ expandedKeys.value = Array.from(new Set([...expandedKeys.value, child.key as string | number]))
347
+ // _loadData 会被 ant-tree 的 expand 自动触发
348
+ return
349
+ }
350
+ if ((child as any).children?.length) {
351
+ await expandToFirstSelectable((child as any).children)
352
+ return
353
+ }
354
+ }
355
+ }
330
356
  </script>
331
357
 
332
358
  <template>
@@ -101,17 +101,18 @@ const getParentKeys = (key: string): string[] | undefined => {
101
101
  return treeNode?.parentKeys || []
102
102
  }
103
103
 
104
- const selectFirstSelectableNode = (preserveExpanded = false) => {
105
- if (!firstSelectableKey.value) {
106
- return
107
- }
108
- nextTick(() => {
104
+ const selectFirstSelectableNode = async (preserveExpanded = false) => {
105
+ await nextTick()
106
+ if (firstSelectableKey.value) {
109
107
  const parentKeys = getParentKeys(firstSelectableKey.value!) as string[]
110
108
  expandedKeys.value = preserveExpanded
111
- ? Array.from(new Set([...expandedKeys.value, ...parentKeys])) // 取并集
109
+ ? Array.from(new Set([...expandedKeys.value, ...parentKeys]))
112
110
  : parentKeys
113
111
  selectedKeys.value = [firstSelectableKey.value!]
114
- })
112
+ }
113
+ else {
114
+ await expandToFirstSelectable(treeData.value)
115
+ }
115
116
  }
116
117
 
117
118
  const canLoadMoreRoot = computed(() => currentRootSize.value < filteredData.value.length)
@@ -322,11 +323,36 @@ async function _loadData(node: EventDataNode) {
322
323
  })
323
324
 
324
325
  node.dataRef!.children = treeData
326
+
327
+ await expandToFirstSelectable(treeData)
325
328
  }
326
329
  else {
327
330
  node.isLeaf = true
328
331
  }
329
332
  }
333
+
334
+ async function expandToFirstSelectable(nodes: DataNode[]) {
335
+ if (_.isEmpty(nodes)) {
336
+ return
337
+ }
338
+ for (const child of nodes) {
339
+ if ((child as any).selectable) {
340
+ selectedKeys.value = [child.key as string]
341
+ const parentKeys = getParentKeys(child.key as string) || []
342
+ expandedKeys.value = Array.from(new Set([...expandedKeys.value, ...parentKeys]))
343
+ return
344
+ }
345
+ if (!(child as any).isLeaf && !(child as any).children?.length) {
346
+ expandedKeys.value = Array.from(new Set([...expandedKeys.value, child.key as string | number]))
347
+ // _loadData 会被 ant-tree 的 expand 自动触发
348
+ return
349
+ }
350
+ if ((child as any).children?.length) {
351
+ await expandToFirstSelectable((child as any).children)
352
+ return
353
+ }
354
+ }
355
+ }
330
356
  </script>
331
357
 
332
358
  <template>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@handaotech-design/bom",
3
- "version": "0.0.53",
3
+ "version": "0.0.54",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "exports": {