values.js
4.4 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
/** 表单 values 读写与隐藏字段清理 — @see docs/features/packageDynamicForm/ARCHITECTURE.md */
import { getVisibleList, getVisibleLeafFields } from './visibility.js'
import {
listSubforms,
listStaticGroups,
getStaticGroupChildren,
getSubformRowTemplate
} from './field-types.js'
import { resolveRadioOptionValue } from './radio-value.js'
import { resolveSelectOptionValue, normalizeSelectValuesInMap } from './select-value.js'
import { coerceMultipleSelectValue } from './multiple-select-value.js'
import { extractSubformValuesFromField } from './group-row-adapter.js'
function isMeaningfulValue(field, value) {
if (value === undefined || value === null || value === '') {
return false
}
if (field.type === 'numberInput' && (value === 0 || value === '0')) {
return false
}
if (Array.isArray(value) && value.length === 0) {
return false
}
return true
}
/** 从 schema list 提取初始 values(含 subform、group、radio/select 归一化) */
export function buildInitialValues(list = []) {
const values = {}
;(list || []).forEach(field => {
if (field.type === 'subform') {
const maps = extractSubformValuesFromField(field)
if (maps?.length) {
values[field.key] = maps
}
return
}
if (field.type === 'group') {
getStaticGroupChildren(field).forEach(child => {
if (isMeaningfulValue(child, child.value)) {
const childValue =
child.type === 'select'
? resolveSelectOptionValue(child, child.value)
: child.value
values[child.key] = childValue
}
})
return
}
if (!isMeaningfulValue(field, field.value)) {
return
}
let nextValue =
field.type === 'radio'
? resolveRadioOptionValue(field, field.value)
: field.type === 'select'
? resolveSelectOptionValue(field, field.value)
: field.type === 'multipleSelect'
? coerceMultipleSelectValue(field.value)
: field.value
if (field.type === 'multipleSelect' && (!Array.isArray(nextValue) || !nextValue.length)) {
return
}
values[field.key] = nextValue
})
return normalizeSelectValuesInMap(list, values)
}
/** @param {object} values - 当前 values
* @param {string} key - 字段 key
* @returns {*} 字段值 */
export function getFieldValue(values, key) {
return values[key]
}
/** 设置顶层字段值,空值会从 values 中删除对应 key
* @returns {object} 新的 values 对象 */
export function setFieldValue(values, key, value) {
if (
value === '' ||
value === null ||
value === undefined ||
(Array.isArray(value) && value.length === 0)
) {
const nextValues = { ...values }
delete nextValues[key]
return nextValues
}
return {
...values,
[key]: value
}
}
/** 清除不可见字段的值(含 subform 子字段与静态 group 子字段)
* @returns {object} 清理后的 values */
export function clearHiddenFieldValues(list, ctx, values) {
const visibleList = getVisibleList(list, ctx, values)
const visibleFixedKeys = new Set(
getVisibleLeafFields(list, ctx, values).map(field => field.key)
)
const visibleSubformKeys = new Set(
visibleList.filter(field => field.type === 'subform').map(field => field.key)
)
const visibleStaticGroupKeys = new Set(
visibleList.filter(field => field.type === 'group').map(field => field.key)
)
const subformChildKeys = new Set()
listSubforms(list).forEach(subform => {
getSubformRowTemplate(subform).forEach(child => {
if (child?.key) subformChildKeys.add(child.key)
})
})
const staticGroupChildKeys = new Map()
listStaticGroups(list).forEach(group => {
const keys = getStaticGroupChildren(group)
.map(child => child.key)
.filter(Boolean)
staticGroupChildKeys.set(group.key, new Set(keys))
})
const nextValues = { ...values }
Object.keys(nextValues).forEach(key => {
if (subformChildKeys.has(key)) {
delete nextValues[key]
}
})
listStaticGroups(list).forEach(group => {
const childKeys = staticGroupChildKeys.get(group.key)
if (!childKeys) return
childKeys.forEach(key => {
if (!visibleFixedKeys.has(key)) {
delete nextValues[key]
}
})
if (!visibleStaticGroupKeys.has(group.key) && Array.isArray(nextValues[group.key])) {
delete nextValues[group.key]
}
})
;(list || []).forEach(field => {
if (field.type === 'subform' && !visibleSubformKeys.has(field.key)) {
delete nextValues[field.key]
} else if (field.type !== 'group' && field.type !== 'subform' && !visibleFixedKeys.has(field.key)) {
delete nextValues[field.key]
}
})
return nextValues
}