D3vugu-components/components/overlay/modal-requester.go
David Vogel 2253e46d09 Improve requester modal
- Add ability to bind to inputs
- Add customizable button text
- Add example for requester modal with binding
2025-09-04 17:35:51 +02:00

88 lines
2.3 KiB
Go

package overlay
import (
"git.d3nexus.de/Dadido3/D3vugu-components/components/input"
"github.com/vugu/vugu"
)
type ModalRequester struct {
OverlayContainerRef
IconSlot vugu.Builder `vugu:"data"` // Slot for the symbol.
Title string `vugu:"data"`
Message string `vugu:"data"`
SignalColor string // A d3c CSS class for the color scheme of the small bar.
// Optional input value that can be set by the modal requester.
// This bound value can be modified even when the requester ended by aborting.
InputBind interface {
input.ValueBinder
input.HTMLInputTyper
}
TextAbort string // Can be used to override the text shown for the abort button.
TextYes string // Can be used to override the text shown for the yes button.
TextNo string // Can be used to override the text shown for the no button.
ClickAbort input.ClickHandler // Fired when the abort button is clicked.
ClickYes input.ClickHandler // Fired when the yes button is clicked.
ClickNo input.ClickHandler // Fired when the no button is clicked.
}
func (c *ModalRequester) buttonTextAbort() string {
if c.TextAbort != "" {
return c.TextAbort
}
return "Abort"
}
func (c *ModalRequester) buttonTextYes() string {
if c.TextYes != "" {
return c.TextYes
}
return "Yes"
}
func (c *ModalRequester) buttonTextNo() string {
if c.TextNo != "" {
return c.TextNo
}
return "No"
}
func (c *ModalRequester) handleClickAbort(event vugu.DOMEvent) {
if c.ClickAbort != nil {
c.ClickAbort.ClickHandle(input.ClickEvent{DOMEvent: event})
}
c.SetModal(nil)
}
func (c *ModalRequester) handleClickYes(event vugu.DOMEvent) {
if c.ClickYes != nil {
c.ClickYes.ClickHandle(input.ClickEvent{DOMEvent: event})
}
c.SetModal(nil)
}
func (c *ModalRequester) handleClickNo(event vugu.DOMEvent) {
if c.ClickNo != nil {
c.ClickNo.ClickHandle(input.ClickEvent{DOMEvent: event})
}
c.SetModal(nil)
}
// OverlayClasses implements OverlayClassesGetter which is used by the overlay package to add classes to overlay elements.
func (c *ModalRequester) OverlayClasses() (signalClasses, containerClasses string) {
return c.SignalColor, ""
}
// PageTitle implements PageTitleGetter which is used by PageInfo.
func (c *ModalRequester) PageTitle() (title, longTitle, shortTitle string) {
return c.Title, c.Title, c.Title
}