Skip to content

Commit f5989d9

Browse files
authored
feat(form): expose focused field (#503)
1 parent 0f36f9d commit f5989d9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

form.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@ func (f *Form) PrevField() tea.Cmd {
479479
return cmd
480480
}
481481

482+
// GetFocusedField returns the focused form field.
483+
func (f *Form) GetFocusedField() Field {
484+
return f.selector.Selected().selector.Selected()
485+
}
486+
482487
// Init initializes the form.
483488
func (f *Form) Init() tea.Cmd {
484489
cmds := make([]tea.Cmd, f.selector.Total())

huh_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,24 @@ func TestAbort(t *testing.T) {
918918
}
919919
}
920920

921+
func TestGetFocusedField(t *testing.T) {
922+
f := NewForm(
923+
NewGroup(
924+
NewInput().Title("First").Key("First"),
925+
NewInput().Title("Second").Key("Second"),
926+
NewInput().Title("Third").Key("Third"),
927+
),
928+
).WithWidth(25)
929+
f = batchUpdate(f, f.Init()).(*Form)
930+
931+
f.NextField()
932+
field := f.GetFocusedField()
933+
934+
if field.GetKey() != "Second" {
935+
t.Error("Expected Second field to be focused but was '" + field.GetKey() + "'")
936+
}
937+
}
938+
921939
// formProgram returns a new Form with a nil input and output, so it can be used as a test program.
922940
func formProgram() *Form {
923941
return NewForm(NewGroup(NewInput().Title("Foo"))).

0 commit comments

Comments
 (0)