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 Jan 5, 2025
1 parent 438e4f7 commit 3364066
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 4 deletions.
16 changes: 13 additions & 3 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ 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
prevShowHelp bool
quitting bool
aborted bool

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

// NB: If dynamic forms come into play this will need to be applied when
Expand Down Expand Up @@ -494,6 +500,8 @@ func (f *Form) Init() tea.Cmd {
cmds = append(cmds, nextGroup)
}

f.selector.Selected().showHelp = f.prevShowHelp

return tea.Batch(cmds...)
}

Expand Down Expand Up @@ -551,6 +559,8 @@ func (f *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

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

Expand Down Expand Up @@ -607,7 +617,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
98 changes: 98 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,86 @@ 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.")
}
})

t.Run("Hide group help when frozen", func(t *testing.T) {
f := NewForm(NewGroup(NewInput()))
f.QuitAfterSubmit = false
f.Update(f.Init())

submitForm(f)
submitForm(f)

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

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

t.Run("Reset previous group help state after re-initialization", func(t *testing.T) {
f := NewForm(NewGroup(NewInput()).WithShowHelp(true))
f.QuitAfterSubmit = false
f.Update(f.Init())

submitForm(f)
submitForm(f)
f.Init()

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

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

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

Please sign in to comment.