-
Notifications
You must be signed in to change notification settings - Fork 6
/
radioButton.go
116 lines (101 loc) · 3.04 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
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
package control
import (
"context"
"github.com/goradd/goradd/pkg/bootstrap/config"
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/control"
)
type RadioButtonI interface {
control.RadioButtonI
}
type RadioButton struct {
control.RadioButton
inline bool
}
func NewRadioButton(parent page.ControlI, id string) *RadioButton {
c := &RadioButton{}
c.Init(c, parent, id)
config.LoadBootstrap(c.ParentForm())
return c
}
func (c *RadioButton) this() RadioButtonI {
return c.Self.(RadioButtonI)
}
func (c *RadioButton) SetInline(v bool) *RadioButton {
c.inline = v
return c
}
func (c *RadioButton) ΩDrawingAttributes() html.Attributes {
a := c.RadioButton.ΩDrawingAttributes()
a.SetDataAttribute("grctl", "bs-radio")
a.AddClass("form-check-input")
if c.Text() == "" {
a.AddClass("position-static")
}
return a
}
func (c *RadioButton) ΩGetDrawingLabelAttributes() html.Attributes {
a := c.RadioButton.ΩGetDrawingLabelAttributes()
a.AddClass("form-check-label")
return a
}
func (c *RadioButton) ΩDrawTag(ctx context.Context) (ctrl string) {
h := c.RadioButton.ΩDrawTag(ctx)
checkWrapperAttributes := html.NewAttributes().
AddClass("form-check").
SetDataAttribute("grel", c.ID()) // make sure the entire control gets removed
if c.inline {
checkWrapperAttributes.AddClass("form-check-inline")
}
return html.RenderTag("div", checkWrapperAttributes, h)
}
type RadioButtonCreator struct {
// ID is the id of the control
ID string
// Text is the text of the label displayed right next to the checkbox.
Text string
// Checked will initialize the checkbox in its checked state.
Checked bool
// LabelMode specifies how the label is drawn with the checkbox.
LabelMode html.LabelDrawingMode
// LabelAttributes are additional attributes placed on the label tag.
LabelAttributes html.AttributeCreator
// SaveState will save the value of the checkbox and restore it when the page is reentered.
SaveState bool
// Group is the name of the group that the button belongs to
Group string
// Inline draws the radio group inline. Specify this when drawing the
// radio button inline or in an inline FormGroup.
Inline bool
page.ControlOptions
}
// Create is called by the framework to create a new control from the Creator. You
// do not normally need to call this.
func (c RadioButtonCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewRadioButton(parent, c.ID)
if c.Text != "" {
ctrl.SetText(c.Text)
}
if c.LabelMode != html.LabelDefault {
ctrl.LabelMode = c.LabelMode
}
if c.LabelAttributes != nil {
ctrl.LabelAttributes().Merge(c.LabelAttributes)
}
if c.Group != "" {
ctrl.SetGroup(c.Group)
}
ctrl.ApplyOptions(c.ControlOptions)
if c.SaveState {
ctrl.SaveState(ctx, c.SaveState)
}
if c.Inline {
ctrl.SetInline(c.Inline)
}
return ctrl
}
// GetRadioButton is a convenience method to return the radio button with the given id from the page.
func GetRadioButton(c page.ControlI, id string) *RadioButton {
return c.Page().GetControl(id).(*RadioButton)
}