values.js
4.88 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
/** 表单 values 读写与隐藏字段清理 — @see packageDynamicForm/docs/ARCHITECTURE.md */
import { getVisibleList, resolveEffectiveRowFields } from './visibility.js'
import { filterVisibleRowFields } from './group-visibility.js'
import {
listSubforms,
getStaticGroupChildren,
getSubformRowTemplate,
getSubformRowTemplateForAdd
} from './field-types.js'
import { getGroupRows } from './group-values.js'
import { normalizeFieldValueByType } from './field-types/registry.js'
import { 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 (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)) {
values[child.key] = normalizeFieldValueByType(child, child.value, 'initial')
}
})
return
}
if (!isMeaningfulValue(field, field.value)) {
return
}
let nextValue =
field.type === 'multipleSelect'
? coerceMultipleSelectValue(field.value)
: normalizeFieldValueByType(field, field.value, 'initial')
if (field.type === 'multipleSelect' && (!Array.isArray(nextValue) || !nextValue.length)) {
return
}
values[field.key] = nextValue
})
return normalizeSelectValuesInMap(list, values)
}
/** strip 前合并 schema 模板默认(含 numberInput 0);详情 initialValues 优先 */
export function mergeTemplateDefaultsIntoInitialValues(schemaList = [], initialValues = {}) {
const templateDefaults = buildInitialValues(schemaList)
return { ...templateDefaults, ...initialValues }
}
/** 用详情回显 initialValues 覆盖 buildInitialValues 结果(schema 不带 .value) */
export function applyInitialValues(values = {}, initialValues = {}) {
if (!initialValues || typeof initialValues !== 'object') {
return values
}
const next = { ...values }
Object.keys(initialValues).forEach(key => {
if (initialValues[key] !== undefined) {
next[key] = initialValues[key]
}
})
return next
}
/** @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 列 key;模版级隐藏字段保留 values 供 jsonContent 回显 */
export function clearHiddenFieldValues(list, ctx, values) {
const subformChildKeys = new Set()
listSubforms(list).forEach(subform => {
getSubformRowTemplate(subform).forEach(child => {
if (child?.key) subformChildKeys.add(child.key)
})
})
const nextValues = { ...values }
Object.keys(nextValues).forEach(key => {
if (subformChildKeys.has(key)) {
delete nextValues[key]
}
})
return nextValues
}
/** 清除 subform 行内 childrenShowRules 不可见 cell 的值(联动切换后清 hidden) */
export function clearHiddenSubformRowValues(list, ctx, values) {
const visibleSubformKeys = new Set(
getVisibleList(list, ctx, values)
.filter(field => field.type === 'subform')
.map(field => field.key)
)
const nextValues = { ...values }
listSubforms(list).forEach(group => {
const groupKey = group.key
if (!groupKey || !visibleSubformKeys.has(groupKey)) {
return
}
const template = getSubformRowTemplateForAdd(group)
if (!template.length) {
return
}
const rows = getGroupRows(nextValues, groupKey)
if (!rows.length) {
return
}
let changed = false
const nextRows = rows.map(rowMap => {
const rowFields = resolveEffectiveRowFields(group, [], rowMap)
const visibleKeys = new Set(
filterVisibleRowFields(group, rowFields, rowMap).map(field => field.key)
)
const nextRow = { ...rowMap }
template.forEach(child => {
if (!child?.key || visibleKeys.has(child.key)) {
return
}
if (nextRow[child.key] !== undefined) {
delete nextRow[child.key]
changed = true
}
})
return nextRow
})
if (changed) {
nextValues[groupKey] = nextRows
}
})
return nextValues
}