Add autocomplete attribute to forms

This commit is contained in:
Tom Wiesing 2023-01-08 17:16:38 +01:00
parent cf59bd7db7
commit c6f77e86fe
No known key found for this signature in database
9 changed files with 169 additions and 72 deletions

View file

@ -0,0 +1,63 @@
package field
// Autocomplete represents different autocomplete options
type Autocomplete string
const (
Off Autocomplete = "off"
On Autocomplete = "on"
Name Autocomplete = "name"
HonorificPrefix Autocomplete = "honorific-prefix"
GivenName Autocomplete = "given-name"
AdditionalName Autocomplete = "additional-name"
FamilyName Autocomplete = "family-name"
HonorificSuffix Autocomplete = "honorific-suffix"
Nickname Autocomplete = "nickname"
Email_ Autocomplete = "email"
Username Autocomplete = "username"
NewPassword Autocomplete = "new-password"
CurrentPassword Autocomplete = "current-password"
OneTimeCode Autocomplete = "one-time-code"
OrganizationTitle Autocomplete = "organization-title"
Organization Autocomplete = "organization"
AddressLine1 Autocomplete = "address-line1"
AddressLine2 Autocomplete = "address-line2"
AddressLine3 Autocomplete = "address-line3"
StreetAddress Autocomplete = "street-address"
AddressLevel4 Autocomplete = "address-level4"
AddressLevel3 Autocomplete = "address-level3"
AddressLevel2 Autocomplete = "address-level2"
AddressLevel1 Autocomplete = "address-level1"
Country Autocomplete = "country"
CountryName Autocomplete = "country-name"
PostalCode Autocomplete = "postal-code"
CcName Autocomplete = "cc-name"
CcGivenName Autocomplete = "cc-given-name"
CcAdditionalName Autocomplete = "cc-additional-name"
CcFamilyName Autocomplete = "cc-family-name"
CcNumber Autocomplete = "cc-number"
CcExp Autocomplete = "cc-exp"
CcExpMonth Autocomplete = "cc-exp-month"
CcExpYear Autocomplete = "cc-exp-year"
CcCsc Autocomplete = "cc-csc"
CcType Autocomplete = "cc-type"
TransactionCurrency Autocomplete = "transaction-currency"
TransactionAmount Autocomplete = "transaction-amount"
Language Autocomplete = "language"
Bday Autocomplete = "bday"
BdayDay Autocomplete = "bday-day"
BdayMonth Autocomplete = "bday-month"
BdayYear Autocomplete = "bday-year"
Sex Autocomplete = "sex"
Tel_ Autocomplete = "tel"
TelCountryCode Autocomplete = "tel-country-code"
TelNational Autocomplete = "tel-national"
TelAreaCode Autocomplete = "tel-area-code"
TelLocal Autocomplete = "tel-local"
TelLocalPrefix Autocomplete = "tel-local-prefix"
TelLocalSuffix Autocomplete = "tel-local-suffix"
TelExtension Autocomplete = "tel-extension"
Impp Autocomplete = "impp"
Url_ Autocomplete = "url"
Photo Autocomplete = "photo"
)

40
pkg/httpx/field/field.go Normal file
View file

@ -0,0 +1,40 @@
package field
import (
"html/template"
"io"
)
// DefaultFieldTemplate is the default template to render fields.
var DefaultFieldTemplate = template.Must(template.New("").Parse(`<input type="{{.Type}}" value="{{.Value}}" name="{{.Name}}" placeholder={{.Placeholder}}{{if .Autocomplete }} autocomplete="{{.Autocomplete}}{{end}}>`))
var PureCSSFieldTemplate = template.Must(template.New("").Parse(`
<div class="pure-control-group"><label for="{{.Name}}">{{.Label}}</label><input type="{{.Type}}" value="{{.Value}}" name="{{.Name}}" id="{{.Name}}" placeholder="{{.Placeholder}}"{{if .Autocomplete }} autocomplete="{{.Autocomplete}}" {{end}}></div>`))
// Field represents a field inside a form.
type Field struct {
Name string // Name is the name of the field
Type InputType // Type is the type of the field. It corresponds to the "name" attribute in html.
Placeholder string // Value for the "placeholder" attribute
Label string // (External) Label for the field. Not used by the default template.
Autocomplete Autocomplete
EmptyOnError bool // indicates if the field should be reset on error
}
// fieldContext is passed to the template context
type fieldContext struct {
Field
Value string
}
func (field Field) WriteTo(w io.Writer, template *template.Template, value string) {
if template == nil {
template = DefaultFieldTemplate
}
template.Execute(w, fieldContext{Field: field, Value: value})
}
// CheckboxChecked is the default value of a checked checkbox
const CheckboxChecked = "on"

30
pkg/httpx/field/type.go Normal file
View file

@ -0,0 +1,30 @@
package field
// InputType represents the type of input
type InputType string
const (
Button InputType = "button"
Checkbox InputType = "checkbox"
Color InputType = "color"
Date InputType = "date"
DatetimeLocal InputType = "datetime-local"
Email InputType = "email"
File InputType = "file"
Hidden InputType = "hidden"
Image InputType = "image"
Month InputType = "month"
Number InputType = "number"
Password InputType = "password"
Radio InputType = "radio"
Range InputType = "range"
Reset InputType = "reset"
Search InputType = "search"
Submit InputType = "submit"
Tel InputType = "tel"
Text InputType = "text"
Time InputType = "time"
Url InputType = "url"
Week InputType = "week"
Datetime InputType = "datetime"
)