-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.go
71 lines (60 loc) · 1.94 KB
/
element.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
package dom
import "syscall/js"
// CreateElement returns a new element.
func CreateElement(document js.Value, tagName string, attrs map[string]interface{}) js.Value {
element := document.Call("createElement", tagName)
for key, value := range attrs {
element.Set(key, value)
}
return element
}
// Div returns a new div element.
func Div(document js.Value, attributes map[string]interface{}) js.Value {
return CreateElement(document, "div", attributes)
}
// Input returns a new input element.
func Input(document js.Value, inputType string, attributes map[string]interface{}) js.Value {
attributes["type"] = inputType
return CreateElement(document, "input", attributes)
}
// Button returns a new button element.
func Button(document js.Value, label string) js.Value {
return CreateElement(document, "button", map[string]interface{}{
"type": "button",
"innerHTML": label,
})
}
// Label returns a new label element.
func Label(document js.Value, label, labelFor string) js.Value {
return CreateElement(document, "label", map[string]interface{}{
"innerHTML": label,
"htmlFor": labelFor,
})
}
// P returns a new p element.
func P(document js.Value, text string) js.Value {
return CreateElement(document, "p", map[string]interface{}{
"innerHTML": text,
})
}
// Form returns a new form element.
func Form(document js.Value, attributes map[string]interface{}) js.Value {
return CreateElement(document, "form", attributes)
}
// H1 returns a new h1 element.
func H1(document js.Value, text string) js.Value {
return CreateElement(document, "h1", map[string]interface{}{
"innerHTML": text,
})
}
// Span returns a new span element.
func Span(document js.Value, text, classList string) js.Value {
return CreateElement(document, "span", map[string]interface{}{
"innerHTML": text,
"classList": classList,
})
}
// TextNode returns a new text node.
func TextNode(document js.Value, text string) js.Value {
return document.Call("createTextNode", text)
}