-
Notifications
You must be signed in to change notification settings - Fork 6
/
checkbox.go
94 lines (81 loc) · 2.75 KB
/
checkbox.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
package button
import (
"context"
"github.com/goradd/goradd/pkg/page/action"
"github.com/goradd/goradd/pkg/page/event"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/html5tag"
)
// Checkbox is a basic html checkbox input form control.
type Checkbox struct {
CheckboxBase
}
// NewCheckbox creates a new checkbox control.
func NewCheckbox(parent page.ControlI, id string) *Checkbox {
c := &Checkbox{}
c.Self = c
c.CheckboxBase.Init(parent, id)
return c
}
// DrawingAttributes is called by the framework to set the temporary attributes that the control
// needs. Checkboxes set the grctl, name, type and value attributes automatically.
// You do not normally need to call this function.
func (c *Checkbox) DrawingAttributes(ctx context.Context) html5tag.Attributes {
a := c.CheckboxBase.DrawingAttributes(ctx)
a.SetData("grctl", "checkbox")
a.Set("name", c.ID()) // needed for posts
a.Set("type", "checkbox")
a.Set("value", "1") // required for html validity
return a
}
// UpdateFormValues is an internal call that lets us reflect the value of the checkbox on the form.
// You do not normally need to call this function.
func (c *Checkbox) UpdateFormValues(ctx context.Context) {
c.UpdateCheckboxFormValues(ctx)
}
type CheckboxCreator 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 html5tag.LabelDrawingMode
// LabelAttributes are additional attributes placed on the label tag.
LabelAttributes html5tag.Attributes
// SaveState will save the value of the checkbox and restore it when the page is reentered.
SaveState bool
// OnChange is an action to take when the user checks or unchecks the control.
OnChange action.ActionI
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 CheckboxCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewCheckbox(parent, c.ID)
if c.Text != "" {
ctrl.SetText(c.Text)
}
if c.LabelMode != html5tag.LabelDefault {
ctrl.LabelMode = c.LabelMode
}
if c.LabelAttributes != nil {
ctrl.LabelAttributes().Merge(c.LabelAttributes)
}
ctrl.ApplyOptions(ctx, c.ControlOptions)
if c.SaveState {
ctrl.SaveState(ctx, c.SaveState)
}
if c.OnChange != nil {
ctrl.On(event.Change(), c.OnChange)
}
return ctrl
}
// GetCheckbox is a convenience method to return the checkbox with the given id from the page.
func GetCheckbox(c page.ControlI, id string) *Checkbox {
return c.Page().GetControl(id).(*Checkbox)
}
func init() {
page.RegisterControl(&Checkbox{})
}