-
Notifications
You must be signed in to change notification settings - Fork 0
/
choicefieldoption.go
78 lines (70 loc) · 2.2 KB
/
choicefieldoption.go
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
package aform
import (
"golang.org/x/exp/slices"
)
func fieldGroupsToWidgetGroups(groups []choiceFieldOptionGroup, optionWidget Widget, baseID, htmlName string, selected []string) []map[string][]widgetOption {
output := make([]map[string][]widgetOption, 0, len(groups))
groupIndex := uint(0)
for _, group := range groups {
groupWidget := group.widget(optionWidget, baseID, htmlName, groupIndex, selected)
groupIndex += indexesUsedByGroup(groupWidget)
output = append(output, groupWidget)
}
return output
}
func indexesUsedByGroup(group map[string][]widgetOption) uint {
output := uint(0)
for name, options := range group {
if len(name) > 0 {
output += 1
} else {
output += uint(len(options))
}
}
return output
}
type choiceFieldOptionGroup map[string][]ChoiceFieldOption
type ChoiceFieldOption struct {
Value string
Label string
}
func (g choiceFieldOptionGroup) values() []string {
var values []string
for _, options := range g {
for _, option := range options {
values = append(values, option.Value)
}
}
return values
}
func (g choiceFieldOptionGroup) widget(inputWidget Widget, baseID, htmlName string, groupIndex uint, selected []string) map[string][]widgetOption {
output := map[string][]widgetOption{}
for groupName, groupOptions := range g {
widgetOptions := make([]widgetOption, len(groupOptions))
withID := len(baseID) > 0
isSubGroup := len(groupName) > 0
for i, option := range groupOptions {
attrs := map[string]string{}
if withID {
if isSubGroup {
attrs["id"] = normalizedSubGroupID(baseID, groupIndex, uint(i))
} else {
attrs["id"] = normalizedGroupID(baseID, groupIndex+uint(i))
}
}
widgetOptions[i] = option.widget(inputWidget, htmlName, slices.Contains(selected, option.Value), attrs)
}
output[groupName] = widgetOptions
}
return output
}
func (o ChoiceFieldOption) widget(inputWidget Widget, htmlName string, selected bool, attrs map[string]string) widgetOption {
if selectedAttr, ok := inputWidget.selectedAttr(selected); ok {
attrs[selectedAttr.n] = selectedAttr.v
}
return widgetOption{
Label: o.Label,
WrapLabel: true,
widgetInput: widgetInput{Type: inputWidget, Name: htmlName, Value: o.Value, Attrs: attrs},
}
}