Skip to content

Commit

Permalink
add sorted map support for the dropdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
donseba committed Oct 29, 2023
1 parent 96db431 commit 20a842b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ 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 "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 }}
{{ range $k, $option := .Field.Values }}
<option value="{{$option.Value}}" {{ if eq $value.String $option.Value }}selected{{ end }} {{ if eq $option.Disabled true }}disabled{{ end }}>{{$option.Name}}</option>
{{ end }}
</select>
{{ else if eq .Type "checkbox" }}
<input {{with .Field.Id}}id="{{.}}"{{end}} name="{{.Field.Name}}" type="checkbox" {{ if eq .Field.Required true }}required{{end}} {{ if eq .Field.Value true }}checked{{end}} class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600">
{{ else }}
Expand Down Expand Up @@ -99,6 +106,41 @@ func (fe fieldError) FieldError() (field, err string) {
}
```

Support for sorted maps is also included.

```go
type CountryListOption struct {
Selected string
SortedValues []form.SortedMap
}

type SortedMap struct {
SKey string
SValue string
}

func (s SortedMap) Key() string { return s.SKey }
func (s SortedMap) Value() string { return s.SValue }

func (t *CountryListOption) SortedMapper() []form.SortedMap {
return t.SortedValues
}

func (t *CountryListOption) String() string {
return t.Selected
}
```

Now in the struct that contains the form you can add the following:

```go
UserForm struct {
Country *CountryListOption
}
```

however the downside is that you create a direct dependancy to the `form.SortedMap` interface.

supported tags
- label
- placeholder
Expand Down
33 changes: 33 additions & 0 deletions transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,25 @@ type (
Mapper() map[string]string
String() string
}

// SortedMapper is new addition to provide sorted key value pairs
SortedMapper interface {
String() string
SortedMapper() []SortedMap
}

SortedMap interface {
Key() string
Value() string
}
)

var (
enumType = reflect.TypeOf((*Enumerator)(nil)).Elem()
mapperType = reflect.TypeOf((*Mapper)(nil)).Elem()

//new addition to provide sorted key value pairs
sortedMapperType = reflect.TypeOf((*SortedMapper)(nil)).Elem()
)

type Transformer struct {
Expand Down Expand Up @@ -117,6 +131,25 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names
continue
}

//new addition to provide sorted key value pairs
if rType.Field(i).Type.Implements(sortedMapperType) {
maps := rValue.Field(i).Interface().(SortedMapper).SortedMapper()
var fieldValue []FieldValue
for _, v := range maps {
fieldValue = append(fieldValue, FieldValue{
Value: v.Key(),
Name: v.Value(),
Disabled: false,
})
}

field.Type = FieldTypeDropdownMapped
field.Values = fieldValue

fields = append(fields, field)
continue
}

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

fType := rType.Field(i).Type
Expand Down

0 comments on commit 20a842b

Please sign in to comment.