Preview filter while browsing an open dropdown; Esc closes only the list

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-20 20:42:53 +02:00
co-authored by Claude Sonnet 5.5
parent 6b780242c9
commit ac1c2ebce2
6 changed files with 146 additions and 16 deletions
+53 -2
View File
@@ -1,6 +1,7 @@
package ui
import (
"reflect"
"testing"
"github.com/gdamore/tcell/v2"
@@ -12,7 +13,7 @@ func TestArrowNavigation(t *testing.T) {
AddDropDown("A", []string{"x", "y", "z"}, 0, nil).
AddCheckbox("B", true, nil).
AddButton("Add", nil).AddButton("Cancel", nil)
arrowNavigation(f)
arrowNavigation(f, nil, nil)
var focus func(p tview.Primitive)
focus = func(p tview.Primitive) { p.Focus(focus) }
f.Focus(focus)
@@ -56,7 +57,7 @@ func TestSearchNowFollowsMonitored(t *testing.T) {
AddCheckbox("M", true, nil).
AddCheckbox("S", true, nil).
AddButton("Add", nil)
arrowNavigation(f)
arrowNavigation(f, nil, nil)
var focus func(p tview.Primitive)
focus = func(p tview.Primitive) { p.Focus(focus) }
f.Focus(focus)
@@ -108,3 +109,53 @@ func TestSearchNowFollowsMonitored(t *testing.T) {
t.Error("Search now should return to its previous choice")
}
}
func TestBrowseOpenDropDown(t *testing.T) {
f := tview.NewForm().AddDropDown("D", []string{"any", "a", "b", "c"}, 0, nil)
var seen []int
aborted, cancelled := 0, 0
arrowNavigation(f, func() { cancelled++ }, &browseHooks{
browse: func(_ *tview.DropDown, i int) { seen = append(seen, i) },
abort: func() { aborted++ },
})
var focus func(p tview.Primitive)
focus = func(p tview.Primitive) { p.Focus(focus) }
f.Focus(focus)
send := func(k tcell.Key) { f.InputHandler()(tcell.NewEventKey(k, 0, 0), focus) }
dd := f.GetFormItem(0).(*tview.DropDown)
send(tcell.KeyEnter) // open
if !dd.IsOpen() {
t.Fatal("dropdown did not open")
}
send(tcell.KeyDown)
send(tcell.KeyDown)
send(tcell.KeyUp)
send(tcell.KeyUp)
send(tcell.KeyUp) // wraps to the last entry
if want := []int{1, 2, 1, 0, 3}; !reflect.DeepEqual(seen, want) {
t.Fatalf("browse saw %v, want %v", seen, want)
}
send(tcell.KeyEnter) // select what the highlight is on
if i, _ := dd.GetCurrentOption(); i != 3 {
t.Errorf("selected option %d, want 3: the mirrored highlight drifted from the real list", i)
}
seen = nil
send(tcell.KeyEnter)
send(tcell.KeyDown) // wraps to 0
send(tcell.KeyEscape)
if cancelled != 0 {
t.Error("Esc on an open list must only close the list")
}
send(tcell.KeyEscape) // list closed: now Esc cancels the form
if cancelled != 1 {
t.Errorf("cancelled=%d, want 1", cancelled)
}
if aborted != 1 || !reflect.DeepEqual(seen, []int{0}) {
t.Errorf("aborted=%d seen=%v", aborted, seen)
}
if i, _ := dd.GetCurrentOption(); i != 3 {
t.Errorf("Esc must keep the committed option, got %d", i)
}
}