-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect.go
311 lines (282 loc) · 6.57 KB
/
select.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package carbon
import (
"io"
"slices"
)
type select_ struct {
children []*selectItem
attrs []Attr
selected string
size string
inline bool
light bool
disabled bool
name string
invalid bool
invalidText string
warn bool
warnText string
helperText string
noLabel bool
labelText string
hideLabel bool
required bool
}
var _ Component = (*select_)(nil)
func Select(s ...*selectItem) *select_ {
return &select_{
children: s,
attrs: nil,
selected: "",
size: "",
inline: false,
light: false,
disabled: false,
name: "",
invalid: false,
invalidText: "",
warn: false,
warnText: "",
helperText: "",
noLabel: false,
labelText: "",
hideLabel: false,
required: false,
}
}
func (s *select_) Attr(name string, value string) *select_ {
s.attrs = append(s.attrs, Attr{name, value})
return s
}
func (s *select_) Selected(selected string) *select_ {
s.selected = selected
return s
}
func (s *select_) Size(size string) *select_ {
var sizes = []string{"", "sm", "xl"}
if !slices.Contains(sizes, size) {
panic("invalid size")
}
s.size = size
return s
}
func (s *select_) Inline(inline bool) *select_ {
s.inline = inline
return s
}
func (s *select_) Light(light bool) *select_ {
s.light = light
return s
}
func (s *select_) Disabled(disabled bool) *select_ {
s.disabled = disabled
return s
}
func (s *select_) Name(name string) *select_ {
s.name = name
return s
}
func (s *select_) Invalid(invalid bool) *select_ {
s.invalid = invalid
return s
}
func (s *select_) InvalidText(invalidText string) *select_ {
s.invalidText = invalidText
return s
}
func (s *select_) Warn(warn bool) *select_ {
s.warn = warn
return s
}
func (s *select_) WarnText(warnText string) *select_ {
s.warnText = warnText
return s
}
func (s *select_) HelperText(helperText string) *select_ {
s.helperText = helperText
return s
}
func (s *select_) NoLabel(noLabel bool) *select_ {
s.noLabel = noLabel
return s
}
func (s *select_) LabelText(labelText string) *select_ {
s.labelText = labelText
return s
}
func (s *select_) HideLabel(hideLabel bool) *select_ {
s.hideLabel = hideLabel
return s
}
func (s *select_) Required(required bool) *select_ {
s.required = required
return s
}
func (s *select_) Render(w io.Writer) {
id := "ccs-" + useId()
errorId := `error-` + id
renderSelect := renderSelectClosure(s.invalid, s.disabled, s.required, errorId, id, s.name, s.size, s.selected, s.attrs, s.children)
w.Write([]byte(`<div class="bx--form-item"><div class="bx--select`))
if s.inline {
w.Write([]byte(` bx--select--inline`))
}
if s.light {
w.Write([]byte(` bx--select--light`))
}
if s.invalid {
w.Write([]byte(` bx--select--invalid`))
}
if s.disabled {
w.Write([]byte(` bx--select--disabled`))
}
if s.warn {
w.Write([]byte(` bx--select--warning`))
}
w.Write([]byte(`">`))
{
if !s.noLabel {
w.Write([]byte(`<label for="`))
io.WriteString(w, id)
w.Write([]byte(`" class="bx--label`))
if s.hideLabel {
w.Write([]byte(` bx--visually-hidden`))
}
if s.disabled {
w.Write([]byte(` bx--label--disabled`))
}
w.Write([]byte(`">`))
{
io.WriteString(w, s.labelText)
}
w.Write([]byte(`</label>`))
}
if s.inline {
w.Write([]byte(`<div class="bx--select-input--inline__wrapper">`))
{
w.Write([]byte(`<div class="bx--select-input__wrapper"`))
if s.invalid {
w.Write([]byte(` data-invalid`))
}
w.Write([]byte(`>`))
{
renderSelect(w)
ChevronDown().Attr("class", "bx--select__arrow").Render(w)
if s.invalid {
WarningFilled().Attr("class", "bx--select__invalid-icon").Render(w)
}
}
w.Write([]byte(`</div>`))
if s.invalid {
w.Write([]byte(`<div class="bx--form-requirement" id="`))
io.WriteString(w, errorId)
w.Write([]byte(`">`))
{
io.WriteString(w, s.invalidText)
}
w.Write([]byte(`</div>`))
}
w.Write([]byte(`</div>`))
if !s.invalid && !s.warn && s.helperText != "" {
w.Write([]byte(`<div class="bx--form__helper-text`))
if s.disabled {
w.Write([]byte(` bx--form__helper-text--disabled`))
}
w.Write([]byte(`">`))
{
io.WriteString(w, s.helperText)
}
w.Write([]byte(`</div>`))
}
}
w.Write([]byte(`</div>`))
}
if !s.inline {
w.Write([]byte(`<div class="bx--select-input__wrapper"`))
if s.invalid {
w.Write([]byte(` data-invalid`))
}
w.Write([]byte(`>`))
{
renderSelect(w)
ChevronDown().Attr("class", "bx--select__arrow").Render(w)
if s.invalid {
WarningFilled().Attr("class", "bx--select__invalid-icon").Render(w)
}
if !s.invalid && s.warn {
WarningAltFilled().Attr("class", "bx--select__invalid-icon bx--select__invalid-icon--warning").Render(w)
}
}
w.Write([]byte(`</div>`))
if !s.invalid && s.helperText != "" {
w.Write([]byte(`<div class="bx--form__helper-text`))
if s.disabled {
w.Write([]byte(` bx--form__helper-text--disabled`))
}
w.Write([]byte(`">`))
{
io.WriteString(w, s.helperText)
}
w.Write([]byte(`</div>`))
}
if s.invalid {
w.Write([]byte(`<div id="`))
io.WriteString(w, errorId)
w.Write([]byte(`" class="bx--form-requirement">`))
{
io.WriteString(w, s.invalidText)
}
w.Write([]byte(`</div>`))
}
if !s.invalid && s.warn {
w.Write([]byte(`<div id="`))
io.WriteString(w, errorId)
w.Write([]byte(`" class="bx--form-requirement">`))
{
io.WriteString(w, s.warnText)
}
w.Write([]byte(`</div>`))
}
}
}
w.Write([]byte(`</div></div>`))
}
func renderSelectClosure(invalid, disabled, required bool, errorId, id, name, size, selected string, attrs []Attr, children []*selectItem) func(io.Writer) {
return func(w io.Writer) {
w.Write([]byte(`<select`))
if invalid {
w.Write([]byte(` aria-describedby="`))
io.WriteString(w, errorId)
w.Write([]byte(`" aria-invalid`))
}
if disabled {
w.Write([]byte(` disabled`))
}
if required {
w.Write([]byte(` required`))
}
w.Write([]byte(` id="`))
io.WriteString(w, id)
w.Write([]byte(`" name="`))
io.WriteString(w, name)
w.Write([]byte(`" class="bx--select-input`))
switch size {
case "sm":
w.Write([]byte(` bx--select-input--sm`))
case "xl":
w.Write([]byte(` bx--select-input--xl`))
}
renderAttrs(w, attrs)
w.Write([]byte(`">`))
{
for _, child := range children {
if child.value == selected {
child.selected = true
} else {
child.selected = false
}
child.Render(w)
}
}
w.Write([]byte(`</select>`))
}
}