-
Notifications
You must be signed in to change notification settings - Fork 1
/
radio.go
81 lines (61 loc) · 1.78 KB
/
radio.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
79
80
81
package widget
import (
"strings"
"github.com/benpate/form"
"github.com/benpate/html"
"github.com/benpate/rosetta/schema"
)
type Radio struct{}
func (widget Radio) View(element *form.Element, s *schema.Schema, provider form.LookupProvider, value any, b *html.Builder) error {
// find the path and schema to use
schemaElement := element.GetSchema(s)
valueString := element.GetString(value, s)
lookupCodes, _ := form.GetLookupCodes(element, schemaElement, provider)
// Start building a new tag
b.Div().Class("layout-value")
for _, lookupCode := range lookupCodes {
if lookupCode.Value == valueString {
b.WriteString(lookupCode.Label)
break
}
}
b.Close()
return nil
}
func (widget Radio) Edit(element *form.Element, s *schema.Schema, provider form.LookupProvider, value any, b *html.Builder) error {
if element.ReadOnly {
return Radio{}.View(element, s, provider, value, b)
}
// Calculate the element's ID
id := element.ID
if id == "" {
id = "radio-" + strings.ReplaceAll(element.Path, ".", "-")
}
// find the path and schema to use
schemaElement := element.GetSchema(s)
valueString := element.GetString(value, s)
lookupCodes, _ := form.GetLookupCodes(element, schemaElement, provider)
// Start building a new tag
for _, lookupCode := range lookupCodes {
radioID := id + "-" + lookupCode.Value
b.Label(radioID)
radio := b.Input("radio", element.Path).
ID(radioID).
Value(lookupCode.Value)
if lookupCode.Value == valueString {
radio.Attr("checked", "true")
}
radio.InnerText(lookupCode.Label).Close()
b.CloseAll()
}
return nil
}
/***********************************
* Wiget Metadata
***********************************/
func (widget Radio) ShowLabels() bool {
return true
}
func (widget Radio) Encoding(_ *form.Element) string {
return ""
}