SelectBox: Ability to set the current value

This commit is contained in:
Noah 2021-06-06 14:18:43 -07:00
parent 6df7bade48
commit 9a25ec3782

View File

@ -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) {