input

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 2 Imported by: 1

README

Input Types

This package contains all input implementations for tinywasm/form. Each input implements fmt.Widget (Type, Validate, Clone) and dom.Component (RenderHTML). All inputs use only tinywasm/fmt — no errors or strconv from the standard library.

Available Inputs

Type HTML type Validation rules
Address text Letters, Numbers, . , - # /, Min: 5, Max: 200
Checkbox checkbox true, false, on, 1, 0 or empty
Date date YYYY-MM-DD format, leap year + month/day range check
Datalist text Value must match one of the registered Options.Key
Email email Letters, Numbers, @ . _ -, Min: 5, Max: 100
Filepath text Letters, digits, .\/- _, no whitespace, Min: 1
Gender radio m/f pre-wired options
Hour time HH:MM format, digits + :, validates 24h range
IP text IPv4 or IPv6 format; 0.0.0.0 rejected
Number number Digits only (0-9), Min: 1, Max: 20 chars
Password password Any char, Min: 5, Max: 50
Phone tel Digits, + ( ) -, Min: 7, Max: 15
Radio radio Value must match one of the registered Options.Key
Rut text Chilean RUT XXXXXXX-D, verifies check digit
Select select Value must match one of the registered Options.Key
Text text Letters, Numbers, . , ( ), Min: 2, Max: 100
Textarea textarea Wide char set incl. \n, Min: 5, Max: 2000

No Standard Library

Rule: All input files must import only github.com/tinywasm/fmt. No errors, strconv, or strings.

Use the tinywasm/fmt equivalents:

// Instead of strconv.Atoi:
val, err := fmt.Convert("42").Int()

// Instead of errors.New:
return fmt.Err("Field", "Invalid")

// Instead of strings.ToLower:
lower := fmt.Convert(s).ToLower().String()

// Instead of strings.Contains:
found := fmt.Contains(haystack, needle)

Prototype Pattern

Constructors take zero arguments and return stateless prototypes. The form layer calls Clone(parentID, name) to create positioned instances that preserve all configuration.

// Schema definition (ormc generates this)
var schema = []fmt.Field{
    {Name: "email", Type: fmt.FieldText, Widget: input.Email()},
}

// form.New calls Clone internally:
// field.Widget.Clone(formID, fieldName) → positioned input with id, name, HTML attributes

Creating a Custom Input (embedding Base)

All inputs share the same pattern: embed Base, configure Permitted rules, implement the Input interface.

package input

import "github.com/tinywasm/fmt"

// myInput is a custom input that only allows lowercase hex characters.
type myInput struct {
    Base
}

// MyInput creates a prototype — no arguments.
func MyInput() Input {
    m := &myInput{}
    m.Letters = true
    m.Numbers = true
    m.Minimum = 1
    m.Maximum = 40
    m.InitBase("", "", "text")
    m.SetPlaceholder("e.g. 3f4a1b")
    m.SetTitle("Lowercase hex only")
    return m
}

func (m *myInput) Validate(value string) error {
    // Custom rule: no uppercase letters
    for _, c := range value {
        if c >= 'A' && c <= 'F' {
            return fmt.Err("Character", "Invalid")
        }
    }
    return m.Permitted.Validate(m.name, value)
}

// Clone creates a positioned copy preserving all configuration.
func (m *myInput) Clone(parentID, name string) fmt.Widget {
    c := *m
    c.InitBase(parentID, name, "text")
    return &c
}
Base Available Methods
Method Purpose
InitBase(parentID, name, htmlName) Required — sets ID, name, and HTML type
SetPlaceholder(string) HTML placeholder text
SetTitle(string) HTML title (tooltip)
SetOptions(...fmt.KeyValue) Options for select/radio/datalist
AddAttribute(key, value string) Custom extra HTML attributes
SetRequired(bool) HTML required attribute
SetSkipValidation(bool) Skip validation entirely
RenderInput() Generates <input>, <textarea>, or <select> tag

Composition Pattern (wrapping another input)

Reuse existing inputs to create semantic wrappers:

func Gender() Input {
    g := &gender{}
    g.InitBase("", "", "radio")
    g.SetOptions(
        fmt.KeyValue{Key: "m", Value: "Male"},
        fmt.KeyValue{Key: "f", Value: "Female"},
    )
    return g
}

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
	fmt.Permitted  // anonymous embed: promotes Letters, Numbers, Validate(), etc.
	// 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) Children added in v0.0.12

func (b *Base) Children() []dom.Component

Children returns empty slice (inputs are leaf nodes).

func (*Base) FieldName

func (b *Base) FieldName() string

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

func (*Base) GetID added in v0.0.12

func (b *Base) GetID() string

GetID returns the component's unique identifier.

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) HandlerName added in v0.0.4

func (b *Base) HandlerName() string

HandlerName returns the component's unique identifier. Deprecated: use GetID instead.

func (*Base) InitBase

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

InitBase initializes the base fields and constructs the unique ID.

func (*Base) RenderHTML added in v0.0.12

func (b *Base) RenderHTML() string

RenderHTML renders the input to HTML.

func (*Base) RenderInput

func (b *Base) RenderInput() string

RenderInput generates the HTML for the input based on its htmlName. Handles: input, textarea, select (with options), radio (label+input per option), and datalist (input + datalist element). No custom RenderHTML needed in sub-types.

func (*Base) SetID added in v0.0.12

func (b *Base) SetID(id string)

SetID sets the component's unique identifier.

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) SetRequired added in v0.2.0

func (b *Base) SetRequired(req bool)

SetRequired sets the required attribute.

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.

func (*Base) Type added in v0.2.0

func (b *Base) Type() string

Type satisfies fmt.Widget.Type(). Returns the semantic input type name.

func (*Base) Validate added in v0.2.0

func (b *Base) Validate(value string) error

Validate satisfies fmt.Widget.Validate(). Concrete structs embedding Base can override this to provide specialized validation.

type Input

type Input interface {
	fmt.Widget    // Type(), Validate(), Clone(parentID, name) — semantic type contract
	dom.Component // Includes GetID(), SetID(), RenderHTML(), Children()
	FieldName() string
	SetRequired(bool)
	AddAttribute(key, value string)
}

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() Input

Address creates a new Address input instance.

func Checkbox added in v0.0.19

func Checkbox() Input

Checkbox creates a new checkbox input instance.

func Datalist added in v0.0.19

func Datalist() Input

Datalist creates a new datalist input instance.

func Date added in v0.0.19

func Date() Input

Date creates a new date input instance.

func Email

func Email() Input

Email creates a new Email input instance.

func Filepath added in v0.0.19

func Filepath() Input

Filepath creates a new filepath input instance.

func Gender

func Gender() Input

Gender creates a new Gender input instance with default Male/Female options.

func Hour added in v0.0.19

func Hour() Input

Hour creates a new time input instance.

func IP added in v0.0.19

func IP() Input

IP creates a new IP input instance.

func Number added in v0.0.19

func Number() Input

Number creates a new number input instance.

func Password

func Password() Input

Password creates a new Password input instance.

func Phone added in v0.0.19

func Phone() Input

Phone creates a new phone input instance.

func Radio

func Radio() Input

Radio creates a new Radio input instance.

func Rut added in v0.0.19

func Rut() Input

Rut creates a new RUT input instance.

func Select

func Select() Input

Select creates a new Select input instance.

func Text

func Text() Input

Text creates a new Text input instance.

func Textarea added in v0.0.19

func Textarea() Input

Textarea creates a new textarea input instance.

Jump to

Keyboard shortcuts

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