-
Notifications
You must be signed in to change notification settings - Fork 6
/
radioButton.go
72 lines (60 loc) · 1.58 KB
/
radioButton.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
package control
import (
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/page"
)
type RadioButtonI interface {
CheckboxI
}
// RadioButton is a standard html radio button. You can optionally specify a group name for the radiobutton to belong
// to and the browser will make sure only one item in the group is selected.
type RadioButton struct {
CheckboxBase
group string
}
func NewRadioButton(parent page.ControlI, id string) *RadioButton {
c := &RadioButton{}
c.Init(c, parent, id)
return c
}
func (c *RadioButton) this() RadioButtonI {
return c.Self.(RadioButtonI)
}
func (c *RadioButton) DrawingAttributes() *html.Attributes {
a := c.CheckboxBase.DrawingAttributes()
a.SetDataAttribute("grctl", "radio")
a.Set("type", "radio")
if c.group == "" {
a.Set("name", c.ID()) // treat it like a checkbox if no group is specified
} else {
a.Set("name", c.group)
a.Set("value", c.ID())
}
return a
}
func (c *RadioButton) UpdateFormValues(ctx *page.Context) {
id := c.ID()
if v, ok := ctx.CheckableValue(id); ok {
c.SetCheckedNoRefresh(v)
}
}
func (c *RadioButton) SetGroup(g string) RadioButtonI {
c.group = g
c.Refresh()
return c.this()
}
func (c *RadioButton) Group() string {
return c.group
}
func (c *RadioButton) SetChecked(v bool) RadioButtonI {
if c.group != "" && v {
if c.Checked() != v {
c.SetCheckedNoRefresh(v)
// make sure any other buttons in the group are unchecked
c.ParentForm().Response().ExecuteJsFunction("goradd.setRadioInGroup", page.PriorityStandard, c.ID())
}
} else {
c.CheckboxBase.SetChecked(v)
}
return c.this()
}