-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
121 lines (98 loc) · 2.47 KB
/
input.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
116
117
118
119
120
121
package compton
import (
_ "embed"
"github.com/boggydigital/compton/consts/attr"
"github.com/boggydigital/compton/consts/class"
"github.com/boggydigital/compton/consts/compton_atoms"
"github.com/boggydigital/compton/consts/font_weight"
"github.com/boggydigital/compton/consts/input_types"
"golang.org/x/exp/maps"
"golang.org/x/net/html/atom"
"slices"
"strconv"
"time"
)
const (
rnInput = "input"
rnDatalistPfx = "datalist-"
)
var (
//go:embed "style/input.css"
styleInputs []byte
)
type InputElement struct {
*BaseElement
r Registrar
it input_types.Type
dataList Element
}
func (ie *InputElement) SetPlaceholder(placeholder string) *InputElement {
ie.SetAttribute("placeholder", placeholder)
return ie
}
func (ie *InputElement) SetName(name string) *InputElement {
ie.SetAttribute("name", name)
return ie
}
func (ie *InputElement) SetValue(value string) *InputElement {
ie.SetAttribute("value", value)
return ie
}
func (ie *InputElement) SetChecked(condition bool) *InputElement {
if condition {
ie.SetAttribute("checked", "")
}
return ie
}
func (ie *InputElement) SetDisabled(condition bool) *InputElement {
if condition {
ie.SetAttribute("disabled", "")
}
return ie
}
func (ie *InputElement) SetDatalist(list map[string]string, listId string) *InputElement {
if listId == "" {
listId = ie.GetAttribute(attr.Id)
if listId == "" {
listId = strconv.FormatInt(time.Now().Unix(), 10)
}
listId += "-list"
}
if len(list) > 0 {
dataList := Datalist(listId)
values := maps.Keys(list)
slices.Sort(values)
for _, value := range values {
dataList.Append(Option(value, list[value]))
}
ie.dataList = dataList
}
ie.SetAttribute(attr.List, listId)
ie.r.RegisterDeferrals(rnDatalistPfx+listId, ie.dataList)
return ie
}
func (ie *InputElement) FontWeight(w font_weight.Weight) *InputElement {
ie.AddClass(class.FontWeight(w))
return ie
}
func Input(r Registrar, it input_types.Type) *InputElement {
input := &InputElement{
BaseElement: NewElement(tacMarkup(atom.Input)),
r: r,
it: it,
}
input.SetAttribute(attr.Type, it.String())
r.RegisterStyles(DefaultStyle,
compton_atoms.StyleName(atom.Input))
return input
}
func InputValue(r Registrar, it input_types.Type, value string) *InputElement {
input := Input(r, it)
input.SetAttribute(attr.Value, value)
return input
}
func Switch(r Registrar) *InputElement {
input := Input(r, input_types.Checkbox)
input.SetAttribute("switch", "")
return input
}