Repository navigation
Expand file tree
/
Copy pathcheckbox.go
More file actions
66 lines (53 loc) · 1.64 KB
/
Copy pathcheckbox.go
File metadata and controls
66 lines (53 loc) · 1.64 KB
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
//go:build js && wasm
package ui
import "github.com/rfwlab/rfw/v2/dom"
// CheckboxNode builds a labelled checkbox input.
type CheckboxNode struct {
root dom.Element
input dom.Element
}
// Checkbox creates a new checkbox with the given label.
func Checkbox(label string) *CheckboxNode {
InjectCSS()
doc := dom.Doc()
root := doc.CreateElement("label")
root.AddClass("rfw-ui-check")
input := doc.CreateElement("input")
input.SetAttr("type", "checkbox")
input.AddClass("rfw-ui-check__input")
root.AppendChild(input)
if label != "" {
caption := doc.CreateElement("span")
caption.AddClass("rfw-ui-check__label")
caption.SetText(label)
root.AppendChild(caption)
}
return &CheckboxNode{root: root, input: input}
}
// Element returns the wrapper element.
func (c *CheckboxNode) Element() dom.Element { return c.root }
// Control returns the inner <input> element.
func (c *CheckboxNode) Control() dom.Element { return c.input }
// Checked sets the checked state.
func (c *CheckboxNode) Checked(checked bool) *CheckboxNode {
c.input.Set("checked", checked)
return c
}
// IsChecked reports the current checked state.
func (c *CheckboxNode) IsChecked() bool { return c.input.Checked() }
// Disabled toggles the disabled state of the checkbox.
func (c *CheckboxNode) Disabled(disabled bool) *CheckboxNode {
if disabled {
c.input.SetAttr("disabled", "")
} else {
c.input.Call("removeAttribute", "disabled")
}
return c
}
// OnChange invokes fn with the checked state whenever it changes.
func (c *CheckboxNode) OnChange(fn func(bool)) *CheckboxNode {
if fn != nil {
c.input.On("change", func(dom.Event) { fn(c.input.Checked()) })
}
return c
}