From 023b522d351167c0a085c82c890dbaf1d835b521 Mon Sep 17 00:00:00 2001 From: Eric Wollesen Date: Fri, 15 Nov 2019 15:12:26 -0700 Subject: [PATCH] Guards against a panic in dropdown.SetCurrentOption(-1) When SetCurrentOption is called, that option's Selected hook is called, if present. However, should -1 (no selection) be passed, then the code would panic with an index out of range error. --- dropdown.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dropdown.go b/dropdown.go index 58eee24..1d4a7af 100644 --- a/dropdown.go +++ b/dropdown.go @@ -434,8 +434,10 @@ func (d *DropDown) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr if d.selected != nil { d.selected(d.options[d.currentOption].Text, d.currentOption) } - if d.options[d.currentOption].Selected != nil { - d.options[d.currentOption].Selected() + if d.currentOption >= 0 && d.currentOption < len(d.options) { + if d.options[d.currentOption].Selected != nil { + d.options[d.currentOption].Selected() + } } }).SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if event.Key() == tcell.KeyRune {