-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go
256 lines (208 loc) · 6.17 KB
/
types.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
package forms
import (
"fmt"
"html/template"
"strconv"
)
// List of attributes that should not be
var noUseAttrs []string
func init() {
noUseAttrs = []string{"type", "name", "value"}
}
// Type is interface that tells us
type Type interface {
// Tells if fields type should accept multiple values
IsMultiValue() bool
// Cleans data before it goes to user
CleanData(values []string) interface{}
// Render form
Render(*Field, []Choice, []string) template.HTML
}
// Input is basic input type
type Input struct{}
// IsMultiValue returns if basic input allow multiple values
func (i *Input) IsMultiValue() bool {
return false
}
// CleanData returns cleaned values for basic input
func (i *Input) CleanData(values []string) interface{} {
if len(values) > 0 {
return values[0]
}
return ""
}
// Render returns string with rendered basic input
func (i *Input) Render(f *Field, cs []Choice, vs []string) template.HTML {
return renderInput(f.Attributes, f.Name, "input", noUseAttrs, vs)
}
// Radio is radio input type
type Radio struct{}
// IsMultiValue returns if radio input allow multiple values
func (i *Radio) IsMultiValue() bool {
return true
}
// CleanData returns cleaned values for radio
func (i *Radio) CleanData(values []string) interface{} {
return values
}
// Render returns string with rendered radio input
func (i *Radio) Render(f *Field, cs []Choice, vs []string) template.HTML {
field := ""
attrs := Attributes{}
for _, c := range cs {
for k, v := range f.Attributes {
attrs[k] = v
}
attrs["id"] = fmt.Sprintf("c_%s_%s", f.Name, c.Value)
field = field + fmt.Sprintf(
"<label for=\"c_%s_%s\">%s %s</label>\n",
f.Name, c.Value,
renderInput(attrs, f.Name, "radio", noUseAttrs, []string{c.Value}),
c.Label,
)
}
return template.HTML(field)
}
// Textarea is textarea type
type Textarea struct {
Input
}
// Render returns string with rendered textarea
func (t *Textarea) Render(f *Field, cs []Choice, vs []string) template.HTML {
value := ""
if len(vs) > 0 && vs[0] != "" {
value = vs[0]
}
return template.HTML(fmt.Sprintf(
"<textarea id=\"f_%s\" name=\"%s\"%s>%s</textarea>", f.Name, f.Name,
prepareAttributes(f.Attributes, noUseAttrs), value,
))
}
// InputNumber is number input type
type InputNumber struct{}
// IsMultiValue returns if numeric input allow multiple values
func (t *InputNumber) IsMultiValue() bool {
return false
}
// CleanData returns cleaned values for number input
func (t *InputNumber) CleanData(values []string) interface{} {
if len(values) == 1 {
ival, err := strconv.ParseInt(values[0], 10, 64)
if err == nil {
return ival
}
fval, err := strconv.ParseFloat(values[0], 64)
if err == nil {
return fval
}
}
return nil
}
// Render returns string with rendered number input
func (t *InputNumber) Render(f *Field, cs []Choice, vs []string) template.HTML {
return renderInput(f.Attributes, f.Name, "number", noUseAttrs, vs)
}
// Checkbox is checkbox input type
type Checkbox struct {
*Input
}
// CleanData returns cleaned values for checkbox
func (t *Checkbox) CleanData(values []string) interface{} {
if len(values) == 1 && values[0] != "" {
return true
}
return false
}
// Render returns string with rendered checkbox input
func (t *Checkbox) Render(f *Field, cs []Choice, vs []string) template.HTML {
var attrs Attributes
if f.Attributes == nil {
attrs = Attributes{}
} else {
attrs = f.Attributes
}
if len(vs) > 0 && vs[0] != "" {
attrs["checked"] = "checked"
}
return renderInput(attrs, f.Name, "checkbox", noUseAttrs, nil)
}
// InputEmail is email input type
type InputEmail struct {
*Input
}
// Render returns string with rendered email input
func (t *InputEmail) Render(f *Field, cs []Choice, vs []string) template.HTML {
return renderInput(f.Attributes, f.Name, "email", noUseAttrs, vs)
}
// InputPassword is password input type
type InputPassword struct {
*Input
}
// Render returns string with rendered password input
func (t *InputPassword) Render(f *Field, cs []Choice, vs []string) template.HTML {
return renderInput(f.Attributes, f.Name, "password", noUseAttrs, vs)
}
// InputDate is date input type
type InputDate struct {
*Input
}
// Render returns string with rendered date input
func (t *InputDate) Render(f *Field, cs []Choice, vs []string) template.HTML {
return renderInput(f.Attributes, f.Name, "date", noUseAttrs, vs)
}
// InputTime is date input type
type InputTime struct {
*Input
}
// Render returns string with rendered time input
func (t *InputTime) Render(f *Field, cs []Choice, vs []string) template.HTML {
return renderInput(f.Attributes, f.Name, "time", noUseAttrs, vs)
}
// InputDateTime is date datetime (uses datetime-local) input type
type InputDateTime struct {
*Input
}
// Render returns string with rendered datetime input
func (t *InputDateTime) Render(f *Field, cs []Choice, vs []string) template.HTML {
return renderInput(f.Attributes, f.Name, "datetime-local", noUseAttrs, vs)
}
// InputMonth is month input type
type InputMonth struct {
*Input
}
// Render returns string with rendered month input
func (t *InputMonth) Render(f *Field, cs []Choice, vs []string) template.HTML {
return renderInput(f.Attributes, f.Name, "month", noUseAttrs, vs)
}
// InputWeek is week input type
type InputWeek struct {
*Input
}
// Render returns string with rendered week input
func (t *InputWeek) Render(f *Field, cs []Choice, vs []string) template.HTML {
return renderInput(f.Attributes, f.Name, "week", noUseAttrs, vs)
}
// InputURL is url input type
type InputURL struct {
*Input
}
// Render returns string with rendered url input
func (t *InputURL) Render(f *Field, cs []Choice, vs []string) template.HTML {
return renderInput(f.Attributes, f.Name, "url", noUseAttrs, vs)
}
// InputTel is tel input type
type InputTel struct {
*Input
}
// Render returns string with rendered tel input
func (t *InputTel) Render(f *Field, cs []Choice, vs []string) template.HTML {
return renderInput(f.Attributes, f.Name, "tel", noUseAttrs, vs)
}
// InputSearch is tel search type
type InputSearch struct {
*Input
}
// Render returns string with rendered search input
func (t *InputSearch) Render(f *Field, cs []Choice, vs []string) template.HTML {
return renderInput(f.Attributes, f.Name, "search", noUseAttrs, vs)
}