Skip to content

Commit

Permalink
feat: expose focused field
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-grgt committed Jan 5, 2025
1 parent 438e4f7 commit 78745f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
20 changes: 20 additions & 0 deletions huh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,26 @@ func TestAbort(t *testing.T) {
}
}

func TestGetFocussedField(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"))).
Expand Down

0 comments on commit 78745f7

Please sign in to comment.