Skip to content

Commit b81a44f

Browse files
authored
Merge pull request #3 from donseba/textarea
add textarea
2 parents bd02d40 + f035e31 commit b81a44f

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ There is currently only one template file for all the currently supported templa
6767
<option value="{{$option.Id}}">{{$option.Name}}</option>
6868
{{ end }}
6969
</select>
70+
{{ else if eq .Field.Type "textarea" }}
71+
<textarea {{with .Field.Id}}id="{{.}}"{{end}} name="{{.Field.Name}}" {{with .Field.Id}}rows="{{.}}"{{end}} {{with .Field.Cols}}cols="{{.}}"{{end}} placeholder="{{.Field.Placeholder}}" {{ if eq .Field.Required true }}required{{end}} class="block w-full shadow-sm sm:text-sm border-gray-300 rounded-md"></textarea>
7072
{{ else if eq .Field.Type "dropdownmapped" }}
7173
<select {{with .Field.Id}}id="{{.}}"{{end}} name="{{.Field.Name}}" class="text-gray-700 dark:text-gray-200 dark:bg-gray-700 bg-white block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm">
7274
{{ $value := .Field.Value }}
@@ -152,6 +154,9 @@ supported tags
152154
- placeholder
153155
- name
154156
- required
157+
- cols
158+
- rows
159+
- step
155160

156161

157162
## TODO

fields.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const (
1212
FieldTypeDropdown FieldType = "dropdown"
1313
FieldTypeDropdownMapped FieldType = "dropdownmapped"
1414
FieldTypeSubmit FieldType = "submit"
15-
FieldTypeTextArea FieldType = "textArea"
15+
FieldTypeTextArea FieldType = "textarea"
1616
)
1717

1818
type InputFieldType string
@@ -23,7 +23,6 @@ const (
2323
InputFieldTypeEmail InputFieldType = "email"
2424
InputFieldTypeTel InputFieldType = "tel"
2525
InputFieldTypeNumber InputFieldType = "number"
26-
InputFieldTypeDate InputFieldType = "date"
2726
InputFieldTypeNone InputFieldType = ""
2827
)
2928

@@ -56,6 +55,8 @@ type FormField struct {
5655
InputType InputFieldType `json:"inputType,omitempty"`
5756
Label string `json:"label,omitempty"`
5857
Step string `json:"step,omitempty"`
58+
Rows string `json:"rows,omitempty"`
59+
Cols string `json:"cols,omitempty"`
5960
Values []FieldValue `json:"values,omitempty"`
6061
Required bool `json:"required"`
6162
Fields []FormField `json:"fields,omitempty"`

transformer.go

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ import (
77
"strings"
88
)
99

10+
const (
11+
tagName = "name"
12+
tagLabel = "label"
13+
tagPlaceholder = "placeholder"
14+
tagRequired = "required"
15+
tagInputType = "inputType"
16+
tagLegend = "legend"
17+
tagType = "type"
18+
tagStep = "step"
19+
tagRows = "rows"
20+
tagCols = "cols"
21+
)
22+
1023
type (
1124
Enumerator interface{ Enum() []any }
1225
Mapper interface {
@@ -56,6 +69,8 @@ func NewTransformer(model interface{}) (*Transformer, error) {
5669

5770
tr.Fields = fields
5871

72+
fmt.Printf("tr: %+v\n", tr.Fields)
73+
5974
return tr, nil
6075
}
6176

@@ -72,16 +87,16 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names
7287
for i := 0; i < rType.NumField(); i++ {
7388
tags := rType.Field(i).Tag
7489

75-
name := tags.Get("name")
90+
name := tags.Get(tagName)
7691
if name == "" {
7792
name = rType.Field(i).Name
7893
}
7994

8095
nname := append(names, name)
8196

8297
field := FormField{
83-
Label: tags.Get("label"),
84-
Placeholder: tags.Get("placeholder"),
98+
Label: tags.Get(tagLabel),
99+
Placeholder: tags.Get(tagPlaceholder),
85100
Name: strings.Join(nname, "."),
86101
Value: rValue.Field(i).Interface(),
87102
}
@@ -90,7 +105,7 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names
90105
field.Label = name
91106
}
92107

93-
if tags.Get("required") == "true" {
108+
if tags.Get(tagRequired) == "true" {
94109
field.Required = true
95110
}
96111

@@ -150,7 +165,7 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names
150165
continue
151166
}
152167

153-
inputType := InputFieldType(tags.Get("inputType"))
168+
inputType := InputFieldType(tags.Get(tagInputType))
154169

155170
fType := rType.Field(i).Type
156171
fValue := rValue.Field(i)
@@ -167,14 +182,23 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names
167182

168183
switch fType.Kind() {
169184
case reflect.String:
170-
171185
if inputType == "" {
172186
inputType = InputFieldTypeText
173187
}
174188

175-
field.Type = FieldTypeInput
176-
field.InputType = inputType
189+
typ := FieldType(tags.Get(tagType))
190+
if typ == "" {
191+
typ = FieldTypeInput
192+
}
177193

194+
field.Type = typ
195+
field.InputType = inputType
196+
if tags.Get(tagRows) != "" {
197+
field.Rows = tags.Get(tagRows)
198+
}
199+
if tags.Get(tagCols) != "" {
200+
field.Cols = tags.Get(tagCols)
201+
}
178202
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
179203
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
180204

@@ -184,36 +208,40 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names
184208

185209
field.Type = FieldTypeInput
186210
field.InputType = inputType
187-
field.Step = "1"
188211

212+
if tags.Get(tagStep) != "" {
213+
field.Step = tags.Get(tagStep)
214+
} else {
215+
field.Step = "1"
216+
}
189217
case reflect.Float32, reflect.Float64:
190218
if inputType == "" {
191219
inputType = InputFieldTypeNumber
192220
}
193221

194222
field.Type = FieldTypeInput
195223
field.InputType = inputType
196-
field.Step = "any"
197224

198-
case reflect.Bool:
199-
fieldType := FieldTypeCheckbox
200-
if len(names) > 0 && names[len(names)-1] == name {
201-
// radio-options use the same 'name' as their parent for grouping
202-
fieldType = FieldTypeRadios
225+
if tags.Get(tagStep) != "" {
226+
field.Step = tags.Get(tagStep)
227+
} else {
228+
field.Step = "any"
203229
}
204-
205-
field.Type = fieldType
230+
case reflect.Bool:
231+
field.Type = FieldTypeCheckbox
206232
case reflect.Slice, reflect.Array:
207233
case reflect.Map:
208234
case reflect.Struct:
209235
field.Type = FieldTypeGroup
210-
field.Legend = tags.Get("legend")
236+
field.Legend = tags.Get(tagLegend)
211237

212238
var err error
213239
field.Fields, err = t.scanModel(fValue, fType, nname...)
214240
if err != nil {
215241
return nil, err
216242
}
243+
default:
244+
return nil, fmt.Errorf("unsupported type: %s", fType.Kind())
217245
}
218246

219247
fields = append(fields, field)

0 commit comments

Comments
 (0)