Skip to content

Commit c94f4d1

Browse files
committed
feat: add option to not quit form after submission
1 parent 690ffa4 commit c94f4d1

3 files changed

Lines changed: 55 additions & 4 deletions

File tree

form.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ type Form struct {
7676
// to be more accessible to screen readers.
7777
accessible bool
7878

79-
quitting bool
80-
aborted bool
79+
// whether to quit the form after Submitting or not,
80+
// defaults to true
81+
QuitAfterSubmit bool
82+
quitting bool
83+
aborted bool
8184

8285
// options
8386
width int
@@ -105,6 +108,7 @@ func NewForm(groups ...*Group) *Form {
105108
teaOptions: []tea.ProgramOption{
106109
tea.WithOutput(os.Stderr),
107110
},
111+
QuitAfterSubmit: true,
108112
}
109113

110114
// 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 {
607611

608612
// View renders the form.
609613
func (f *Form) View() string {
610-
if f.quitting {
614+
if f.quitting && f.QuitAfterSubmit {
611615
return ""
612616
}
613617

huh_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,39 @@ func TestForm(t *testing.T) {
275275
// TODO: Finish and submit form.
276276
}
277277

278+
func TestFormNotQuiting(t *testing.T) {
279+
t.Run("Quit form after submitting", func(t *testing.T) {
280+
field := NewInput()
281+
f := NewForm(NewGroup(field))
282+
f.Update(f.Init())
283+
284+
f = submitForm(f)
285+
286+
view := ansi.Strip(f.View())
287+
288+
if strings.Contains(view, "enter submit") {
289+
t.Log(pretty.Render(view))
290+
t.Error("Expected form to not be visible but was.")
291+
}
292+
})
293+
294+
t.Run("Do not quit form after submitting", func(t *testing.T) {
295+
field := NewInput()
296+
f := NewForm(NewGroup(field))
297+
f.QuitAfterSubmit = false
298+
f.Update(f.Init())
299+
300+
submitForm(f)
301+
302+
view := ansi.Strip(f.View())
303+
304+
if !strings.Contains(view, "enter submit") {
305+
t.Log(pretty.Render(view))
306+
t.Error("Expected form to be visible but was not.")
307+
}
308+
})
309+
}
310+
278311
func TestInput(t *testing.T) {
279312
field := NewInput()
280313
f := NewForm(NewGroup(field))
@@ -925,6 +958,20 @@ func formProgram() *Form {
925958
WithAccessible(false)
926959
}
927960

961+
func submitForm(f *Form) *Form {
962+
m, cmd := f.Update(tea.KeyMsg{Type: tea.KeyEnter})
963+
f = m.(*Form)
964+
for {
965+
if cmd != nil {
966+
msg := cmd()
967+
_, cmd = f.Update(msg)
968+
} else {
969+
break
970+
}
971+
}
972+
return f
973+
}
974+
928975
func batchUpdate(m tea.Model, cmd tea.Cmd) tea.Model {
929976
if cmd == nil {
930977
return m

internal/selector/selector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (s *Selector[T]) Index() int {
5252
return s.index
5353
}
5454

55-
// Totoal returns the total number of items.
55+
// Total returns the total number of items.
5656
func (s *Selector[T]) Total() int {
5757
return len(s.items)
5858
}

0 commit comments

Comments
 (0)