1
0
mirror of https://github.com/rivo/tview.git synced 2025-04-26 13:49:06 +08:00

DropDown's SetCurrentOption() will also trigger the selected event. Closes #256, resolves #260

This commit is contained in:
Oliver 2019-04-05 23:20:22 +02:00
parent 8a9e26fab0
commit 9d616aee87

View File

@ -96,10 +96,25 @@ func NewDropDown() *DropDown {
} }
// SetCurrentOption sets the index of the currently selected option. This may // SetCurrentOption sets the index of the currently selected option. This may
// be a negative value to indicate that no option is currently selected. // be a negative value to indicate that no option is currently selected. Calling
// this function will also trigger the "selected" callback (if there is one).
func (d *DropDown) SetCurrentOption(index int) *DropDown { func (d *DropDown) SetCurrentOption(index int) *DropDown {
if index >= 0 && index < len(d.options) {
d.currentOption = index d.currentOption = index
d.list.SetCurrentItem(index) d.list.SetCurrentItem(index)
if d.selected != nil {
d.selected(d.options[index].Text, index)
}
if d.options[index].Selected != nil {
d.options[index].Selected()
}
} else {
d.currentOption = -1
d.list.SetCurrentItem(0) // Set to 0 because -1 means "last item".
if d.selected != nil {
d.selected("", -1)
}
}
return d return d
} }
@ -216,7 +231,8 @@ func (d *DropDown) SetOptions(texts []string, selected func(text string, index i
// SetSelectedFunc sets a handler which is called when the user changes the // SetSelectedFunc sets a handler which is called when the user changes the
// drop-down's option. This handler will be called in addition and prior to // drop-down's option. This handler will be called in addition and prior to
// an option's optional individual handler. The handler is provided with the // an option's optional individual handler. The handler is provided with the
// selected option's text and index. // selected option's text and index. If "no option" was selected, these values
// are an empty string and -1.
func (d *DropDown) SetSelectedFunc(handler func(text string, index int)) *DropDown { func (d *DropDown) SetSelectedFunc(handler func(text string, index int)) *DropDown {
d.selected = handler d.selected = handler
return d return d