@veltra/desktop 1.15.8 → 1.16.1

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.
Files changed (38) hide show
  1. package/dist/components/dialog/use-maximum.js +1 -1
  2. package/dist/components/dialog/use-maximum.js.map +1 -1
  3. package/dist/components/multi-tree-select/multi-tree-select.js +44 -8
  4. package/dist/components/multi-tree-select/multi-tree-select.js.map +1 -1
  5. package/dist/components/scroll/style2.css +1 -1
  6. package/dist/components/tag/style2.css +1 -1
  7. package/dist/components/tip/style2.css +2 -2
  8. package/dist/components/tree/forest.js +47 -0
  9. package/dist/components/tree/forest.js.map +1 -0
  10. package/dist/components/tree/tree-node.js +3 -3
  11. package/dist/components/tree/tree-node.js.map +1 -1
  12. package/dist/components/tree/tree-node2.js +9 -0
  13. package/dist/components/tree/tree-node2.js.map +1 -1
  14. package/dist/components/tree/tree.js +1 -0
  15. package/dist/components/tree/tree.js.map +1 -1
  16. package/dist/components/tree/use-check.js +17 -1
  17. package/dist/components/tree/use-check.js.map +1 -1
  18. package/dist/components/tree/use-tree-nodes.js +6 -33
  19. package/dist/components/tree/use-tree-nodes.js.map +1 -1
  20. package/dist/components/tree-select/tree-select.js +1 -0
  21. package/dist/components/tree-select/tree-select.js.map +1 -1
  22. package/dist/types/multi-tree-select.d.ts +8 -1
  23. package/dist/types/tree.d.ts +9 -0
  24. package/package.json +1 -1
  25. package/src/components/dialog/use-maximum.ts +1 -1
  26. package/src/components/multi-tree-select/__test__/show-checkbox.test.ts +216 -0
  27. package/src/components/multi-tree-select/multi-tree-select.vue +44 -9
  28. package/src/components/scroll/style.scss +2 -1
  29. package/src/components/tag/style.scss +1 -1
  30. package/src/components/tip/style.scss +2 -2
  31. package/src/components/tree/__test__/show-checkbox.test.ts +153 -0
  32. package/src/components/tree/forest.ts +63 -0
  33. package/src/components/tree/tree-node.ts +11 -0
  34. package/src/components/tree/tree-node.vue +4 -2
  35. package/src/components/tree/use-check.ts +20 -3
  36. package/src/components/tree/use-tree-nodes.ts +10 -41
  37. package/src/types/multi-tree-select.ts +9 -1
  38. package/src/types/tree.ts +9 -0
@@ -0,0 +1,153 @@
1
+ // @vitest-environment happy-dom
2
+
3
+ import { describe, expect, it } from 'vite-plus/test'
4
+ import { createApp, h, nextTick, ref, type Ref } from 'vue'
5
+
6
+ import UTree from '../tree.vue'
7
+
8
+ const data = [
9
+ {
10
+ label: '亚洲',
11
+ value: 'asia',
12
+ children: [
13
+ { label: '中国', value: 'cn' },
14
+ { label: '日本', value: 'jp' }
15
+ ]
16
+ }
17
+ ]
18
+
19
+ function mountTree(props: Record<string, unknown> = {}) {
20
+ const host = document.createElement('div')
21
+ document.body.appendChild(host)
22
+
23
+ const checked = ref(props.checked ?? []) as Ref<(string | number)[]>
24
+ const emitted: (string | number)[][] = []
25
+
26
+ const app = createApp({
27
+ render() {
28
+ return h(UTree, {
29
+ data,
30
+ checkable: true,
31
+ expandAll: true,
32
+ ...props,
33
+ checked: checked.value,
34
+ 'onUpdate:checked': (value: (string | number)[]) => {
35
+ checked.value = value
36
+ emitted.push(value)
37
+ }
38
+ })
39
+ }
40
+ })
41
+
42
+ app.mount(host)
43
+
44
+ return {
45
+ host,
46
+ checked,
47
+ emitted,
48
+ unmount() {
49
+ app.unmount()
50
+ host.remove()
51
+ }
52
+ }
53
+ }
54
+
55
+ function queryNodeContents() {
56
+ return [...document.body.querySelectorAll<HTMLElement>('.u-tree__node-content')]
57
+ }
58
+
59
+ function queryCheckboxes() {
60
+ return [...document.body.querySelectorAll<HTMLInputElement>('.u-tree .u-checkbox__native')]
61
+ }
62
+
63
+ function findNode(label: string) {
64
+ const node = queryNodeContents().find((el) => el.textContent?.includes(label))
65
+ expect(node, `tree node "${label}" not found`).toBeTruthy()
66
+ return node!
67
+ }
68
+
69
+ describe('UTree showCheckbox', () => {
70
+ it('renders every checkbox when showCheckbox is not provided', async () => {
71
+ const { unmount } = mountTree()
72
+
73
+ try {
74
+ await nextTick()
75
+ expect(queryCheckboxes()).toHaveLength(3)
76
+ } finally {
77
+ unmount()
78
+ }
79
+ })
80
+
81
+ it('skips the checkbox of nodes whose showCheckbox returns false', async () => {
82
+ const { unmount } = mountTree({
83
+ showCheckbox: (_item: Record<string, any>, node: any) => !node.children?.length
84
+ })
85
+
86
+ try {
87
+ await nextTick()
88
+ // 只有两个叶子节点渲染复选框
89
+ expect(queryCheckboxes()).toHaveLength(2)
90
+ // 展开图标与节点文本保持不变
91
+ expect(findNode('亚洲')).toBeTruthy()
92
+ expect(document.body.querySelectorAll('.u-tree__expand-icon').length).toBe(1)
93
+ } finally {
94
+ unmount()
95
+ }
96
+ })
97
+
98
+ it('does not check a hidden node when checkOnClickNode clicks it', async () => {
99
+ const { checked, unmount } = mountTree({
100
+ showCheckbox: (_item: Record<string, any>, node: any) => !node.children?.length
101
+ })
102
+
103
+ try {
104
+ await nextTick()
105
+ findNode('亚洲').dispatchEvent(new MouseEvent('click', { bubbles: true }))
106
+ await nextTick()
107
+ expect(checked.value).toEqual([])
108
+
109
+ // 叶子仍可单独勾选
110
+ findNode('中国').dispatchEvent(new MouseEvent('click', { bubbles: true }))
111
+ await nextTick()
112
+ expect(checked.value).toEqual(['cn'])
113
+ } finally {
114
+ unmount()
115
+ }
116
+ })
117
+
118
+ it('keeps the parent out of checked when all its leaves are checked', async () => {
119
+ const { checked, unmount } = mountTree({
120
+ showCheckbox: (_item: Record<string, any>, node: any) => !node.children?.length
121
+ })
122
+
123
+ try {
124
+ await nextTick()
125
+ findNode('中国').dispatchEvent(new MouseEvent('click', { bubbles: true }))
126
+ await nextTick()
127
+ findNode('日本').dispatchEvent(new MouseEvent('click', { bubbles: true }))
128
+ await nextTick()
129
+
130
+ expect(checked.value).toEqual(['cn', 'jp'])
131
+ } finally {
132
+ unmount()
133
+ }
134
+ })
135
+
136
+ it('does not treat hidden nodes in checked as selected values', async () => {
137
+ const { checked, unmount } = mountTree({
138
+ checked: ['asia', 'cn'],
139
+ showCheckbox: (_item: Record<string, any>, node: any) => !node.children?.length
140
+ })
141
+
142
+ try {
143
+ await nextTick()
144
+
145
+ // 隐藏复选框的节点不进入勾选数据:触发一次事件即可观察到 update:checked 内容
146
+ findNode('日本').dispatchEvent(new MouseEvent('click', { bubbles: true }))
147
+ await nextTick()
148
+ expect(checked.value).toEqual(['cn', 'jp'])
149
+ } finally {
150
+ unmount()
151
+ }
152
+ })
153
+ })
@@ -0,0 +1,63 @@
1
+ import { Forest } from '@cat-kit/core'
2
+ import { fieldKey } from '@veltra/utils'
3
+
4
+ import type { TreeProps } from '../../types'
5
+ import { TreeNode } from './tree-node'
6
+
7
+ /** 构建森林所需的节点级判定输入 */
8
+ export type TreeForestOptions = Pick<
9
+ TreeProps,
10
+ 'data' | 'childrenKey' | 'valueKey' | 'labelKey' | 'expandAll' | 'disabledNode' | 'showCheckbox'
11
+ >
12
+
13
+ /**
14
+ * 按 props 构建森林,并在整棵树挂载完成后完成节点级判定。
15
+ * @description Forest 的 createNode 返回时 children 尚未挂载,因此 `disabledNode` /
16
+ * `showCheckbox` 必须整棵树构建完成后再调用,才能安全访问 `node.children` / `node.isLeaf`。
17
+ * 树组件与 UMultiTreeSelect 共用本函数,保证两侧对「复选框是否可见」判定一致。
18
+ */
19
+ export function createTreeForest(options: TreeForestOptions): Forest<Record<string, unknown>, any> {
20
+ const { expandAll = false } = options
21
+ const childrenKey = options.childrenKey ?? 'children'
22
+ const valueKey = fieldKey(options.valueKey, 'value')
23
+ const labelKey = fieldKey(options.labelKey, 'label')
24
+
25
+ const forest = new Forest<Record<string, unknown>, any>({
26
+ data: (options.data ?? []) as Record<string, unknown>[],
27
+ childrenKey,
28
+ createNode: (data, index, depth, _f, parent) => {
29
+ const node = new TreeNode({
30
+ data: data as Record<string, any>,
31
+ index,
32
+ depth,
33
+ parent: parent as TreeNode | undefined,
34
+ valueKey,
35
+ labelKey
36
+ })
37
+ node.expanded = expandAll
38
+ return node
39
+ }
40
+ })
41
+
42
+ const { disabledNode, showCheckbox } = options
43
+
44
+ if (disabledNode) {
45
+ forest.dfs((node) => {
46
+ const tn = node as unknown as TreeNode
47
+ if (!tn.data) return
48
+ tn.disabled = disabledNode(tn.data as Record<string, any>, tn) ?? false
49
+ })
50
+ }
51
+
52
+ // showCheckbox 可能读取 node.disabled(如按禁用状态决定是否渲染复选框),
53
+ // 因此单独一轮:等全部节点的 disabled 判定完再调用。
54
+ if (showCheckbox) {
55
+ forest.dfs((node) => {
56
+ const tn = node as unknown as TreeNode
57
+ if (!tn.data) return
58
+ tn.showCheckbox = showCheckbox(tn.data as Record<string, any>, tn) !== false
59
+ })
60
+ }
61
+
62
+ return forest
63
+ }
@@ -15,6 +15,8 @@ export class TreeNode<Val extends Record<string, any> = Record<string, any>> ext
15
15
  /** 多选是否选中 */
16
16
  checked = false
17
17
  disabled = false
18
+ /** 是否渲染复选框;`showCheckbox` 返回 false 的节点置为 false */
19
+ showCheckbox = true
18
20
  visible = true
19
21
  /** 子节点选中数量 */
20
22
  childrenCheckCount = 0
@@ -27,6 +29,15 @@ export class TreeNode<Val extends Record<string, any> = Record<string, any>> ext
27
29
  )
28
30
  }
29
31
 
32
+ /** 可勾选的子节点数量:父节点全选判定用,隐藏复选框的子节点不参与 */
33
+ get checkableChildrenCount(): number {
34
+ if (!this.children) return 0
35
+ let count = 0
36
+ for (const child of this.children) {
37
+ if (child.showCheckbox) count++
38
+ }
39
+ return count
40
+ }
30
41
  labelKey: string
31
42
  valueKey: string
32
43
 
@@ -17,14 +17,15 @@
17
17
  @click="handleClick"
18
18
  @contextmenu="treeEmit('node-contextmenu', $event, node)"
19
19
  v-ripple="
20
- (treeProps.selectable || (treeProps.checkable && treeProps.checkOnClickNode)) &&
20
+ (treeProps.selectable ||
21
+ (treeProps.checkable && node.showCheckbox && treeProps.checkOnClickNode)) &&
21
22
  !node.disabled
22
23
  ? cls.e('ripple')
23
24
  : false
24
25
  "
25
26
  >
26
27
  <u-checkbox
27
- v-if="treeProps.checkable"
28
+ v-if="treeProps.checkable && node.showCheckbox"
28
29
  :class="cls.e('checkbox')"
29
30
  :model-value="node.checked"
30
31
  :indeterminate="!treeProps.checkStrictly && node.indeterminate"
@@ -81,6 +82,7 @@ function handleClick(e: MouseEvent) {
81
82
  treeProps.selectable && handleSelect(node)
82
83
  treeProps.expandOnClickNode && toggleExpand()
83
84
  treeProps.checkable &&
85
+ node.showCheckbox &&
84
86
  treeProps.checkOnClickNode &&
85
87
  !node.disabled &&
86
88
  !treeProps.expandOnClickNode &&
@@ -49,6 +49,8 @@ export function useCheck(options: Options): UseCheckReturned {
49
49
  let lastDict: Map<any, TreeNode> | null = null
50
50
 
51
51
  function checkNode(node: TreeNode): void {
52
+ // 不渲染复选框的节点不是勾选值:不选中自身,也不参与向上计数
53
+ if (!node.showCheckbox) return
52
54
  if (node.checked) return
53
55
  node.checked = true
54
56
  if (!props.checkStrictly && node.parent) {
@@ -58,6 +60,7 @@ export function useCheck(options: Options): UseCheckReturned {
58
60
  }
59
61
 
60
62
  function uncheckNode(node: TreeNode): void {
63
+ if (!node.showCheckbox) return
61
64
  if (!node.checked) return
62
65
  node.checked = false
63
66
  if (!props.checkStrictly && node.parent) {
@@ -66,6 +69,22 @@ export function useCheck(options: Options): UseCheckReturned {
66
69
  checkedData.delete(node.data)
67
70
  }
68
71
 
72
+ /** 父节点是否视为已全选:分母只统计渲染了复选框的子节点 */
73
+ function isAllChildrenChecked(parent: TreeNode): boolean {
74
+ const count = parent.checkableChildrenCount
75
+ return count > 0 && parent.childrenCheckCount === count
76
+ }
77
+
78
+ /**
79
+ * 按子节点勾选情况同步父节点状态。
80
+ * @description 没有可勾选子节点时无法由子节点状态推导父节点,保持父节点自身状态,
81
+ * 否则点击父节点自身会被立即回滚。
82
+ */
83
+ function syncParentChecked(parent: TreeNode): void {
84
+ if (!parent.checkableChildrenCount) return
85
+ isAllChildrenChecked(parent) ? checkNode(parent) : uncheckNode(parent)
86
+ }
87
+
69
88
  /**
70
89
  * 模型值是否由事件触发
71
90
  */
@@ -152,9 +171,7 @@ export function useCheck(options: Options): UseCheckReturned {
152
171
  const { parent } = node
153
172
 
154
173
  if (parent) {
155
- const parentChecked = parent.childrenCheckCount === parent.children!.length
156
-
157
- parentChecked ? checkNode(parent) : uncheckNode(parent)
174
+ syncParentChecked(parent)
158
175
  }
159
176
  })
160
177
  }
@@ -1,9 +1,9 @@
1
- import { Forest } from '@cat-kit/core'
2
- import { fieldKey } from '@veltra/utils'
1
+ import type { Forest } from '@cat-kit/core'
3
2
  import { computed, shallowRef, triggerRef, watch, type ShallowRef, type ComputedRef } from 'vue'
4
3
 
5
4
  import type { TreeProps } from '../../types'
6
- import { TreeNode } from './tree-node'
5
+ import { createTreeForest } from './forest'
6
+ import type { TreeNode } from './tree-node'
7
7
 
8
8
  interface Options {
9
9
  props: TreeProps
@@ -25,53 +25,21 @@ export function useTreeNodes(options: Options): UseTreeNodesReturned {
25
25
  // - computed 会因为任一 prop 变化(尤其是内联 disabledNode 函数)而整棵重建,
26
26
  // 丢失 expanded/checked/visible 等运行时状态;
27
27
  // - 显式 watch 仅在真正影响结构的输入变化时才重建,行为可控、可测量。
28
- const forestRef = shallowRef<Forest<Record<string, unknown>, any>>(
29
- new Forest<Record<string, unknown>, any>({
30
- data: [],
31
- childrenKey: props.childrenKey ?? 'children'
32
- })
33
- )
28
+ const forestRef = shallowRef<Forest<Record<string, unknown>, any>>(createTreeForest({ data: [] }))
34
29
 
35
30
  const nodeDictRef = shallowRef<Map<string | number, TreeNode>>(new Map())
36
31
 
37
32
  const nodes = shallowRef<TreeNode[]>([])
38
33
 
39
- function buildForest(): Forest<Record<string, unknown>, any> {
40
- const { expandAll = false } = props
41
- const childrenKey = props.childrenKey ?? 'children'
42
- const valueKey = fieldKey(props.valueKey, 'value')
43
- const labelKey = fieldKey(props.labelKey, 'label')
44
-
45
- return new Forest<Record<string, unknown>, any>({
46
- data: (props.data ?? []) as Record<string, unknown>[],
47
- childrenKey,
48
- createNode: (data, index, depth, _f, parent) => {
49
- const node = new TreeNode({
50
- data: data as Record<string, any>,
51
- index,
52
- depth,
53
- parent: parent as TreeNode | undefined,
54
- valueKey,
55
- labelKey
56
- })
57
- node.expanded = expandAll
58
- return node
59
- }
60
- })
61
- }
62
-
63
34
  function rebuildForest(): void {
64
- const nextForest = buildForest()
35
+ const nextForest = createTreeForest(props)
65
36
  const nextDict = new Map<string | number, TreeNode>()
66
- const { disabledNode } = props
67
37
 
68
- // Forest 的 createNode 返回时 children 尚未挂载,因此 disabledNode 必须在
69
- // 整棵树构建完成后再调用,才能安全访问 node.children / node.isLeaf。
38
+ // 整棵树构建完成后节点才是完整的,disabledNode / showCheckbox 由 createTreeForest
39
+ // 在此之后统一判定,这里只需收集节点字典。
70
40
  nextForest.dfs((node) => {
71
- if (disabledNode && node.data) {
72
- node.disabled = disabledNode(node.data as Record<string, any>, node) ?? false
73
- }
74
- nextDict.set(node.key, node)
41
+ const tn = node as unknown as TreeNode
42
+ nextDict.set(tn.key, tn)
75
43
  })
76
44
 
77
45
  forestRef.value = nextForest
@@ -112,6 +80,7 @@ export function useTreeNodes(options: Options): UseTreeNodesReturned {
112
80
  [
113
81
  () => props.data,
114
82
  () => props.disabledNode,
83
+ () => props.showCheckbox,
115
84
  () => props.childrenKey,
116
85
  () => props.valueKey,
117
86
  () => props.labelKey,
@@ -1,7 +1,7 @@
1
1
  import type { DeconstructValue, FormComponentProps } from '@veltra/utils'
2
2
  import type { CSSProperties } from 'vue'
3
3
 
4
- import type { TreeProps } from './tree'
4
+ import type { TreeProps, TreeNode } from './tree'
5
5
  /** 树形多选组件组件属性 */
6
6
  export interface MultiTreeSelectProps
7
7
  extends
@@ -9,6 +9,14 @@ export interface MultiTreeSelectProps
9
9
  Omit<TreeProps, 'selected' | 'checked' | 'selectable' | 'checkable' | 'data'> {
10
10
  modelValue?: (string | number)[]
11
11
 
12
+ /**
13
+ * 是否渲染某个节点的复选框,签名与 `UTree` 一致,返回值原样透传给内部树。
14
+ * @description 返回 `false` 的节点不渲染 checkbox,也不会作为勾选值:头部全选总数、
15
+ * 标签回显与 `modelValue` 联动都跳过它;其后代各自判定,不受影响。
16
+ * @default 不传时每个节点都渲染复选框
17
+ */
18
+ showCheckbox?: (item: Record<string, any>, node: TreeNode) => boolean
19
+
12
20
  /**
13
21
  * 树数据
14
22
  * @description 如果传入一个函数,那么filterable会被强制启用;
package/src/types/tree.ts CHANGED
@@ -17,6 +17,8 @@ export interface TreeNode<Data extends Record<string, any> = Record<string, any>
17
17
  checked: boolean
18
18
  indeterminate: boolean
19
19
  disabled: boolean
20
+ /** 是否渲染复选框,由 `showCheckbox` 在整棵树构建完成后判定,默认 true */
21
+ showCheckbox: boolean
20
22
  label: string
21
23
  key: string | number
22
24
  childrenCheckCount: number
@@ -51,6 +53,13 @@ export interface TreeProps {
51
53
  * 在整棵树构建完成后调用,可安全访问 `node.children` / `node.isLeaf`。
52
54
  */
53
55
  disabledNode?: (item: Record<string, any>, node: TreeNode) => boolean
56
+ /**
57
+ * 是否渲染某个节点的复选框。返回 `false` 的节点不渲染 checkbox,也不作为勾选值参与
58
+ * 级联勾选、向上冒泡、全选与 `update:checked`;其后代各自判定,不受影响。
59
+ * 在整棵树构建完成后调用,可安全访问 `node.children` / `node.isLeaf`。
60
+ * @default 不传时每个节点都渲染复选框
61
+ */
62
+ showCheckbox?: (item: Record<string, any>, node: TreeNode) => boolean
54
63
  /** 可多选 */
55
64
  checkable?: boolean
56
65
  /**