values.js
3.06 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
import { getVisibleList, getVisibleLeafFields } from './visibility.js'
import {
listSubforms,
listStaticGroups,
getStaticGroupChildren,
getSubformRowTemplate
} from './field-types.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
}
export function buildInitialValues(list = []) {
const values = {}
;(list || []).forEach(field => {
if (field.type === 'subform') {
if (Array.isArray(field.value) && field.value.length) {
values[field.key] = JSON.parse(JSON.stringify(field.value))
}
return
}
if (field.type === 'group') {
getStaticGroupChildren(field).forEach(child => {
if (isMeaningfulValue(child, child.value)) {
values[child.key] = child.value
}
})
return
}
if (!isMeaningfulValue(field, field.value)) {
return
}
values[field.key] = field.value
})
return values
}
export function getFieldValue(values, key) {
return values[key]
}
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
}
}
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
}