-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect.go
95 lines (72 loc) · 1.77 KB
/
select.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
package dhtml
import "github.com/mitoteam/mttools"
type SelectElement struct {
tag *Tag
options []*OptionElement
}
// force interface implementation declaring fake variable
var _ ElementI = (*SelectElement)(nil)
func NewSelect() *SelectElement {
return &SelectElement{
tag: NewTag("select"),
}
}
func (c *SelectElement) Class(v any) *SelectElement {
c.tag.Class(v)
return c
}
func (c *SelectElement) Id(id string) *SelectElement {
c.tag.Id(id)
return c
}
func (c *SelectElement) Attribute(name, value string) *SelectElement {
c.tag.Attribute(name, value)
return c
}
func (c *SelectElement) Option(value any, body any) *OptionElement {
o := NewOption().Value(value).Body(body)
c.AppendOption(o)
return o
}
func (c *SelectElement) AppendOption(option *OptionElement) *SelectElement {
c.options = append(c.options, option)
return c
}
func (c *SelectElement) GetTags() TagList {
for _, option := range c.options {
c.tag.Append(option)
}
return c.tag.GetTags()
}
// ==================== OptionElement ===================
type OptionElement struct {
value string
selected bool
body HtmlPiece
}
// force interface implementation declaring fake variable
var _ ElementI = (*OptionElement)(nil)
func NewOption() *OptionElement {
return &OptionElement{}
}
func (c *OptionElement) Value(v any) *OptionElement {
c.value = mttools.AnyToString(v)
return c
}
func (c *OptionElement) Body(v any) *OptionElement {
c.body.Append(v)
return c
}
func (c *OptionElement) Selected(b bool) *OptionElement {
c.selected = b
return c
}
// region Rendering
func (c *OptionElement) GetTags() TagList {
selectTag := NewTag("option").Append(c.body)
selectTag.Attribute("value", c.value)
if c.selected {
selectTag.Attribute("selected", "")
}
return selectTag.GetTags()
}