-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccordionitem.go
102 lines (88 loc) · 2.2 KB
/
accordionitem.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
package carbon
import "io"
type accordionItem struct {
children any
attrs []Attr
title any
open bool
disabled bool
iconDescription string
}
var _ Component = (*accordionItem)(nil)
func AccordionItem() *accordionItem {
return &accordionItem{
children: nil,
attrs: nil,
title: "title",
open: false,
disabled: false,
iconDescription: "Expand/Collapse",
}
}
func (a *accordionItem) Content(content ...any) *accordionItem {
a.children = content
return a
}
func (a *accordionItem) Attr(name string, value string) *accordionItem {
a.attrs = append(a.attrs, Attr{name, value})
return a
}
func (a *accordionItem) Title(title ...any) *accordionItem {
a.title = title
return a
}
func (a *accordionItem) TitleComponent(cs ...Component) *accordionItem {
a.title = cs
return a
}
func (a *accordionItem) Open(open bool) *accordionItem {
a.open = open
return a
}
func (a *accordionItem) Disabled(disabled bool) *accordionItem {
a.disabled = disabled
return a
}
func (a *accordionItem) IconDescription(iconDescription string) *accordionItem {
a.iconDescription = iconDescription
return a
}
func (a *accordionItem) Render(w io.Writer) {
w.Write([]byte(`<li class="bx--accordion__item`))
if a.open {
w.Write([]byte(` bx--accordion__item--active`))
}
if a.disabled {
w.Write([]byte(` bx--accordion__item--disabled`))
}
w.Write([]byte(`">`))
{
w.Write([]byte(`<button type="button" class="bx--accordion__heading"`))
w.Write([]byte(` title="`))
io.WriteString(w, a.iconDescription)
w.Write([]byte(`"`))
w.Write([]byte(` aria-expanded="`))
w.Write(ternary(a.open, []byte(`true`), []byte(`false`)))
w.Write([]byte(`"`))
if a.disabled {
w.Write([]byte(` disabled`))
}
w.Write([]byte(`>`))
{
ChevronRight().Attr("class", "bx--accordion__arrow").Attr("aria-label", a.iconDescription).Render(w)
w.Write([]byte(`<div class="bx--accordion__title">`))
{
renderAny(w, a.title)
}
w.Write([]byte(`</div>`))
}
renderAttrs(w, a.attrs)
w.Write([]byte(`</button>`))
w.Write([]byte(`<div class="bx--accordion__content">`))
{
renderAny(w, a.children)
}
w.Write([]byte(`</div>`))
}
w.Write([]byte(`</li>`))
}