D3vugu-components/components/input/field.go
David Vogel a587fea398 Rework value and list binder
- Rename FieldBinder to ValueBinder
- Split ValueBinder interface into ValueGetter, ValueSetter and HTMLInputTyper
- Add non pointer types to ValueBindAny.StringValue
- Move encoding.TextMarshaler in type switch
- Simplify ListBinder, and reuse ValueBinder logic
- Add ListBindGenericValues type
2023-07-03 00:18:58 +02:00

52 lines
874 B
Go

package input
import (
"github.com/vugu/vugu"
)
// Field is a text or number based input component.
// The HTML input type is determined by the bound data type.
type Field struct {
AttrMap vugu.AttrMap
ID string // The ID of the internal input component.
Bind interface {
ValueBinder
HTMLInputTyper
}
DefaultSlot vugu.Builder
Type string // Overrides the type of the input component.
err error // Current error caused by any invalid input.
}
func (c *Field) content() string {
if c.Bind != nil {
return c.Bind.StringValue()
}
return ""
}
func (c *Field) inputType() string {
if c.Type != "" {
return c.Type
}
if c.Bind != nil {
return c.Bind.HTMLInputType()
}
return ""
}
func (c *Field) handleChange(event vugu.DOMEvent) {
val := event.PropString("target", "value")
if c.Bind != nil {
c.err = c.Bind.SetStringValue(val)
}
}