field-submit-value.js
8.23 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* fieldDataList / subformDataList 提交与回显值转换
* @see packageDynamicForm/docs/field-options-api.md
*/
import { coerceSelectValue } from './select-value.js'
import { coerceMultipleSelectValue } from './multiple-select-value.js'
import {
normalizeCarBrandValue,
carInfoToCascadeLevels,
cascadeLevelsToCarInfo
} from './car-brand-value.js'
function isPlainObject(value) {
return value != null && typeof value === 'object' && !Array.isArray(value)
}
function resolveOptionId(option = {}) {
return option.id ?? option.orderCode ?? option.value ?? ''
}
function resolveOptionName(option = {}) {
return option.name ?? option.orderCode ?? option.label ?? option.productName ?? ''
}
/** 将 picker option 归一化为 { id, name, ...extras } */
export function normalizeOptionRecord(option = {}) {
if (!option || typeof option !== 'object') {
return null
}
const id = resolveOptionId(option)
const name = resolveOptionName(option)
if (id === '' && name === '') {
return null
}
const next = { id: String(id), name: String(name) }
Object.keys(option).forEach(key => {
if (key === 'label' || key === 'value') {
return
}
if (option[key] != null && option[key] !== '') {
next[key] = option[key]
}
})
return next
}
function findStaticOption(field = {}, rawId) {
const str = String(rawId)
return (field.options || []).find(
option =>
String(option.value) === str ||
String(option.id) === str ||
String(option.label) === str ||
String(option.name) === str
)
}
function scalarToSelectObject(field = {}, rawValue) {
const scalar = coerceSelectValue(rawValue)
if (scalar === '' || scalar == null) {
return undefined
}
const matched = findStaticOption(field, scalar)
return {
id: String(matched?.id ?? matched?.value ?? scalar),
name: matched?.label ?? matched?.name ?? matched?.value ?? String(scalar)
}
}
/** 从 subform 行 FormField 提取 fieldDataList 用的 select 完整对象 */
export function extractSelectStorageValueFromRowField(field = {}) {
if (field.type !== 'select') {
return undefined
}
const key = field.key
const meta = key != null ? field[key] : undefined
if (field.optionsKey) {
if (isPlainObject(meta)) {
return normalizeOptionRecord(meta) || undefined
}
const v = field.value
if (isPlainObject(v)) {
return normalizeOptionRecord(v) || undefined
}
const scalar = meta != null && meta !== '' ? meta : v
if (scalar === undefined || scalar === null || scalar === '') {
return undefined
}
const obj = scalarToSelectObject(field, scalar)
if (
obj &&
typeof v === 'string' &&
v !== '' &&
String(v) !== String(scalar) &&
(!obj.name || obj.name === String(scalar))
) {
obj.name = v
}
return obj
}
const v = field.value
if (v === undefined || v === null || v === '') {
return undefined
}
if (isPlainObject(v)) {
return v
}
return scalarToSelectObject(field, v)
}
/**
* subform 校验取值:optionsKey select 优先 field[key] / 行 map(避免 value 为空串时误报必填)
* @param {Object} [rowField]
* @param {string} [childKey]
* @param {*} [rowMapValue]
* @returns {*}
*/
export function resolveSelectValueForValidation(rowField = {}, childKey, rowMapValue) {
if (rowField?.type !== 'select' || !rowField?.optionsKey) {
return rowField?.value !== undefined ? rowField.value : rowMapValue
}
const key = childKey ?? rowField.key
const meta = key != null ? rowField[key] : undefined
if (isPlainObject(meta)) {
return meta
}
if (meta != null && meta !== '') {
return meta
}
if (isPlainObject(rowMapValue)) {
return rowMapValue
}
if (rowMapValue != null && rowMapValue !== '') {
return rowMapValue
}
const v = rowField.value
if (v !== undefined && v !== null && v !== '') {
return v
}
return rowMapValue
}
/** select 提交值 → { id, name, ... } */
export function buildSelectSubmitValue(field = {}, rawValue) {
if (rawValue == null || rawValue === '') {
return undefined
}
if (isPlainObject(rawValue)) {
const normalized = normalizeOptionRecord(rawValue)
return normalized || undefined
}
return scalarToSelectObject(field, rawValue)
}
function itemToSelectObject(field = {}, item) {
if (isPlainObject(item)) {
const normalized = normalizeOptionRecord(item)
if (!normalized) return null
if (field.optionsKey && field.optionsKey !== 'options') return normalized
const matched = findStaticOption(field, normalized.name) ||
findStaticOption(field, normalized.id)
if (!matched) return normalized
return {
...normalized,
id: String(matched.id ?? matched.value ?? normalized.id),
name: String(matched.label ?? matched.name ?? matched.value ?? normalized.name)
}
}
return scalarToSelectObject(field, item)
}
/** multipleSelect 提交值 → [{ id, name, ... }, ...] */
export function buildMultipleSelectSubmitValue(field = {}, rawValue) {
const coerced = coerceMultipleSelectValue(rawValue)
if (coerced == null || coerced === '') {
return undefined
}
if (!Array.isArray(coerced)) {
return undefined
}
const items = coerced
.map(item => itemToSelectObject(field, item))
.filter(Boolean)
return items.length ? items : undefined
}
/** carBrandSelect 提交值 → [{ id, name }×4] */
export function buildCarBrandSubmitValue(rawValue) {
const normalized = normalizeCarBrandValue(rawValue)
if (!normalized) {
return undefined
}
const levels = carInfoToCascadeLevels(normalized)
return levels.length ? levels : undefined
}
/** fieldDataList 回显:select → 引擎值(对象或标量) */
export function parseSelectEchoValue(rawValue, field = {}) {
if (rawValue == null || rawValue === '') {
return undefined
}
if (isPlainObject(rawValue)) {
if (field.optionsKey) {
return normalizeOptionRecord(rawValue) || rawValue
}
return rawValue.id ?? rawValue.value ?? rawValue
}
return rawValue
}
/** fieldDataList 回显:multipleSelect → 引擎值 */
export function parseMultipleSelectEchoValue(rawValue, field = {}) {
if (rawValue == null || rawValue === '') {
return undefined
}
const coerced = coerceMultipleSelectValue(rawValue)
if (!Array.isArray(coerced)) {
return coerced
}
if (field.optionsKey) {
return coerced.map(item => (isPlainObject(item) ? normalizeOptionRecord(item) || item : item))
}
return coerced.map(item =>
isPlainObject(item) ? item.id ?? item.value ?? item : item
)
}
/** fieldDataList 回显:carBrandSelect → flat carInfo */
export function parseCarBrandEchoValue(rawValue) {
if (rawValue == null || rawValue === '') {
return undefined
}
if (Array.isArray(rawValue)) {
return cascadeLevelsToCarInfo(rawValue)
}
if (typeof rawValue === 'string') {
try {
const parsed = JSON.parse(rawValue)
if (Array.isArray(parsed)) {
return cascadeLevelsToCarInfo(parsed)
}
return normalizeCarBrandValue(parsed)
} catch (error) {
return normalizeCarBrandValue(rawValue)
}
}
return normalizeCarBrandValue(rawValue)
}
/** 按 schema 字段类型将 fieldDataList 条目转为引擎值 */
export function parseFieldDataEchoValue(field = {}, coercedValue) {
if (coercedValue === undefined) {
return undefined
}
if (field.type === 'select') {
return parseSelectEchoValue(coercedValue, field)
}
if (field.type === 'multipleSelect') {
return parseMultipleSelectEchoValue(coercedValue, field)
}
if (field.type === 'carBrandSelect') {
return parseCarBrandEchoValue(coercedValue)
}
return coercedValue
}
export default {
normalizeOptionRecord,
extractSelectStorageValueFromRowField,
resolveSelectValueForValidation,
buildSelectSubmitValue,
buildMultipleSelectSubmitValue,
buildCarBrandSubmitValue,
parseSelectEchoValue,
parseMultipleSelectEchoValue,
parseCarBrandEchoValue,
parseFieldDataEchoValue
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
normalizeOptionRecord,
extractSelectStorageValueFromRowField,
resolveSelectValueForValidation,
buildSelectSubmitValue,
buildMultipleSelectSubmitValue,
buildCarBrandSubmitValue,
parseSelectEchoValue,
parseMultipleSelectEchoValue,
parseCarBrandEchoValue,
parseFieldDataEchoValue,
default: {
normalizeOptionRecord,
extractSelectStorageValueFromRowField,
buildSelectSubmitValue,
buildMultipleSelectSubmitValue,
buildCarBrandSubmitValue,
parseSelectEchoValue,
parseMultipleSelectEchoValue,
parseCarBrandEchoValue,
parseFieldDataEchoValue
}
}
}