multiple-select-value.js
3.77 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
119
120
121
122
123
124
/** multipleSelect 字段值归一化 — @see packageDynamicForm/docs/ARCHITECTURE.md */
import { flattenFields } from './visibility.js'
import { listSubforms, getSubformRowTemplate } from './field-types.js'
import { getGroupRows } from './group-values.js'
const REMOTE_MULTIPLE_SELECT_OPTIONS_KEYS = new Set([
'4sStaffList',
'storeStaffList',
'orderList'
])
function normalizeMultipleSelectItem(item) {
if (item == null || item === '') {
return null
}
if (typeof item === 'object') {
return item
}
return String(item)
}
/** 将 multipleSelect 原始值强制为数组(支持 JSON 字符串、对象数组、单值) */
export function coerceMultipleSelectValue(rawValue) {
if (rawValue == null || rawValue === '') {
return rawValue
}
if (Array.isArray(rawValue)) {
return rawValue.map(normalizeMultipleSelectItem).filter(Boolean)
}
if (typeof rawValue === 'string') {
const trimmed = rawValue.trim()
if (trimmed.startsWith('[')) {
try {
const parsed = JSON.parse(trimmed)
if (Array.isArray(parsed)) {
return parsed.map(normalizeMultipleSelectItem).filter(Boolean)
}
} catch (error) {
// fall through to single value
}
}
return trimmed ? [trimmed] : []
}
return [String(rawValue)]
}
function isRemoteMultipleSelectField(field = {}) {
return REMOTE_MULTIPLE_SELECT_OPTIONS_KEYS.has(field.optionsKey)
}
function getAliases(value) {
if (value == null || value === '') return []
if (typeof value !== 'object') return [String(value)]
return [...new Set([value.value, value.id, value.name, value.label]
.filter(alias => alias != null && alias !== '')
.map(String))]
}
/** 静态 multipleSelect 统一使用 option.value;历史 label/id 会映射到新 value。 */
export function resolveMultipleSelectOptionValue(field = {}, rawValue) {
const values = coerceMultipleSelectValue(rawValue)
if (!Array.isArray(values) || isRemoteMultipleSelectField(field)) {
return values
}
const options = Array.isArray(field.options) ? field.options : []
if (!options.length) return values
return values.map(item => {
const aliases = new Set(getAliases(item))
const matched = options.find(option =>
getAliases(option).some(alias => aliases.has(alias))
)
return matched ? (matched.value ?? matched.id ?? item) : item
})
}
function normalizeMultipleSelectFieldValue(field, rawValue) {
if (rawValue === undefined) return undefined
return resolveMultipleSelectOptionValue(field, rawValue)
}
/** 批量归一化 values 中所有 multipleSelect 字段 */
export function normalizeMultipleSelectValuesInMap(list = [], values = {}) {
const multipleSelectFields = flattenFields(list).filter(
field => field.type === 'multipleSelect'
)
if (!multipleSelectFields.length) {
return values
}
const nextValues = { ...values }
multipleSelectFields.forEach(field => {
if (nextValues[field.key] === undefined) {
return
}
nextValues[field.key] = normalizeMultipleSelectFieldValue(field, nextValues[field.key])
})
listSubforms(list).forEach(subform => {
const childMultipleSelects = getSubformRowTemplate(subform).filter(
child => child.type === 'multipleSelect'
)
if (!childMultipleSelects.length) return
const rows = getGroupRows(nextValues, subform.key)
if (!rows.length) return
nextValues[subform.key] = rows.map(row => {
const nextRow = { ...row }
childMultipleSelects.forEach(child => {
if (nextRow[child.key] === undefined) return
nextRow[child.key] = normalizeMultipleSelectFieldValue(child, nextRow[child.key])
})
return nextRow
})
})
return nextValues
}
if (typeof module !== 'undefined' && module.exports) {
const displayHelpers = require('./multiple-select-display.helpers.js')
module.exports = {
coerceMultipleSelectValue,
resolveMultipleSelectOptionValue,
normalizeMultipleSelectValuesInMap,
...displayHelpers
}
}