input

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 2 Imported by: 0

README

Input Types

This package contains all input implementations for tinywasm/form.

Default Inputs

Type HTML Aliases Validation
Text <input type="text"> name, fullname, username Letters, Numbers, Min: 2, Max: 100
Email <input type="email"> mail, correo Letters, Numbers, @._-, Min: 5, Max: 100
Password <input type="password"> pass, clave, pwd All chars, Min: 5, Max: 50

Composition Pattern

Reuse base inputs to create semantic ones:

func Gender(parentID, name string) input.Input {
    g := input.Radio(parentID, name).(*radio)
    g.Base.SetOptions(fmt.KeyValue{Key: "m", Value: "Male"}, ...)
    return g
}

Matching Logic

The form engine matches fields in this order:

  1. LowerCase(FieldName) vs htmlName or aliases
  2. LowerCase(StructName.FieldName) vs aliases

Registering Custom Inputs

If you create a new input type, register it to make it available:

form.RegisterInput(MyCustomInput("", ""))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Base

type Base struct {
	Values         []string       // Multiple values support (for select/checkbox/etc)
	Options        []fmt.KeyValue // Multiple options for select/checkbox/etc
	Placeholder    string
	Title          string
	Required       bool // HTML required attribute
	Disabled       bool // HTML disabled attribute
	Readonly       bool // HTML readonly attribute
	SkipValidation bool // Whether to skip validation for this input
	Attributes     []fmt.KeyValue
	// contains filtered or unexported fields
}

Base contains common logic and fields (State) for all inputs. It is intended to be embedded in concrete input structs.

func (*Base) AddAttribute

func (b *Base) AddAttribute(key, value string)

AddAttribute adds a custom attribute to the input.

func (*Base) FieldName

func (b *Base) FieldName() string

FieldName returns the struct field name (without parent prefix).

func (*Base) GetOptions

func (b *Base) GetOptions() []fmt.KeyValue

GetOptions returns all options.

func (*Base) GetPlaceholder

func (b *Base) GetPlaceholder() string

GetPlaceholder returns the input placeholder.

func (*Base) GetSelectedValue

func (b *Base) GetSelectedValue() string

GetSelectedValue returns the first value in Values, or empty if none.

func (*Base) GetSkipValidation

func (b *Base) GetSkipValidation() bool

GetSkipValidation returns whether to skip validation.

func (*Base) GetTitle

func (b *Base) GetTitle() string

GetTitle returns the input title.

func (*Base) GetValue

func (b *Base) GetValue() string

GetValue returns the first value (for simple inputs).

func (*Base) GetValues

func (b *Base) GetValues() []string

GetValues returns all input values.

func (*Base) HTMLName

func (b *Base) HTMLName() string

HTMLName returns the HTML input type.

func (*Base) ID

func (b *Base) ID() string

ID returns the component's unique identifier.

func (*Base) InitBase

func (b *Base) InitBase(parentID, name, htmlName string, aliases ...string)

InitBase initializes the base fields and constructs the unique ID.

func (*Base) Matches

func (b *Base) Matches(fieldName string) bool

Matches checks if the given field name matches this input's htmlName, name or aliases.

func (*Base) RenderInput

func (b *Base) RenderInput() string

RenderInput generates the standard HTML tag for the input.

func (*Base) SetAliases

func (b *Base) SetAliases(aliases ...string)

SetAliases sets the field name aliases for matching.

func (*Base) SetOptions

func (b *Base) SetOptions(opts ...fmt.KeyValue)

SetOptions sets multiple options (for select/checkbox/etc).

func (*Base) SetPlaceholder

func (b *Base) SetPlaceholder(ph string)

SetPlaceholder sets the input placeholder.

func (*Base) SetSkipValidation

func (b *Base) SetSkipValidation(skip bool)

SetSkipValidation sets whether to skip validation for this input.

func (*Base) SetTitle

func (b *Base) SetTitle(title string)

SetTitle sets the input title (tooltip).

func (*Base) SetValues

func (b *Base) SetValues(v ...string)

SetValues sets the input values.

type Input

type Input interface {
	dom.Component // Includes ID() and RenderHTML()

	HTMLName() string                  // Standard HTML5 type (e.g., "text", "email")
	FieldName() string                 // Struct field name (without parent prefix)
	ValidateField(value string) error  // Self-contained validation logic
	Clone(parentID, name string) Input // Creates a new instance with given parentID and name
}

Input interface defines the behavior for all form input types. It embeds dom.Component to ensure compatibility with the tinywasm/dom ecosystem.

func Address

func Address(parentID, name string) Input

Address creates a new Address input instance. It is a semantic wrapper around Text.

func Email

func Email(parentID, name string) Input

Email creates a new Email input instance.

func Gender

func Gender(parentID, name string) Input

Gender creates a new Gender input instance with default Male/Female options. It is a semantic wrapper around Radio.

func Password

func Password(parentID, name string) Input

Password creates a new Password input instance.

func Radio

func Radio(parentID, name string) Input

Radio creates a new Radio input instance.

func Select

func Select(parentID, name string) Input

Select creates a new Select input instance.

func Text

func Text(parentID, name string) Input

Text creates a new Text input instance.

type Permitted

type Permitted struct {
	Letters         bool
	Tilde           bool
	Numbers         bool
	BreakLine       bool     // line breaks allowed
	WhiteSpaces     bool     // white spaces allowed
	Tabulation      bool     // tabulation allowed
	TextNotAllowed  []string // text not allowed eg: "hola" not allowed
	Characters      []rune   // other special characters eg: '\','/','@'
	Minimum         int      // min characters eg 2 "lo" ok default 0 no defined
	Maximum         int      // max characters eg 1 "l" ok default 0 no defined
	ExtraValidation func(string) error
	StartWith       *Permitted // characters allowed at the beginning
}

func (Permitted) MinMaxAllowedChars

func (p Permitted) MinMaxAllowedChars() (min, max int)

func (Permitted) Validate

func (h Permitted) Validate(text string) (err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL