2023-05-25 13:59:13 +00:00
|
|
|
package input
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/vugu/vugu"
|
|
|
|
)
|
|
|
|
|
2023-05-25 20:55:42 +00:00
|
|
|
// Field is a text or number based input component.
|
2023-05-25 13:59:13 +00:00
|
|
|
// The HTML input type is determined by the bound data type.
|
|
|
|
type Field struct {
|
|
|
|
AttrMap vugu.AttrMap
|
2023-06-04 15:45:27 +00:00
|
|
|
ID string // The ID of the internal input component.
|
2023-05-25 13:59:13 +00:00
|
|
|
|
2023-07-02 22:18:58 +00:00
|
|
|
Bind interface {
|
|
|
|
ValueBinder
|
|
|
|
HTMLInputTyper
|
|
|
|
}
|
2023-05-25 13:59:13 +00:00
|
|
|
|
|
|
|
DefaultSlot vugu.Builder
|
|
|
|
|
2023-06-23 19:10:35 +00:00
|
|
|
Type string // Overrides the type of the input component.
|
|
|
|
|
2023-05-25 13:59:13 +00:00
|
|
|
err error // Current error caused by any invalid input.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Field) content() string {
|
|
|
|
if c.Bind != nil {
|
2023-07-02 22:18:58 +00:00
|
|
|
return c.Bind.StringValue()
|
2023-05-25 13:59:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Field) inputType() string {
|
2023-06-23 19:10:35 +00:00
|
|
|
if c.Type != "" {
|
|
|
|
return c.Type
|
|
|
|
}
|
|
|
|
|
2023-05-25 13:59:13 +00:00
|
|
|
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 {
|
2023-07-02 22:18:58 +00:00
|
|
|
c.err = c.Bind.SetStringValue(val)
|
2023-05-25 13:59:13 +00:00
|
|
|
}
|
|
|
|
}
|