@veltra/desktop 1.15.7 → 1.16.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/dist/components/multi-tree-select/multi-tree-select.js +44 -8
- package/dist/components/multi-tree-select/multi-tree-select.js.map +1 -1
- package/dist/components/table/style2.css +7 -0
- package/dist/components/table/table.js +46 -7
- package/dist/components/table/table.js.map +1 -1
- package/dist/components/table/use-cell-tip.js +62 -0
- package/dist/components/table/use-cell-tip.js.map +1 -0
- package/dist/components/tree/forest.js +47 -0
- package/dist/components/tree/forest.js.map +1 -0
- package/dist/components/tree/tree-node.js +3 -3
- package/dist/components/tree/tree-node.js.map +1 -1
- package/dist/components/tree/tree-node2.js +9 -0
- package/dist/components/tree/tree-node2.js.map +1 -1
- package/dist/components/tree/tree.js +1 -0
- package/dist/components/tree/tree.js.map +1 -1
- package/dist/components/tree/use-check.js +17 -1
- package/dist/components/tree/use-check.js.map +1 -1
- package/dist/components/tree/use-tree-nodes.js +6 -33
- package/dist/components/tree/use-tree-nodes.js.map +1 -1
- package/dist/components/tree-select/tree-select.js +1 -0
- package/dist/components/tree-select/tree-select.js.map +1 -1
- package/dist/types/multi-tree-select.d.ts +8 -1
- package/dist/types/tree.d.ts +9 -0
- package/package.json +1 -1
- package/src/components/multi-tree-select/__test__/show-checkbox.test.ts +216 -0
- package/src/components/multi-tree-select/multi-tree-select.vue +44 -9
- package/src/components/table/__test__/cell-tip.test.ts +117 -0
- package/src/components/table/style.scss +12 -0
- package/src/components/table/table.vue +44 -7
- package/src/components/table/use-cell-tip.ts +76 -0
- package/src/components/tree/__test__/show-checkbox.test.ts +153 -0
- package/src/components/tree/forest.ts +63 -0
- package/src/components/tree/tree-node.ts +11 -0
- package/src/components/tree/tree-node.vue +4 -2
- package/src/components/tree/use-check.ts +20 -3
- package/src/components/tree/use-tree-nodes.ts +10 -41
- package/src/types/multi-tree-select.ts +9 -1
- package/src/types/tree.ts +9 -0
|
@@ -23,10 +23,10 @@
|
|
|
23
23
|
<!-- 选择的数据项 -->
|
|
24
24
|
<div v-if="tags.length || filterable" :class="cls.e('tags')">
|
|
25
25
|
<u-tag
|
|
26
|
-
v-for="
|
|
26
|
+
v-for="tag in visibleTags"
|
|
27
27
|
:key="tag[valueKey]"
|
|
28
28
|
:closable="!disabled"
|
|
29
|
-
@close="handleRemove(
|
|
29
|
+
@close="handleRemove(tag[valueKey])"
|
|
30
30
|
>
|
|
31
31
|
{{ renderNodeLabel(tag) }}
|
|
32
32
|
</u-tag>
|
|
@@ -109,7 +109,7 @@ import { bem, fieldKey, FORM_EMPTY_CONTENT, renderLabelTemplate } from '@veltra/
|
|
|
109
109
|
import { injectFormContext } from '@veltra/utils'
|
|
110
110
|
import { computed, nextTick, shallowRef, useTemplateRef, watch } from 'vue'
|
|
111
111
|
|
|
112
|
-
import type { MultiTreeSelectProps, MultiTreeSelectEmits, TreeExposed } from '../../types'
|
|
112
|
+
import type { MultiTreeSelectProps, MultiTreeSelectEmits, TreeExposed, TreeNode } from '../../types'
|
|
113
113
|
import type { DropdownExposed } from '../../types'
|
|
114
114
|
import { UButton } from '../button'
|
|
115
115
|
import { UCheckbox } from '../checkbox'
|
|
@@ -119,6 +119,7 @@ import { ULoading } from '../loading'
|
|
|
119
119
|
import { UTag } from '../tag'
|
|
120
120
|
import { UTree } from '../tree'
|
|
121
121
|
import type { TreeSlotsScope } from '../tree/di'
|
|
122
|
+
import { createTreeForest } from '../tree/forest'
|
|
122
123
|
|
|
123
124
|
defineOptions({ name: 'UMultiTreeSelect', inheritAttrs: false })
|
|
124
125
|
|
|
@@ -222,6 +223,37 @@ watch(
|
|
|
222
223
|
|
|
223
224
|
const model = defineModel<(string | number)[]>({ default: () => [] })
|
|
224
225
|
|
|
226
|
+
/**
|
|
227
|
+
* 渲染了复选框的节点 value 集合。
|
|
228
|
+
* @description 与 UTree 内部同源(共用 `createTreeForest`):`showCheckbox` 返回 false 的节点
|
|
229
|
+
* 不在集合内。未配置 `showCheckbox` 时为 undefined,完全跳过过滤以保持原有行为。
|
|
230
|
+
* 面板为懒渲染(挂载前 treeRef 为空),因此按数据单独构建一次,供头部计数与标签回显使用。
|
|
231
|
+
*/
|
|
232
|
+
const checkableValues = computed<Set<string | number> | undefined>(() => {
|
|
233
|
+
const { showCheckbox } = props
|
|
234
|
+
if (!showCheckbox) return undefined
|
|
235
|
+
|
|
236
|
+
const values = new Set<string | number>()
|
|
237
|
+
createTreeForest({
|
|
238
|
+
data: treeData.value as Record<string, any>[],
|
|
239
|
+
childrenKey: props.childrenKey,
|
|
240
|
+
valueKey: props.valueKey,
|
|
241
|
+
labelKey: props.labelKey,
|
|
242
|
+
disabledNode: props.disabledNode,
|
|
243
|
+
showCheckbox
|
|
244
|
+
}).dfs((node) => {
|
|
245
|
+
const tn = node as unknown as TreeNode
|
|
246
|
+
if (tn.showCheckbox) values.add(tn.key)
|
|
247
|
+
})
|
|
248
|
+
return values
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
/** 模型中的有效选中值:配了 showCheckbox 时剔除隐藏复选框的节点 */
|
|
252
|
+
const effectiveModel = computed<(string | number)[]>(() => {
|
|
253
|
+
const values = checkableValues.value
|
|
254
|
+
return values ? model.value.filter((v) => values.has(v)) : model.value
|
|
255
|
+
})
|
|
256
|
+
|
|
225
257
|
const hovered = shallowRef(false)
|
|
226
258
|
|
|
227
259
|
const tags = shallowRef<Record<string, any>[]>([])
|
|
@@ -247,12 +279,14 @@ function markEvent() {
|
|
|
247
279
|
|
|
248
280
|
/**是否全选 */
|
|
249
281
|
const allChecked = computed(() => {
|
|
282
|
+
const values = checkableValues.value
|
|
283
|
+
if (values) return values.size > 0 && effectiveModel.value.length === values.size
|
|
250
284
|
return model.value?.length === treeRef.value?.forest.size
|
|
251
285
|
})
|
|
252
286
|
|
|
253
287
|
/**部分 */
|
|
254
288
|
const indeterminate = computed(() => {
|
|
255
|
-
return
|
|
289
|
+
return effectiveModel.value.length > 0 && !allChecked.value
|
|
256
290
|
})
|
|
257
291
|
|
|
258
292
|
/** 全选*/
|
|
@@ -272,10 +306,10 @@ const handleCheck = (checked: (string | number)[], checkedData: Record<string, a
|
|
|
272
306
|
}
|
|
273
307
|
|
|
274
308
|
/**删除 */
|
|
275
|
-
const handleRemove = (
|
|
309
|
+
const handleRemove = (value: string | number) => {
|
|
276
310
|
markEvent()
|
|
277
|
-
tags.value = tags.value.filter((
|
|
278
|
-
model.value = model.value.filter((
|
|
311
|
+
tags.value = tags.value.filter((tag) => tag[valueKey.value] !== value)
|
|
312
|
+
model.value = model.value.filter((v) => v !== value)
|
|
279
313
|
emit('change', tags.value)
|
|
280
314
|
}
|
|
281
315
|
|
|
@@ -312,7 +346,7 @@ watch(
|
|
|
312
346
|
)
|
|
313
347
|
|
|
314
348
|
watch(
|
|
315
|
-
[keyDicts, model],
|
|
349
|
+
[keyDicts, model, checkableValues],
|
|
316
350
|
([keyDicts, model]) => {
|
|
317
351
|
if (changedByEvent) return
|
|
318
352
|
|
|
@@ -321,7 +355,8 @@ watch(
|
|
|
321
355
|
return
|
|
322
356
|
}
|
|
323
357
|
|
|
324
|
-
|
|
358
|
+
// 隐藏了复选框的节点不作为已选标签回显
|
|
359
|
+
tags.value = effectiveModel.value.filter((v) => keyDicts.has(v)).map((v) => keyDicts.get(v)!)
|
|
325
360
|
},
|
|
326
361
|
{ immediate: true }
|
|
327
362
|
)
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
4
|
+
import { createApp } from 'vue'
|
|
5
|
+
|
|
6
|
+
import { useCellTip } from '../use-cell-tip'
|
|
7
|
+
|
|
8
|
+
let tip: ReturnType<typeof useCellTip>
|
|
9
|
+
let app: ReturnType<typeof createApp>
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
document.body.innerHTML = ''
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
app?.unmount()
|
|
17
|
+
document.body.innerHTML = ''
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
/** 挂载 hook 并返回一个单元格与悬停函数 */
|
|
21
|
+
function setup(text = '很长很长很长的文本', options?: { delay?: number; enabled?: () => boolean }) {
|
|
22
|
+
const cell = document.createElement('td')
|
|
23
|
+
cell.className = 'u-table__cell'
|
|
24
|
+
cell.textContent = text
|
|
25
|
+
document.body.appendChild(cell)
|
|
26
|
+
|
|
27
|
+
const host = document.createElement('div')
|
|
28
|
+
document.body.appendChild(host)
|
|
29
|
+
app = createApp({
|
|
30
|
+
setup() {
|
|
31
|
+
tip = useCellTip({
|
|
32
|
+
cellSelector: '.u-table__cell',
|
|
33
|
+
enabled: options?.enabled ?? (() => true),
|
|
34
|
+
delay: options?.delay ?? 0
|
|
35
|
+
})
|
|
36
|
+
return () => null
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
app.mount(host)
|
|
40
|
+
|
|
41
|
+
const hover = () => {
|
|
42
|
+
const e = new MouseEvent('mouseover')
|
|
43
|
+
Object.defineProperty(e, 'target', { value: cell })
|
|
44
|
+
tip.handleMouseover(e)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return { cell, hover, tip }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
describe('useCellTip', () => {
|
|
51
|
+
it('悬停单元格时展示完整文本', () => {
|
|
52
|
+
const { cell, hover, tip } = setup()
|
|
53
|
+
hover()
|
|
54
|
+
|
|
55
|
+
expect(tip.visible.value).toBe(true)
|
|
56
|
+
expect(tip.trigger.value).toBe(cell)
|
|
57
|
+
expect(tip.content.value).toBe('很长很长很长的文本')
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('跨列单元格(展开行)不展示提示', () => {
|
|
61
|
+
const { cell, hover, tip } = setup()
|
|
62
|
+
Object.defineProperty(cell, 'colSpan', { value: 3 })
|
|
63
|
+
hover()
|
|
64
|
+
|
|
65
|
+
expect(tip.visible.value).toBe(false)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('提示横向对齐跟随单元格的列对齐', () => {
|
|
69
|
+
for (const [cls, expected] of [
|
|
70
|
+
['is-left', 'start'],
|
|
71
|
+
['is-center', 'center'],
|
|
72
|
+
['is-right', 'end']
|
|
73
|
+
] as const) {
|
|
74
|
+
const { cell, hover, tip } = setup()
|
|
75
|
+
cell.className = `u-table__cell ${cls}`
|
|
76
|
+
hover()
|
|
77
|
+
expect(tip.align.value).toBe(expected)
|
|
78
|
+
app.unmount()
|
|
79
|
+
document.body.innerHTML = ''
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('未启用时完全不响应', () => {
|
|
84
|
+
const { hover, tip } = setup('很长很长很长的文本', { enabled: () => false })
|
|
85
|
+
hover()
|
|
86
|
+
|
|
87
|
+
expect(tip.visible.value).toBe(false)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('鼠标移出表格时收起提示', () => {
|
|
91
|
+
const { hover, tip } = setup()
|
|
92
|
+
hover()
|
|
93
|
+
expect(tip.visible.value).toBe(true)
|
|
94
|
+
|
|
95
|
+
tip.handleMouseleave()
|
|
96
|
+
expect(tip.visible.value).toBe(false)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('延迟到期后才弹出,延迟期间离开则不弹', () => {
|
|
100
|
+
vi.useFakeTimers()
|
|
101
|
+
const { hover, tip } = setup('很长很长很长的文本', { delay: 200 })
|
|
102
|
+
|
|
103
|
+
hover()
|
|
104
|
+
expect(tip.visible.value).toBe(false)
|
|
105
|
+
vi.advanceTimersByTime(200)
|
|
106
|
+
expect(tip.visible.value).toBe(true)
|
|
107
|
+
|
|
108
|
+
tip.handleMouseleave()
|
|
109
|
+
hover()
|
|
110
|
+
vi.advanceTimersByTime(100)
|
|
111
|
+
tip.handleMouseleave()
|
|
112
|
+
vi.advanceTimersByTime(200)
|
|
113
|
+
expect(tip.visible.value).toBe(false)
|
|
114
|
+
|
|
115
|
+
vi.useRealTimers()
|
|
116
|
+
})
|
|
117
|
+
})
|
|
@@ -281,3 +281,15 @@ $root-name: table;
|
|
|
281
281
|
}
|
|
282
282
|
}
|
|
283
283
|
}
|
|
284
|
+
|
|
285
|
+
// 单元格溢出提示的内容层。
|
|
286
|
+
// 该节点被 teleport 到 pop-container,不在 .u-table 后代里,因此不能写在块内;
|
|
287
|
+
// tip 默认 width: max-content,长文本需显式限宽并允许换行。
|
|
288
|
+
// 注意类名前缀:$root-name 是 'table',块级混入才会补 'u-',这里需手写完整类名。
|
|
289
|
+
.u-#{$root-name}__tip {
|
|
290
|
+
max-width: 360px;
|
|
291
|
+
// 纯展示,不拦截鼠标:否则提示会盖住相邻单元格,用户无法直接移过去
|
|
292
|
+
pointer-events: none;
|
|
293
|
+
white-space: normal;
|
|
294
|
+
word-break: break-all;
|
|
295
|
+
}
|
|
@@ -9,7 +9,9 @@
|
|
|
9
9
|
]"
|
|
10
10
|
ref="scrollRef"
|
|
11
11
|
:content-class="cls.e('content')"
|
|
12
|
-
@scroll="
|
|
12
|
+
@scroll="handleTableScroll"
|
|
13
|
+
@mouseover="handleCellMouseover"
|
|
14
|
+
@mouseleave="handleCellMouseleave"
|
|
13
15
|
>
|
|
14
16
|
<table :class="cls.e('wrap')">
|
|
15
17
|
<colgroup ref="colgroupRef">
|
|
@@ -60,7 +62,16 @@
|
|
|
60
62
|
|
|
61
63
|
<div v-if="showResizeLine" :class="cls.e('resize-line')" ref="resizeLineRef"></div>
|
|
62
64
|
|
|
63
|
-
<!--
|
|
65
|
+
<!-- 单元格文本溢出提示:整表仅一个实例,由 useCellTip 按需驱动 -->
|
|
66
|
+
<u-tip
|
|
67
|
+
v-if="textEllipsis"
|
|
68
|
+
:visible="cellTipVisible"
|
|
69
|
+
:trigger-dom="cellTipTrigger"
|
|
70
|
+
:content="cellTipContent"
|
|
71
|
+
:alignment="cellTipAlign"
|
|
72
|
+
:class="cls.e('tip')"
|
|
73
|
+
direction="top"
|
|
74
|
+
/>
|
|
64
75
|
</u-scroll>
|
|
65
76
|
</template>
|
|
66
77
|
|
|
@@ -79,13 +90,14 @@ import type {
|
|
|
79
90
|
TableRowSlotsScope
|
|
80
91
|
} from '../../types'
|
|
81
92
|
import { UScroll } from '../scroll'
|
|
93
|
+
import { UTip } from '../tip'
|
|
82
94
|
import { TableDIKey } from './di'
|
|
83
95
|
import type { ColumnNode } from './node/col'
|
|
84
96
|
import type { TableRowNode } from './node/row'
|
|
85
97
|
import UTableBody from './table-body.vue'
|
|
86
98
|
import UTableFoot from './table-foot.vue'
|
|
87
|
-
// import { UTip } from '../tip'
|
|
88
99
|
import UTableHead from './table-head'
|
|
100
|
+
import { useCellTip } from './use-cell-tip'
|
|
89
101
|
import { useCheck } from './use-check'
|
|
90
102
|
import { useColResize } from './use-col-resize'
|
|
91
103
|
import { useColumns } from './use-columns'
|
|
@@ -98,7 +110,6 @@ defineOptions({ name: 'UTable' })
|
|
|
98
110
|
const props = withDefaults(defineProps<TableProps>(), {
|
|
99
111
|
tree: false,
|
|
100
112
|
stripe: true,
|
|
101
|
-
textOverflow: 'line-break',
|
|
102
113
|
virtualThreshold: 80
|
|
103
114
|
})
|
|
104
115
|
const emit = defineEmits<TableEmits>()
|
|
@@ -153,6 +164,35 @@ const { leafColumns } = columnConfig
|
|
|
153
164
|
|
|
154
165
|
const { handleScroll, leftFixed, rightFixed } = useFixedColumns()
|
|
155
166
|
|
|
167
|
+
/**
|
|
168
|
+
* 滚动时收起提示:虚拟滚动会把滚出视口的行卸载,触发元素随之失效;
|
|
169
|
+
* 同时滚动中继续挂着提示也不符合交互惯例。
|
|
170
|
+
*/
|
|
171
|
+
const handleTableScroll: typeof handleScroll = (e) => {
|
|
172
|
+
handleCellMouseleave()
|
|
173
|
+
handleScroll(e)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 省略号模式下的溢出提示。
|
|
178
|
+
*
|
|
179
|
+
* 整表只挂一个 tip(见模板),这里只负责「哪个单元格被截断」的判定。
|
|
180
|
+
* 事件委托在根节点,单元格本身零成本;滚动时立即收起,避免提示跟随已回收的行。
|
|
181
|
+
*/
|
|
182
|
+
const {
|
|
183
|
+
visible: cellTipVisible,
|
|
184
|
+
trigger: cellTipTrigger,
|
|
185
|
+
content: cellTipContent,
|
|
186
|
+
align: cellTipAlign,
|
|
187
|
+
handleMouseover: handleCellMouseover,
|
|
188
|
+
handleMouseleave: handleCellMouseleave
|
|
189
|
+
} = useCellTip({
|
|
190
|
+
cellSelector: `.${cls.e('cell')}`,
|
|
191
|
+
enabled: () => props.textEllipsis === true,
|
|
192
|
+
// 快速掠过表格时不打扰,停留片刻才提示
|
|
193
|
+
delay: 200
|
|
194
|
+
})
|
|
195
|
+
|
|
156
196
|
const {
|
|
157
197
|
getColumnSlotsNode,
|
|
158
198
|
getExpandRowSlotsNode,
|
|
@@ -210,8 +250,6 @@ watch(
|
|
|
210
250
|
}
|
|
211
251
|
)
|
|
212
252
|
|
|
213
|
-
// const tipRef = useTemplateRef('tip')
|
|
214
|
-
|
|
215
253
|
provide(TableDIKey, {
|
|
216
254
|
tableProps: props,
|
|
217
255
|
tableSlots: slots,
|
|
@@ -227,7 +265,6 @@ provide(TableDIKey, {
|
|
|
227
265
|
getCellClass,
|
|
228
266
|
getCellCtx,
|
|
229
267
|
getHeaderCellClass,
|
|
230
|
-
// tipRef,
|
|
231
268
|
toggleTreeRowExpand,
|
|
232
269
|
virtualList: items,
|
|
233
270
|
virtualEnabled,
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { onBeforeUnmount, shallowRef } from 'vue'
|
|
2
|
+
|
|
3
|
+
import type { TipAlign } from '../../types'
|
|
4
|
+
|
|
5
|
+
interface Options {
|
|
6
|
+
/** 单元格选择器,事件委托用 */
|
|
7
|
+
cellSelector: string
|
|
8
|
+
/** 是否启用 */
|
|
9
|
+
enabled: () => boolean
|
|
10
|
+
/** 显示延迟(毫秒),避免鼠标快速掠过时闪烁 */
|
|
11
|
+
delay?: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** 列对齐 → tip 横向对齐,让提示跟单元格文字对齐方式一致 */
|
|
15
|
+
const ALIGN_MAP: Record<string, TipAlign> = { left: 'start', center: 'center', right: 'end' }
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 单元格文本提示:整表只挂一个 tip,事件委托到根节点,不给每个单元格包 tip。
|
|
19
|
+
* 不判断文本是否真的溢出——开了省略号就展示完整内容,省掉每次悬停的测量开销。
|
|
20
|
+
*/
|
|
21
|
+
export function useCellTip({ cellSelector, enabled, delay = 0 }: Options) {
|
|
22
|
+
const visible = shallowRef(false)
|
|
23
|
+
const trigger = shallowRef<HTMLElement>()
|
|
24
|
+
const content = shallowRef('')
|
|
25
|
+
const align = shallowRef<TipAlign>('center')
|
|
26
|
+
|
|
27
|
+
/** 上次悬停的单元格:在单元格内部子元素间移动时早退,避免提示闪烁 */
|
|
28
|
+
let hoverCell: HTMLElement | null = null
|
|
29
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
30
|
+
|
|
31
|
+
const hide = () => {
|
|
32
|
+
clearTimeout(timer)
|
|
33
|
+
visible.value = false
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const handleMouseover = (e: MouseEvent) => {
|
|
37
|
+
if (!enabled()) return
|
|
38
|
+
|
|
39
|
+
const target = e.target
|
|
40
|
+
if (!(target instanceof Element)) return
|
|
41
|
+
|
|
42
|
+
const cell = target.closest<HTMLElement>(cellSelector)
|
|
43
|
+
if (cell === hoverCell) return
|
|
44
|
+
hoverCell = cell
|
|
45
|
+
|
|
46
|
+
if (!cell) return hide()
|
|
47
|
+
|
|
48
|
+
// 跨列单元格(展开行)装的是整块自定义内容,textContent 拼起来没有意义
|
|
49
|
+
const spanning = cell instanceof HTMLTableCellElement && cell.colSpan > 1
|
|
50
|
+
const text = spanning ? '' : cell.textContent?.trim()
|
|
51
|
+
if (!text) return hide()
|
|
52
|
+
|
|
53
|
+
// 列的 align 已反映在单元格的 is-left / is-center / is-right 类上
|
|
54
|
+
const match = cell.className.match(/is-(left|center|right)/)
|
|
55
|
+
align.value = ALIGN_MAP[match?.[1] ?? ''] ?? 'center'
|
|
56
|
+
|
|
57
|
+
const show = () => {
|
|
58
|
+
trigger.value = cell
|
|
59
|
+
content.value = text
|
|
60
|
+
visible.value = true
|
|
61
|
+
}
|
|
62
|
+
clearTimeout(timer)
|
|
63
|
+
// 已经弹出或未配置延迟时直接跟随鼠标,不必等待
|
|
64
|
+
if (delay <= 0 || visible.value) show()
|
|
65
|
+
else timer = setTimeout(show, delay)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const handleMouseleave = () => {
|
|
69
|
+
hoverCell = null
|
|
70
|
+
hide()
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
onBeforeUnmount(() => clearTimeout(timer))
|
|
74
|
+
|
|
75
|
+
return { visible, trigger, content, align, handleMouseover, handleMouseleave }
|
|
76
|
+
}
|
|
@@ -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 ||
|
|
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 &&
|