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
6 changes: 5 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}}>
`)))
tests := []struct {
name string
Expand All @@ -25,14 +25,18 @@ func TestBuilder_Inputs(t *testing.T) {
arg: struct {
Name string
Email string `form:"type=email;[email protected]"`
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="[email protected]">`),
strings.TrimSpace(`
<label>City</label><input type="text" name="City" placeholder="City" value="New York" class="custom-city-class">`),
}, "")),
},
}
Expand Down
4 changes: 4 additions & 0 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ 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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reasoning behind doing HTMLEscapeString? I'm trying to think of a case where this would have something that needs escaped as part of the class and none are coming to mind.

}
}

func parseTags(tags string) map[string]string {
Expand Down Expand Up @@ -134,4 +137,5 @@ type field struct {
ID string
Value interface{}
Footer template.HTML
Class string
}
18 changes: 18 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ 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",
},
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down