From ef38ce1fae3857cc715550e17c96e6e8a164881f Mon Sep 17 00:00:00 2001 From: JonasG Date: Sun, 17 Nov 2024 08:25:49 +0100 Subject: [PATCH] feat: add option to not quit form after submission --- form.go | 12 ++++++--- huh_test.go | 47 +++++++++++++++++++++++++++++++++++ internal/selector/selector.go | 2 +- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/form.go b/form.go index e1afb0d2..4794de6d 100644 --- a/form.go +++ b/form.go @@ -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 @@ -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 @@ -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 "" } diff --git a/huh_test.go b/huh_test.go index 57288b3b..b89bbcc5 100644 --- a/huh_test.go +++ b/huh_test.go @@ -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)) @@ -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 diff --git a/internal/selector/selector.go b/internal/selector/selector.go index d0d41284..83961c54 100644 --- a/internal/selector/selector.go +++ b/internal/selector/selector.go @@ -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) }