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 ef38ce1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
12 changes: 8 additions & 4 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,15 @@ type Form struct {
State FormState

// whether or not to use bubble tea rendering for accessibility
// purposes, if true, the form will render with basic prompting primitives
//purposes, if true, the form will render with basic prompting primitives
// 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 @@ -607,7 +611,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
47 changes: 47 additions & 0 deletions huh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,39 @@ func TestForm(t *testing.T) {
// TODO: Finish and submit form.
}

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

f = submitForm(f)

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

if strings.Contains(view, "enter submit") {
t.Log(pretty.Render(view))
t.Error("Expected form to not be visible but was.")
}
})

t.Run("Do not quit form after submitting", func(t *testing.T) {
field := NewInput()
f := NewForm(NewGroup(field))
f.QuitAfterSubmit = false
f.Update(f.Init())

submitForm(f)

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

if !strings.Contains(view, "enter submit") {
t.Log(pretty.Render(view))
t.Error("Expected form to be visible but was not.")
}
})
}

func TestInput(t *testing.T) {
field := NewInput()
f := NewForm(NewGroup(field))
Expand Down Expand Up @@ -925,6 +958,20 @@ 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)
} 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 ef38ce1

Please sign in to comment.