Skip to content

Commit

Permalink
feat: add option to not quit form after submission
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-grgt committed Nov 17, 2024
1 parent 690ffa4 commit 8c463aa
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
11 changes: 8 additions & 3 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ type Form struct {
// to be more accessible to screen readers.
accessible bool

quitting bool
aborted bool
// whether to quit the form after Submitting or not,
// defaults to true
QuitAfterSubmit bool
quitting bool
aborted bool

// options
width int
Expand Down Expand Up @@ -105,6 +108,7 @@ func NewForm(groups ...*Group) *Form {
teaOptions: []tea.ProgramOption{
tea.WithOutput(os.Stderr),
},
QuitAfterSubmit: true,
}

// NB: If dynamic forms come into play this will need to be applied when
Expand Down Expand Up @@ -551,6 +555,7 @@ func (f *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

if f.selector.OnLast() {
f.selector.Selected().showHelp = false
return submit()
}

Expand Down Expand Up @@ -607,7 +612,7 @@ func (f *Form) isGroupHidden(group *Group) bool {

// View renders the form.
func (f *Form) View() string {
if f.quitting {
if f.quitting && f.QuitAfterSubmit {
return ""
}

Expand Down
65 changes: 65 additions & 0 deletions huh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/charmbracelet/bubbles/cursor"
"io"
"regexp"
"strings"
Expand Down Expand Up @@ -275,6 +276,53 @@ func TestForm(t *testing.T) {
// TODO: Finish and submit form.
}

func TestFormNotQuiting(t *testing.T) {
t.Run("Quit form after submission", func(t *testing.T) {
f := NewForm(NewGroup(NewInput()))
f.Update(f.Init())

f = submitForm(f)

view := ansi.Strip(f.View())

if strings.Contains(view, ">") {
t.Log(pretty.Render(view))
t.Error("Expected form to have quit but was visible.")
}
})

t.Run("Form stays alive after submission", func(t *testing.T) {
f := NewForm(NewGroup(NewInput()))
f.QuitAfterSubmit = false
f.Update(f.Init())

submitForm(f)

view := ansi.Strip(f.View())

if !strings.Contains(view, ">") {
t.Log(pretty.Render(view))
t.Error("Expected form to be visible but has quit.")
}
})

t.Run("Grouped form stays alive after submission", func(t *testing.T) {
f := NewForm(NewGroup(NewInput()), NewGroup(NewInput()))
f.QuitAfterSubmit = false
f.Update(f.Init())

submitForm(f)
submitForm(f)

view := ansi.Strip(f.View())

if !strings.Contains(view, ">") {
t.Log(pretty.Render(view))
t.Error("Expected grouped form to be visible but has quit.")
}
})
}

func TestInput(t *testing.T) {
field := NewInput()
f := NewForm(NewGroup(field))
Expand Down Expand Up @@ -925,6 +973,23 @@ func formProgram() *Form {
WithAccessible(false)
}

func submitForm(f *Form) *Form {
m, cmd := f.Update(tea.KeyMsg{Type: tea.KeyEnter})
f = m.(*Form)
for {
if cmd != nil {
msg := cmd()
_, cmd = f.Update(msg)
if _, ok := msg.(cursor.BlinkMsg); ok {
break
}
} else {
break
}
}
return f
}

func batchUpdate(m tea.Model, cmd tea.Cmd) tea.Model {
if cmd == nil {
return m
Expand Down
2 changes: 1 addition & 1 deletion internal/selector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (s *Selector[T]) Index() int {
return s.index
}

// Totoal returns the total number of items.
// Total returns the total number of items.
func (s *Selector[T]) Total() int {
return len(s.items)
}
Expand Down

0 comments on commit 8c463aa

Please sign in to comment.