-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckbox.go
177 lines (156 loc) · 4.94 KB
/
checkbox.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package components
import (
"github.com/adrianolmedo/components/randutil"
"github.com/maragudk/gomponents"
"github.com/maragudk/gomponents/components"
"github.com/maragudk/gomponents/html"
)
type CheckboxProps struct {
// Classes is the additional class name(s) for the input.
Classes string
// 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
// Children are the contents of the input.
Children []gomponents.Node
}
func Checkbox(props CheckboxProps) gomponents.Node {
color := "neutral"
if props.Color != "" {
color = props.Color
}
size := "md"
if props.Size != "" {
size = props.Size
}
return html.Input(
html.Type("checkbox"),
components.Classes{
"rounded bg-base-100 cursor-pointer": true,
"focus:ring focus:ring-opacity-50 focus:ring-offset-0": true,
"w-4 h-4": size == "sm",
"w-6 h-6": size == "md",
"w-9 h-9": size == "lg",
"focus:ring-primary text-primary": color == "primary",
"focus:ring-secondary text-secondary": color == "secondary",
"focus:ring-neutral text-neutral": color == "neutral",
"focus:ring-info text-info": color == "info",
"focus:ring-success text-success": color == "success",
"focus:ring-warning text-warning": color == "warning",
"focus:ring-error text-error": color == "error",
props.Classes: props.Classes != "",
},
gomponents.If(
props.Children != nil,
gomponents.Group(props.Children),
),
)
}
// CheckboxControlProps is the properties for the CheckboxControl component.
type CheckboxControlProps struct {
// ID is the HTML id attribute value for the checkbox control.
ID string
// Name is the HTML name attribute value for the checkbox control.
Name string
// Title is the text label associated with the checkbox control.
Title string
// TitleNodes is the text label associated with the checkbox control.
// Use this instead of Title if you need to include custom HTML.
TitleNodes []gomponents.Node
// HelpText is additional text displayed below the checkbox, usually providing guidance.
HelpText string
// Color is the color class applied to the checkbox control for styling.
Color string
// Align is the alignment of the checkbox control. Can be left (default) or right.
Align string
// Required, when true, indicates that the checkbox is mandatory for form submission.
Required bool
// Error, when true, applies error styling to the checkbox control.
Error bool
// CheckboxChildren are additional HTML nodes to be included inside the checkbox element.
CheckboxChildren []gomponents.Node
// CheckboxClasses are additional CSS classes to be applied to the checkbox element.
CheckboxClasses string
// ContainerClasses are additional CSS classes to be applied to the checkbox control container.
ContainerClasses string
}
// CheckboxControl is a component that renders a group of nodes
// that are used to present a form checkbox control.
func CheckboxControl(props CheckboxControlProps) gomponents.Node {
id := props.ID
if id == "" {
id = randutil.UUIDStrWithoutDashes()
}
hasHelperText := props.HelpText != ""
describedById := id + "-help-text"
color := props.Color
if props.Error {
color = "error"
}
align := "left"
if props.Align != "" {
align = props.Align
}
checkbox := Checkbox(CheckboxProps{
Color: color,
Classes: props.CheckboxClasses,
Children: []gomponents.Node{
html.ID(id),
html.Name(props.Name),
gomponents.If(props.Required, html.Required()),
gomponents.If(hasHelperText, html.Aria("describedby", describedById)),
gomponents.Group(props.CheckboxChildren),
},
})
return html.Div(
components.Classes{
"checkbox-control-wrapper": true,
"flex items-center": true,
"justify-start": align == "left",
"justify-end": align == "right",
},
html.Label(
components.Classes{
"checkbox-control-container": true,
"space-y-1": true,
props.ContainerClasses: props.ContainerClasses != "",
},
html.For(id),
html.Div(
html.Class("flex justify-start items-center space-x-2"),
gomponents.If(align == "left", checkbox),
html.Span(
components.Classes{
"checkbox-control-label block": true,
"text-error": props.Error,
},
gomponents.If(
len(props.TitleNodes) > 0,
gomponents.Group(props.TitleNodes),
),
gomponents.If(
props.Title != "",
gomponents.Text(props.Title),
),
gomponents.If(props.Required, html.Span(
html.Class("text-error"),
gomponents.Text(" *"),
)),
),
gomponents.If(align == "right", checkbox),
),
gomponents.If(hasHelperText,
html.Span(
components.Classes{
"checkbox-control-help-text block text-sm": true,
"text-error": props.Error,
},
html.ID(describedById),
gomponents.Text(props.HelpText),
),
),
),
)
}