Skip to content

Commit

Permalink
Merge pull request #3 from donseba/textarea
Browse files Browse the repository at this point in the history
add textarea
  • Loading branch information
donseba authored Jan 29, 2024
2 parents bd02d40 + f035e31 commit b81a44f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ There is currently only one template file for all the currently supported templa
<option value="{{$option.Id}}">{{$option.Name}}</option>
{{ end }}
</select>
{{ else if eq .Field.Type "textarea" }}
<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>
{{ else if eq .Field.Type "dropdownmapped" }}
<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">
{{ $value := .Field.Value }}
Expand Down Expand Up @@ -152,6 +154,9 @@ supported tags
- placeholder
- name
- required
- cols
- rows
- step


## TODO
Expand Down
5 changes: 3 additions & 2 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
FieldTypeDropdown FieldType = "dropdown"
FieldTypeDropdownMapped FieldType = "dropdownmapped"
FieldTypeSubmit FieldType = "submit"
FieldTypeTextArea FieldType = "textArea"
FieldTypeTextArea FieldType = "textarea"
)

type InputFieldType string
Expand All @@ -23,7 +23,6 @@ const (
InputFieldTypeEmail InputFieldType = "email"
InputFieldTypeTel InputFieldType = "tel"
InputFieldTypeNumber InputFieldType = "number"
InputFieldTypeDate InputFieldType = "date"
InputFieldTypeNone InputFieldType = ""
)

Expand Down Expand Up @@ -56,6 +55,8 @@ type FormField struct {
InputType InputFieldType `json:"inputType,omitempty"`
Label string `json:"label,omitempty"`
Step string `json:"step,omitempty"`
Rows string `json:"rows,omitempty"`
Cols string `json:"cols,omitempty"`
Values []FieldValue `json:"values,omitempty"`
Required bool `json:"required"`
Fields []FormField `json:"fields,omitempty"`
Expand Down
64 changes: 46 additions & 18 deletions transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ import (
"strings"
)

const (
tagName = "name"
tagLabel = "label"
tagPlaceholder = "placeholder"
tagRequired = "required"
tagInputType = "inputType"
tagLegend = "legend"
tagType = "type"
tagStep = "step"
tagRows = "rows"
tagCols = "cols"
)

type (
Enumerator interface{ Enum() []any }
Mapper interface {
Expand Down Expand Up @@ -56,6 +69,8 @@ func NewTransformer(model interface{}) (*Transformer, error) {

tr.Fields = fields

fmt.Printf("tr: %+v\n", tr.Fields)

return tr, nil
}

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

name := tags.Get("name")
name := tags.Get(tagName)
if name == "" {
name = rType.Field(i).Name
}

nname := append(names, name)

field := FormField{
Label: tags.Get("label"),
Placeholder: tags.Get("placeholder"),
Label: tags.Get(tagLabel),
Placeholder: tags.Get(tagPlaceholder),
Name: strings.Join(nname, "."),
Value: rValue.Field(i).Interface(),
}
Expand All @@ -90,7 +105,7 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names
field.Label = name
}

if tags.Get("required") == "true" {
if tags.Get(tagRequired) == "true" {
field.Required = true
}

Expand Down Expand Up @@ -150,7 +165,7 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names
continue
}

inputType := InputFieldType(tags.Get("inputType"))
inputType := InputFieldType(tags.Get(tagInputType))

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

switch fType.Kind() {
case reflect.String:

if inputType == "" {
inputType = InputFieldTypeText
}

field.Type = FieldTypeInput
field.InputType = inputType
typ := FieldType(tags.Get(tagType))
if typ == "" {
typ = FieldTypeInput
}

field.Type = typ
field.InputType = inputType
if tags.Get(tagRows) != "" {
field.Rows = tags.Get(tagRows)
}
if tags.Get(tagCols) != "" {
field.Cols = tags.Get(tagCols)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:

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

field.Type = FieldTypeInput
field.InputType = inputType
field.Step = "1"

if tags.Get(tagStep) != "" {
field.Step = tags.Get(tagStep)
} else {
field.Step = "1"
}
case reflect.Float32, reflect.Float64:
if inputType == "" {
inputType = InputFieldTypeNumber
}

field.Type = FieldTypeInput
field.InputType = inputType
field.Step = "any"

case reflect.Bool:
fieldType := FieldTypeCheckbox
if len(names) > 0 && names[len(names)-1] == name {
// radio-options use the same 'name' as their parent for grouping
fieldType = FieldTypeRadios
if tags.Get(tagStep) != "" {
field.Step = tags.Get(tagStep)
} else {
field.Step = "any"
}

field.Type = fieldType
case reflect.Bool:
field.Type = FieldTypeCheckbox
case reflect.Slice, reflect.Array:
case reflect.Map:
case reflect.Struct:
field.Type = FieldTypeGroup
field.Legend = tags.Get("legend")
field.Legend = tags.Get(tagLegend)

var err error
field.Fields, err = t.scanModel(fValue, fType, nname...)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported type: %s", fType.Kind())
}

fields = append(fields, field)
Expand Down

0 comments on commit b81a44f

Please sign in to comment.