D3vugu-components/components/input/field.go
David Vogel 94f2eee2db Add event handling to input field and text area components
- Add events that wrap vugu.DOMEvent
- Add event example for the input field component
- Rename handleChange to handleInput
- Forward any oninput, onkeydown, onkeypress or onkeyup events to event listeners
2025-08-30 20:15:06 +02:00

79 lines
1.6 KiB
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.
Input InputHandler // External handler that is called upon an event.
KeyDown KeyDownHandler // External handler that is called upon an event.
KeyPress KeyPressHandler // External handler that is called upon an event.
KeyUp KeyUpHandler // External handler that is called upon an event.
}
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) handleInput(event vugu.DOMEvent) {
val := event.PropString("target", "value")
if c.Bind != nil {
c.err = c.Bind.SetStringValue(val)
}
if c.Input != nil {
c.Input.InputHandle(InputEvent{event})
}
}
func (c *Field) handleKeyDown(event vugu.DOMEvent) {
if c.KeyDown != nil {
c.KeyDown.KeyDownHandle(KeyDownEvent{event})
}
}
func (c *Field) handleKeyPress(event vugu.DOMEvent) {
if c.KeyPress != nil {
c.KeyPress.KeyPressHandle(KeyPressEvent{event})
}
}
func (c *Field) handleKeyUp(event vugu.DOMEvent) {
if c.KeyUp != nil {
c.KeyUp.KeyUpHandle(KeyUpEvent{event})
}
}