D3vugu-components/components/input/events.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

117 lines
3.1 KiB
Go

package input
import "github.com/vugu/vugu"
// A mirror of some DOM events described here:
// https://www.w3schools.com/tags/ref_eventattributes.asp
// TODO: Find a better way to expose DOM events from HTML elements inside D3vugu-components
type ChangeEvent struct {
vugu.DOMEvent
}
// ChangeHandler is the interface for things that can handle ChangeEvent.
type ChangeHandler interface {
ChangeHandle(event ChangeEvent)
}
// ChangeFunc implements ChangeHandler as a function.
type ChangeFunc func(event ChangeEvent)
// ChangeHandle implements the ChangeHandler interface.
func (f ChangeFunc) ChangeHandle(event ChangeEvent) { f(event) }
// assert ChangeFunc implements ChangeHandler.
var _ ChangeHandler = ChangeFunc(nil)
type InputEvent struct {
vugu.DOMEvent
}
// InputHandler is the interface for things that can handle InputEvent.
type InputHandler interface {
InputHandle(event InputEvent)
}
// InputFunc implements InputHandler as a function.
type InputFunc func(event InputEvent)
// InputHandle implements the InputHandler interface.
func (f InputFunc) InputHandle(event InputEvent) { f(event) }
// assert InputFunc implements InputHandler.
var _ InputHandler = InputFunc(nil)
type KeyDownEvent struct {
vugu.DOMEvent
}
// KeyDownHandler is the interface for things that can handle KeyDownEvent.
type KeyDownHandler interface {
KeyDownHandle(event KeyDownEvent)
}
// KeyDownFunc implements KeyDownHandler as a function.
type KeyDownFunc func(event KeyDownEvent)
// KeyDownHandle implements the KeyDownHandler interface.
func (f KeyDownFunc) KeyDownHandle(event KeyDownEvent) { f(event) }
// assert KeyDownFunc implements KeyDownHandler.
var _ KeyDownHandler = KeyDownFunc(nil)
type KeyPressEvent struct {
vugu.DOMEvent
}
// KeyPressHandler is the interface for things that can handle KeyPressEvent.
type KeyPressHandler interface {
KeyPressHandle(event KeyPressEvent)
}
// KeyPressFunc implements KeyPressHandler as a function.
type KeyPressFunc func(event KeyPressEvent)
// KeyPressHandle implements the KeyPressHandler interface.
func (f KeyPressFunc) KeyPressHandle(event KeyPressEvent) { f(event) }
// assert KeyPressFunc implements KeyPressHandler.
var _ KeyPressHandler = KeyPressFunc(nil)
type KeyUpEvent struct {
vugu.DOMEvent
}
// KeyUpHandler is the interface for things that can handle KeyUpEvent.
type KeyUpHandler interface {
KeyUpHandle(event KeyUpEvent)
}
// KeyUpFunc implements KeyUpHandler as a function.
type KeyUpFunc func(event KeyUpEvent)
// KeyUpHandle implements the KeyUpHandler interface.
func (f KeyUpFunc) KeyUpHandle(event KeyUpEvent) { f(event) }
// assert KeyUpFunc implements KeyUpHandler.
var _ KeyUpHandler = KeyUpFunc(nil)
type ClickEvent struct {
vugu.DOMEvent
}
// ClickHandler is the interface for things that can handle ClickEvent.
type ClickHandler interface {
ClickHandle(event ClickEvent)
}
// ClickFunc implements ClickHandler as a function.
type ClickFunc func(event ClickEvent)
// ClickHandle implements the ClickHandler interface.
func (f ClickFunc) ClickHandle(event ClickEvent) { f(event) }
// assert ClickFunc implements ClickHandler.
var _ ClickHandler = ClickFunc(nil)