shelves/internal/templates/components/form.go
2024-11-16 14:31:32 -05:00

45 lines
682 B
Go

package components
import (
"git.soup.land/soup/shelves/internal/templates"
"html/template"
)
type Field struct {
Label string
Name string
Error string
Type string
Value string
Placeholder string
Valid bool
}
func (f *Field) init() {
if f.Type == "" {
f.Type = "text"
}
f.Valid = f.Valid || f.Error == ""
}
type Form struct {
Action string
Method string
Fields []Field
}
var formTmpl = templates.MustParseEmbed("components/form.tmpl.html")
func (f Form) HTML() template.HTML {
if f.Method == "" {
f.Method = "POST"
}
for i := range f.Fields {
f.Fields[i].init()
}
return templates.HTML(formTmpl, "form", f)
}