Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ var inputTpl = `
<label {{with .ID}}for="{{.}}"{{end}}>
{{.Label}}
</label>
<input {{with .ID}}id="{{.}}"{{end}} type="{{.Type}}" name="{{.Name}}" placeholder="{{.Placeholder}}" {{with .Value}}value="{{.}}"{{end}}>
<input {{with .ID}}id="{{.}}"{{end}} type="{{.Type}}" name="{{.Name}}" placeholder="{{.Placeholder}}" {{with .Value}}value="{{.}}"{{end}} {{with .Class}}class="{{.}}"{{end}}>
{{with .Footer}}
<p>{{.}}</p>
{{end}}
Expand All @@ -149,7 +149,7 @@ type Address struct {
City string
State string `form:"footer=Or your Province"`
Zip string `form:"label=Postal Code"`
Country string
Country string `form:"class=specific-country-css-class"`
}

func main() {
Expand Down Expand Up @@ -211,7 +211,7 @@ func main() {
<label >
Country
</label>
<input type="text" name="Country" placeholder="Country" value="United States">
<input class="specific-country-css-class" type="text" name="Country" placeholder="Country" value="United States">
</form>
```

Expand Down
19 changes: 18 additions & 1 deletion builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestBuilder_Inputs(t *testing.T) {
tpl := template.Must(template.New("").Parse(strings.TrimSpace(`
<label>{{.Label}}</label><input type="{{.Type}}" name="{{.Name}}" placeholder="{{.Placeholder}}"{{with .Value}} value="{{.}}"{{end}}>
<label>{{.Label}}</label><input type="{{.Type}}" name="{{.Name}}" placeholder="{{.Placeholder}}"{{with .Value}} value="{{.}}"{{end}}{{with .Class}} class="{{.}}"{{end}}{{range .Attrs}} {{.Render}}{{end}}>
`)))
tests := []struct {
name string
Expand All @@ -25,14 +25,31 @@ func TestBuilder_Inputs(t *testing.T) {
arg: struct {
Name string
Email string `form:"type=email;placeholder=bob@example.com"`
City string `form:"class=custom-city-class"`
}{
Name: "Michael Scott",
City: "New York",
},
want: template.HTML(strings.Join([]string{
strings.TrimSpace(`
<label>Name</label><input type="text" name="Name" placeholder="Name" value="Michael Scott">`),
strings.TrimSpace(`
<label>Email</label><input type="email" name="Email" placeholder="bob@example.com">`),
strings.TrimSpace(`
<label>City</label><input type="text" name="City" placeholder="City" value="New York" class="custom-city-class">`),
}, "")),
},
{
name: "Custom attrs",
tpl: tpl,
arg: struct {
Street string `form:"attrs={\"ts-action\":\"class+ loading, 'parent .parent'\", \"ts-trigger\": \"click\"}"`
}{
Street: "Mckinsey str.",
},
want: template.HTML(strings.Join([]string{
strings.TrimSpace(`
<label>Street</label><input type="text" name="Street" placeholder="Street" value="Mckinsey str." ts-action="class+ loading, 'parent .parent'" ts-trigger="click">`),
}, "")),
},
}
Expand Down
39 changes: 39 additions & 0 deletions reflect.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package form

import (
"encoding/json"
"fmt"
"html/template"
"reflect"
"strings"
Expand Down Expand Up @@ -101,6 +103,12 @@ func applyTags(f *field, tags map[string]string) {
// Probably shouldn't be HTML but whatever.
f.Footer = template.HTML(v)
}
if v, ok := tags["class"]; ok {
f.Class = template.HTMLEscapeString(v)
}
if v, ok := tags["attrs"]; ok {
f.Attrs = parseAttrs(v)
}
}

func parseTags(tags string) map[string]string {
Expand All @@ -126,6 +134,26 @@ func parseTags(tags string) map[string]string {
return ret
}

func parseAttrs(attrs string) []attr {
attrs = strings.TrimSpace(attrs)
var ret = []attr{}

if len(attrs) == 0 {
return ret
}
var attrVects map[string]string

if err := json.Unmarshal([]byte(attrs), &attrVects); err != nil {
fmt.Errorf("Error unmarshaller json attributes: %v", err)
}

for a, v := range attrVects {
ret = append(ret, attr{Attr: a, Value: v})
}

return ret
}

type field struct {
Name string
Label string
Expand All @@ -134,4 +162,15 @@ type field struct {
ID string
Value interface{}
Footer template.HTML
Class string
Attrs []attr
}

type attr struct {
Attr string
Value string
}

func (a *attr) Render() template.HTMLAttr {
return template.HTMLAttr(fmt.Sprintf(`%s="%s"`, a.Attr, a.Value))
}
39 changes: 39 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,45 @@ func Test_fields(t *testing.T) {
},
},
},
{
name: "custom css class",
arg: struct {
Name string `form:"class=custom-css-class"`
}{
Name: "Michael Scott",
},
want: []field{
{
Name: "Name",
Label: "Name",
Placeholder: "Name",
Type: "text",
Value: "Michael Scott",
Class: "custom-css-class",
},
},
},
{
name: "custom attr",
arg: struct {
Name string `form:"attrs={\"ts-action\":\"class+ loading, 'parent .parent'\", \"ts-trigger\": \"click\"}"`
}{
Name: "Michael Scott",
},
want: []field{
{
Name: "Name",
Label: "Name",
Placeholder: "Name",
Type: "text",
Value: "Michael Scott",
Attrs: []attr{
attr{Attr: "ts-action", Value: "class+ loading, 'parent .parent'"},
attr{Attr: "ts-trigger", Value: "click"},
},
},
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down