-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoggle.go
135 lines (119 loc) · 3 KB
/
toggle.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package components
import (
"github.com/adrianolmedo/components/alpine"
lucide "github.com/eduardolat/gomponents-lucide"
"github.com/google/uuid"
"github.com/maragudk/gomponents"
"github.com/maragudk/gomponents/html"
)
type ToggleParams struct {
// Color can be primary, secondary, neutral, info, success, warning, error.
// Default is neutral.
Color string
// Size can be sm, md, lg. Default is md.
Size string
// Name is the name of the input.
Name string
// UnCheckedValue is the value when the toggle is off. Default is "off".
UnCheckedValue string
// CheckedValue is the value when the toggle is on. Default is "on".
CheckedValue string
// Value is the initial value of the toggle. Default is UnCheckedValue.
Value string
// ID is the id of the input. Default is random.
ID string
// InputChildren are the children of the input.
InputChildren []gomponents.Node
}
func Toggle(params ToggleParams) gomponents.Node {
color := "neutral"
size := "md"
name := params.Name
uncheckedValue := params.UnCheckedValue
checkedValue := params.CheckedValue
if params.Color != "" {
color = params.Color
}
if params.Size != "" {
size = params.Size
}
if uncheckedValue == "" {
uncheckedValue = "off"
}
if checkedValue == "" {
checkedValue = "on"
}
value := uncheckedValue
if params.Value == checkedValue {
value = checkedValue
}
id := params.ID
if params.ID == "" {
id = "toggle-" + uuid.NewString()
}
iconClasses := Classes{
"w-8 h-8": size == "sm",
"w-10 h-10": size == "md",
"w-14 h-14": size == "lg",
"text-primary": color == "primary",
"text-secondary": color == "secondary",
"text-neutral": color == "neutral",
"text-info": color == "info",
"text-success": color == "success",
"text-warning": color == "warning",
"text-error": color == "error",
}
return html.Div(
html.Class("inline-block select-none"),
html.Input(
html.Class("hidden"),
html.Type("hidden"),
html.Name(name),
html.ID(id),
gomponents.Group(params.InputChildren),
),
html.Div(
alpine.XData(`{
value: "`+value+`",
uncheckedValue: "`+uncheckedValue+`",
checkedValue: "`+checkedValue+`",
isChecked: false,
setChecked() {
this.isChecked = this.value === this.checkedValue
},
changeValue() {
if (this.isChecked) {
this.value = this.uncheckedValue
} else {
this.value = this.checkedValue
}
},
setInputValue() {
document.getElementById("`+id+`").value = this.value
document.getElementById("`+id+`").dispatchEvent(new Event("change"))
},
toggle() {
this.changeValue()
this.setChecked()
this.setInputValue()
},
init() {
this.setChecked()
this.setInputValue()
}
}`),
html.Div(
html.Class("cursor-pointer"),
alpine.XOn("click", "toggle()"),
alpine.TemplateEl(
alpine.XIf("!isChecked"),
lucide.ToggleLeft(iconClasses),
),
alpine.TemplateEl(
alpine.XIf("isChecked"),
lucide.ToggleRight(iconClasses),
),
),
),
)
}