This repository was archived by the owner on Aug 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaccordion.templ
More file actions
74 lines (63 loc) · 1.3 KB
/
accordion.templ
File metadata and controls
74 lines (63 loc) · 1.3 KB
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
package templdais
import "strings"
import "fmt"
type AccordionAttrs struct {
Items []AccordionItem
Name string
Class string
Arrow bool
PlusMinus bool
}
type AccordionItem struct {
Title string
Content templ.Component
Open bool
Class string
}
func formatName(name string, index int) string {
return fmt.Sprintf("%s-%d", strings.ToLower(name), index)
}
func (acc AccordionItem) GetClassName() string {
var class = "collapse bg-base-200"
if acc.Class != "" {
class += " " + acc.Class
}
return trimSpaces(class)
}
func (acc *AccordionAttrs) GetClassName() string {
if acc.Arrow {
for i := range acc.Items {
acc.Items[i].Class += " collapse-arrow"
}
}
if acc.PlusMinus {
for i := range acc.Items {
acc.Items[i].Class += " collapse-plus-minus"
}
}
return trimSpaces(acc.Class)
}
templ Accordion(acc AccordionAttrs) {
<div class={ acc.GetClassName() }>
for i, item := range acc.Items {
<div
class={ item.GetClassName() }
>
<input
type="radio"
name={ formatName(acc.Name, 1) }
id={ formatName(acc.Name, i) }
if item.Open {
checked="checked"
}
/>
<div class="collapse-title text-xl font-medium">
{ item.Title }
</div>
<div class="collapse-content">
@item.Content
</div>
</div>
}
</div>
}