From 9a25ec37825aeda6cb940e3e8b6c5e70345bf443 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sun, 6 Jun 2021 14:18:43 -0700 Subject: [PATCH] SelectBox: Ability to set the current value --- selectbox.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/selectbox.go b/selectbox.go index 4ebae5f..ef5bab1 100644 --- a/selectbox.go +++ b/selectbox.go @@ -115,7 +115,7 @@ func (w *SelectBox) AddItem(label string, value interface{}, f func()) { // TODO: RemoveItem() -// Value returns the currently selected item in the SelectBox. +// GetValue returns the currently selected item in the SelectBox. // // Returns the SelectValue and true on success, and the Label or underlying Value // can be read from the SelectValue struct. If no valid option is selected, the @@ -130,6 +130,28 @@ func (w *SelectBox) GetValue() (SelectValue, bool) { return SelectValue{}, false } +// SetValueByLabel sets the currently selected option to the given label. +func (w *SelectBox) SetValueByLabel(label string) bool { + for _, option := range w.values { + if option.Label == label { + w.textVariable = option.Label + return true + } + } + return false +} + +// SetValue sets the currently selected option to the given value. +func (w *SelectBox) SetValue(value interface{}) bool { + for _, option := range w.values { + if option.Value == value { + w.textVariable = option.Label + return true + } + } + return false +} + // Compute to re-evaluate the button state (in the case of radio buttons where // a different button will affect the state of this one when clicked). func (w *SelectBox) Compute(e render.Engine) {