diff --git a/form.go b/form.go index 02d34a1..ad2d3f0 100644 --- a/form.go +++ b/form.go @@ -479,6 +479,11 @@ func (f *Form) PrevField() tea.Cmd { return cmd } +// GetFocusedField returns the focused form field. +func (f *Form) GetFocusedField() Field { + return f.selector.Selected().selector.Selected() +} + // Init initializes the form. func (f *Form) Init() tea.Cmd { cmds := make([]tea.Cmd, f.selector.Total()) diff --git a/huh_test.go b/huh_test.go index 57288b3..04a909f 100644 --- a/huh_test.go +++ b/huh_test.go @@ -918,6 +918,24 @@ func TestAbort(t *testing.T) { } } +func TestGetFocusedField(t *testing.T) { + f := NewForm( + NewGroup( + NewInput().Title("First").Key("First"), + NewInput().Title("Second").Key("Second"), + NewInput().Title("Third").Key("Third"), + ), + ).WithWidth(25) + f = batchUpdate(f, f.Init()).(*Form) + + f.NextField() + field := f.GetFocusedField() + + if field.GetKey() != "Second" { + t.Error("Expected Second field to be focused but was '" + field.GetKey() + "'") + } +} + // formProgram returns a new Form with a nil input and output, so it can be used as a test program. func formProgram() *Form { return NewForm(NewGroup(NewInput().Title("Foo"))).