-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentswitcher.go
65 lines (55 loc) · 1.24 KB
/
contentswitcher.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
package carbon
import (
"io"
"slices"
)
type contentSwitcher struct {
children []*switch_
attrs []Attr
selectedIndex int8
size string
}
var _ Component = (*contentSwitcher)(nil)
func ContentSwitcher(cs ...*switch_) *contentSwitcher {
return &contentSwitcher{
children: cs,
attrs: nil,
selectedIndex: 0,
size: "",
}
}
func (c *contentSwitcher) Attr(name string, value string) *contentSwitcher {
c.attrs = append(c.attrs, Attr{name, value})
return c
}
func (c *contentSwitcher) SelectedIndex(selectedIndex int8) *contentSwitcher {
c.selectedIndex = selectedIndex
return c
}
func (c *contentSwitcher) Size(size string) *contentSwitcher {
var sizes = []string{"", "sm", "xl"}
if !slices.Contains(sizes, size) {
panic("Size() received an invalid value")
}
c.size = size
return c
}
func (c *contentSwitcher) Render(w io.Writer) {
w.Write([]byte(`<div role="tablist" class="bx--content-switcher`))
if c.size != "" {
w.Write([]byte(` bx--content-switcher--`))
io.WriteString(w, c.size)
}
w.Write([]byte(`">`))
{
for i, child := range c.children {
if c.selectedIndex == int8(i) {
child.selected = true
} else {
child.selected = false
}
child.Render(w)
}
}
w.Write([]byte(`</div>`))
}