-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasswordinput.go
349 lines (319 loc) · 8.97 KB
/
passwordinput.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package carbon
import (
"io"
"slices"
"strings"
)
type passwordInput struct {
attrs []Attr
size string
value string
type_ string
placeholder string
hidePasswordLabel string
showPasswordLabel string
tooltipAlignment string
tooltipPosition string
light bool
disabled bool
helperText string
labelText string
hideLabel bool
invalid bool
invalidText string
warn bool
warnText string
inline bool
name string
isFluid bool
}
var _ Component = (*passwordInput)(nil)
func PasswordInput(labelText string) *passwordInput {
return &passwordInput{
attrs: nil,
size: "",
value: "",
type_: "password",
placeholder: "",
hidePasswordLabel: "Hide password",
showPasswordLabel: "Show password",
tooltipAlignment: "center",
tooltipPosition: "bottom",
light: false,
disabled: false,
helperText: "",
labelText: labelText,
hideLabel: false,
invalid: false,
invalidText: "",
warn: false,
warnText: "",
inline: false,
name: "",
isFluid: false,
}
}
func (p *passwordInput) Attr(name string, value string) *passwordInput {
p.attrs = append(p.attrs, Attr{name, value})
return p
}
func (p *passwordInput) Size(size string) *passwordInput {
sizes := []string{"", "sm", "xl"}
if !slices.Contains(sizes, size) {
panic("size must be one of " + strings.Join(sizes, ", "))
}
p.size = size
return p
}
func (p *passwordInput) Value(value string) *passwordInput {
p.value = value
return p
}
func (p *passwordInput) Type(type_ string) *passwordInput {
types := []string{"password", "text"}
if !slices.Contains(types, type_) {
panic("type must be one of " + strings.Join(types, ", "))
}
p.type_ = type_
return p
}
func (p *passwordInput) Placeholder(placeholder string) *passwordInput {
p.placeholder = placeholder
return p
}
func (p *passwordInput) HidePasswordLabel(hidePasswordLabel string) *passwordInput {
p.hidePasswordLabel = hidePasswordLabel
return p
}
func (p *passwordInput) ShowPasswordLabel(showPasswordLabel string) *passwordInput {
p.showPasswordLabel = showPasswordLabel
return p
}
func (p *passwordInput) TooltipAlignment(tooltipAlignment string) *passwordInput {
tooltipAlignments := []string{"start", "center", "end"}
if !slices.Contains(tooltipAlignments, tooltipAlignment) {
panic("tooltipAlignment must be one of " + strings.Join(tooltipAlignments, ", "))
}
p.tooltipAlignment = tooltipAlignment
return p
}
func (p *passwordInput) TooltipPosition(tooltipPosition string) *passwordInput {
tooltipPositions := []string{"top", "right", "bottom", "left"}
if !slices.Contains(tooltipPositions, tooltipPosition) {
panic("tooltipPosition must be one of " + strings.Join(tooltipPositions, ", "))
}
p.tooltipPosition = tooltipPosition
return p
}
func (p *passwordInput) Light(v bool) *passwordInput {
p.light = v
return p
}
func (p *passwordInput) Disabled(v bool) *passwordInput {
p.disabled = v
return p
}
func (p *passwordInput) HelperText(helperText string) *passwordInput {
p.helperText = helperText
return p
}
func (p *passwordInput) HideLabel(v bool) *passwordInput {
p.hideLabel = v
return p
}
func (p *passwordInput) Invalid(v bool) *passwordInput {
p.invalid = v
return p
}
func (p *passwordInput) InvalidText(invalidText string) *passwordInput {
p.invalidText = invalidText
return p
}
func (p *passwordInput) Warn(v bool) *passwordInput {
p.warn = v
return p
}
func (p *passwordInput) WarnText(warnText string) *passwordInput {
p.warnText = warnText
return p
}
func (p *passwordInput) Inline(v bool) *passwordInput {
p.inline = v
return p
}
func (p *passwordInput) Name(name string) *passwordInput {
p.name = name
return p
}
func (p *passwordInput) IsFluid(v bool) *passwordInput {
p.isFluid = v
return p
}
func (p *passwordInput) Render(w io.Writer) {
id := "ccs-" + useId()
helperId := "helper-" + id
errorId := "error-" + id
warnId := "warn-" + id
renderLabelText := renderLabelTextClosure(id, p.hideLabel, p.disabled, p.inline, p.size, p.labelText)
renderHelperText := renderHelperTextClosure(p.disabled, p.inline, p.helperText)
w.Write([]byte(`<div class="bx--form-item bx--text-input-wrapper`))
if !p.isFluid {
w.Write([]byte(` bx--password-input-wrapper`))
}
if p.light {
w.Write([]byte(` bx--text-input-wrapper--light`))
}
if p.inline {
w.Write([]byte(` bx--text-input-wrapper--inline`))
}
w.Write([]byte(`">`))
{
if p.inline {
renderLabelText(w)
if !p.isFluid && p.helperText != "" {
renderHelperText(w)
}
}
if !p.inline && p.labelText != "" {
renderLabelText(w)
}
w.Write([]byte(`<div class="bx--text-input__field-outer-wrapper`))
if p.inline {
w.Write([]byte(` bx--text-input__field-outer-wrapper--inline`))
}
w.Write([]byte(`">`))
{
w.Write([]byte(`<div class="bx--text-input__field-wrapper`))
if p.warn {
w.Write([]byte(` bx--text-input__field-wrapper--warning`))
}
w.Write([]byte(`"`))
if p.invalid {
w.Write([]byte(` data-invalid`))
}
w.Write([]byte(`>`))
{
if p.invalid {
WarningFilled().Attr("class", "bx--text-input__invalid-icon").Render(w)
}
if !p.invalid && p.warn {
WarningAltFilled().Attr("class", "bx--text-input__invalid-icon bx--text-input__invalid-icon--warning").Render(w)
}
w.Write([]byte(`<input`))
if p.invalid {
w.Write([]byte(` data-invalid aria-invalid`))
}
describedBy := ternary(p.invalid, errorId, ternary(p.warn, warnId, ternary(p.helperText != "", helperId, "")))
if describedBy != "" {
w.Write([]byte(` aria-describedby="`))
io.WriteString(w, describedBy)
w.Write([]byte(`"`))
}
w.Write([]byte(` id="`))
io.WriteString(w, id)
w.Write([]byte(`"`))
if p.name != "" {
w.Write([]byte(` name="`))
io.WriteString(w, p.name)
w.Write([]byte(`"`))
}
if p.placeholder != "" {
w.Write([]byte(` placeholder="`))
io.WriteString(w, p.placeholder)
w.Write([]byte(`"`))
}
w.Write([]byte(` type="`))
io.WriteString(w, p.type_)
w.Write([]byte(`"`))
if p.value != "" {
w.Write([]byte(` value="`))
io.WriteString(w, p.value)
w.Write([]byte(`"`))
}
if p.disabled {
w.Write([]byte(` disabled`))
}
w.Write([]byte(` class="bx--text-input bx--password-input`))
if p.light {
w.Write([]byte(` bx--text-input--light`))
}
if p.invalid {
w.Write([]byte(` bx--text-input--invalid`))
}
if p.warn {
w.Write([]byte(` bx--text-input--warning`))
}
if p.size == "sm" || p.size == "xl" {
w.Write([]byte(` bx--text-input--`))
io.WriteString(w, p.size)
}
w.Write([]byte(`"`))
renderAttrs(w, p.attrs)
w.Write([]byte(` />`))
if p.isFluid && p.invalid {
w.Write([]byte(`<hr class="bx--text-input__divider"/><div class="bx--form-requirement" id="`))
io.WriteString(w, errorId)
w.Write([]byte(`">`))
io.WriteString(w, p.invalidText)
w.Write([]byte(`</div>`))
} else {
w.Write([]byte(`<button type="button" disabled class="bx--text-input--password__visibility__toggle bx--btn bx--btn--icon-only bx--tooltip__trigger bx--tooltip--a11y`))
if p.disabled {
w.Write([]byte(` disabled`))
}
w.Write([]byte(` bx--tooltip--`))
io.WriteString(w, p.tooltipPosition)
w.Write([]byte(` bx--tooltip--align-`))
io.WriteString(w, p.tooltipAlignment)
w.Write([]byte(`">`))
{
if !p.disabled {
w.Write([]byte(`<span class="bx--assistive-text">`))
if p.type_ == "text" {
io.WriteString(w, p.hidePasswordLabel)
} else {
io.WriteString(w, p.showPasswordLabel)
}
w.Write([]byte(`</span>`))
}
if p.type_ == "text" {
ViewOff().Attr("class", "bx--icon-visibility-off").Render(w)
} else {
View().Attr("class", "bx--icon-visibility-on").Render(w)
}
}
w.Write([]byte(`</button>`))
}
}
w.Write([]byte(`</div>`))
if !p.isFluid && p.invalid {
w.Write([]byte(`<div class="bx--form-requirement" id="`))
io.WriteString(w, errorId)
w.Write([]byte(`">`))
io.WriteString(w, p.invalidText)
w.Write([]byte(`</div>`))
}
if !p.invalid && !p.warn && !p.isFluid && !p.inline && p.helperText != "" {
w.Write([]byte(`<div class="bx--form__helper-text`))
if p.disabled {
w.Write([]byte(` bx--form__helper-text--disabled`))
}
if p.inline {
w.Write([]byte(` bx--form__helper-text--inline`))
}
w.Write([]byte(`">`))
io.WriteString(w, p.helperText)
w.Write([]byte(`</div>`))
}
if !p.isFluid && !p.invalid && p.warn {
w.Write([]byte(`<div class="bx--form-requirement" id="`))
io.WriteString(w, warnId)
w.Write([]byte(`">`))
io.WriteString(w, p.warnText)
w.Write([]byte(`</div>`))
}
}
w.Write([]byte(`</div>`))
}
w.Write([]byte(`</div>`))
}