multiple-select-display.helpers.js
3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* multipleSelect 展示名解析 — 纯函数,供组件回显与 jsonContent 编解码共用
* @see packageDynamicForm/docs/field-options-api.md
*/
/**
* 展示名是否实为 id(jsonContent 误把 uuid 写入 value 时不应作为 label)
* @param {*} label 候选展示名
* @param {*} [id] 对应选项 id
* @returns {boolean}
*/
function isMultipleSelectIdLikeLabel(label, id = '') {
if (label == null || label === '') {
return true
}
return String(label) === String(id)
}
/**
* 解析 multipleSelect 单选项展示文案;optionsKey 未匹配时不回退 id
* @param {Object} [params]
* @param {string} params.selectedKey 选中 id
* @param {*} [params.rawItem] 引擎值中的原始项
* @param {Object} [params.matched] 已加载 options 中的匹配项
* @param {boolean} [params.hasOptionsKey] 是否远程 optionsKey 字段
* @returns {string}
*/
function resolveMultipleSelectItemDisplayLabel({
selectedKey,
rawItem,
matched,
hasOptionsKey = false
} = {}) {
if (rawItem != null && typeof rawItem === 'object') {
const name = rawItem.name || rawItem.label
if (name && !isMultipleSelectIdLikeLabel(name, selectedKey)) {
return String(name)
}
}
if (matched?.label) {
return String(matched.label)
}
return hasOptionsKey ? '' : String(selectedKey || '')
}
/**
* `/` 拼接展示串中剔除与 id 相同的段(保存入库前防御)
* @param {Array<string>} [ids=[]]
* @param {string} [displayLabel='']
* @returns {string}
*/
function sanitizeMultipleSelectSlashLabel(ids = [], displayLabel = '') {
if (typeof displayLabel !== 'string' || displayLabel === '') {
return ''
}
const parts = displayLabel
.trim()
.split('/')
.map(part => part.trim())
const sanitized = ids
.map((id, index) => {
const part = parts[index]
if (part == null || part === '' || isMultipleSelectIdLikeLabel(part, id)) {
return ''
}
return part
})
.filter(Boolean)
return sanitized.join('/')
}
/** App jsonContent 双 key:`value` 为 `/` 拼接展示串 */
function splitMultipleSelectDisplayLabel(value) {
if (typeof value !== 'string' || value === '') {
return []
}
const trimmed = value.trim()
if (trimmed.startsWith('[')) {
return []
}
return trimmed.split('/').map(part => part.trim()).filter(part => part !== '')
}
/** `/` 展示串是否含至少一段真实姓名(非 id) */
function hasRealMultipleSelectDisplayNames(ids = [], displayLabel = '') {
const names = splitMultipleSelectDisplayLabel(displayLabel)
if (!names.length) {
return false
}
return names.some((name, index) => !isMultipleSelectIdLikeLabel(name, ids[index]))
}
/** id 数组 + `/` 展示串 → `{ id, name? }[]`(不把 id 当 name) */
function zipMultipleSelectIdsWithDisplayLabel(ids = [], displayLabel = '') {
const names = splitMultipleSelectDisplayLabel(displayLabel)
return ids.map((id, index) => {
const idStr = String(id)
const namePart = names[index]
const entry = { id: idStr }
if (
namePart != null &&
namePart !== '' &&
!isMultipleSelectIdLikeLabel(namePart, idStr)
) {
entry.name = namePart
}
return entry
})
}
module.exports = {
isMultipleSelectIdLikeLabel,
resolveMultipleSelectItemDisplayLabel,
sanitizeMultipleSelectSlashLabel,
splitMultipleSelectDisplayLabel,
hasRealMultipleSelectDisplayNames,
zipMultipleSelectIdsWithDisplayLabel
}