-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtransformer_radio_struct.go
More file actions
73 lines (66 loc) · 2.09 KB
/
Copy pathtransformer_radio_struct.go
File metadata and controls
73 lines (66 loc) · 2.09 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
package form
import (
"github.com/donseba/go-form/v2/types"
)
// collapseStructRadioGroups inspects group fields and, when a parent group declared as
// `form:"radios,radio_group"` contains child fields that represent struct-based radio options
// (Type=radios, InputType=radio_struct) sharing the same Name, it:
// - converts the parent group into a FieldTypeRadios group (InputType=radio_group)
// - populates parent.Values from the child fields
// - sets parent.Value to the selected option (first checked)
// - clears parent.Fields (so we don't render nested wrappers/labels)
func collapseStructRadioGroups(fields []types.FormField) []types.FormField {
for i := range fields {
// Recurse first
if len(fields[i].Fields) > 0 {
fields[i].Fields = collapseStructRadioGroups(fields[i].Fields)
}
if fields[i].Type != types.FieldTypeGroup {
continue
}
// Explicit opt-in: parent must be declared as radios + radio_group.
if !(fields[i].Type == types.FieldTypeGroup && fields[i].InputType == types.InputFieldTypeRadioGroup) {
continue
}
if len(fields[i].Fields) == 0 {
continue
}
// Detect: all children are struct-radio options and share the same Name.
var radioName string
allRadio := true
for _, ch := range fields[i].Fields {
if ch.Type != types.FieldTypeRadios || ch.InputType != types.InputFieldTypeRadioStruct {
allRadio = false
break
}
if radioName == "" {
radioName = ch.Name
} else if ch.Name != radioName {
allRadio = false
break
}
}
if !allRadio || radioName == "" {
continue
}
// Collapse into a single radio group.
fields[i].Type = types.FieldTypeRadios
fields[i].InputType = types.InputFieldTypeRadioGroup
fields[i].Name = radioName
fields[i].Id = radioName
fields[i].Values = nil
fields[i].Value = ""
for _, ch := range fields[i].Fields {
fields[i].Values = append(fields[i].Values, types.FieldValue{
Value: ch.Id,
Name: ch.Label,
Disabled: ch.Disabled,
})
if s, ok := ch.Value.(string); ok && s != "" {
fields[i].Value = ch.Id
}
}
fields[i].Fields = nil
}
return fields
}