Arrow-key navigation in the add dialog

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-20 19:52:04 +02:00
co-authored by Claude Sonnet 5.5
parent 354279ab55
commit 2dfd018284
5 changed files with 116 additions and 2 deletions
+50
View File
@@ -0,0 +1,50 @@
package ui
import (
"testing"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
func TestArrowNavigation(t *testing.T) {
f := tview.NewForm().
AddDropDown("A", []string{"x", "y", "z"}, 0, nil).
AddCheckbox("B", true, nil).
AddButton("Add", nil).AddButton("Cancel", nil)
arrowNavigation(f)
var focus func(p tview.Primitive)
focus = func(p tview.Primitive) { p.Focus(focus) }
f.Focus(focus)
cap := f.GetInputCapture()
send := func(key tcell.Key) { cap(tcell.NewEventKey(key, 0, 0)) }
dd := f.GetFormItem(0).(*tview.DropDown)
send(tcell.KeyRight)
if i, _ := dd.GetCurrentOption(); i != 1 {
t.Fatalf("Right: option %d, want 1", i)
}
send(tcell.KeyLeft)
send(tcell.KeyLeft)
if i, _ := dd.GetCurrentOption(); i != 2 {
t.Fatalf("Left wraps: option %d, want 2", i)
}
send(tcell.KeyDown)
if item, _ := f.GetFocusedItemIndex(); item != 1 {
t.Fatalf("Down: item %d, want 1", item)
}
send(tcell.KeyDown)
send(tcell.KeyDown)
send(tcell.KeyDown) // clamps at the last button
if _, b := f.GetFocusedItemIndex(); b != 1 {
t.Fatalf("button %d, want 1", b)
}
send(tcell.KeyLeft)
if _, b := f.GetFocusedItemIndex(); b != 0 {
t.Fatalf("Left on buttons: %d, want 0", b)
}
send(tcell.KeyUp)
if item, _ := f.GetFocusedItemIndex(); item != 1 {
t.Fatalf("Up from first button: item %d, want 1", item)
}
}