Skip to content

Commit

Permalink
feat: WithOnConfirm and WithOnAbort
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Nov 6, 2023
1 parent 2364c67 commit a8db65a
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

var (
// ErrUserAborted is the error returned when a user exits the form before submitting.
ErrUserAborted = errors.New("user aborted")
)
// ErrUserAborted is the error returned when a user exits the form before submitting.
var ErrUserAborted = errors.New("user aborted")

// Form is a collection of groups that are displayed one at a time on a "page".
//
Expand All @@ -24,6 +22,9 @@ type Form struct {
// navigation
paginator paginator.Model

onConfirm func() (tea.Model, tea.Cmd)
onAbort func() (tea.Model, tea.Cmd)

// whether or not to use bubble tea rendering for accessibility
// purposes, if true, the form will render with basic prompting primitives
// to be more accessible to screen readers.
Expand All @@ -47,7 +48,7 @@ func NewForm(groups ...*Group) *Form {
p := paginator.New()
p.SetTotalPages(len(groups))

f := Form{
f := &Form{
groups: groups,
paginator: p,
theme: NewCharmTheme(),
Expand All @@ -60,8 +61,10 @@ func NewForm(groups ...*Group) *Form {
f.WithTheme(f.theme)
f.WithKeyMap(f.keymap)
f.WithWidth(f.width)
f.onConfirm = func() (tea.Model, tea.Cmd) { return f, tea.Quit }
f.onAbort = func() (tea.Model, tea.Cmd) { return f, tea.Quit }

return &f
return f
}

// Field is a primitive of a form.
Expand Down Expand Up @@ -118,6 +121,16 @@ func prevGroup() tea.Msg {
return prevGroupMsg{}
}

func (f *Form) WithOnConfirm(fn func() (tea.Model, tea.Cmd)) *Form {
f.onConfirm = fn
return f
}

func (f *Form) WithOnAbort(fn func() (tea.Model, tea.Cmd)) *Form {
f.onAbort = fn
return f
}

// WithAccessible sets the form to run in accessible mode to avoid redrawing the
// views which makes it easier for screen readers to read and describe the form.
//
Expand Down Expand Up @@ -206,7 +219,7 @@ func (f *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, f.keymap.Quit):
f.aborted = true
f.quitting = true
return f, tea.Quit
return f.onAbort(f)

Check failure on line 222 in form.go

View workflow job for this annotation

GitHub Actions / test (^1, ubuntu-latest)

too many arguments in call to f.onAbort

Check failure on line 222 in form.go

View workflow job for this annotation

GitHub Actions / lint

too many arguments in call to f.onAbort

Check failure on line 222 in form.go

View workflow job for this annotation

GitHub Actions / test (^1, macos-latest)

too many arguments in call to f.onAbort

Check failure on line 222 in form.go

View workflow job for this annotation

GitHub Actions / lint-soft

too many arguments in call to f.onAbort
}

case nextGroupMsg:
Expand All @@ -216,7 +229,7 @@ func (f *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

if f.paginator.OnLastPage() {
f.quitting = true
return f, tea.Quit
return f.onConfirm(f)

Check failure on line 232 in form.go

View workflow job for this annotation

GitHub Actions / test (^1, ubuntu-latest)

too many arguments in call to f.onConfirm

Check failure on line 232 in form.go

View workflow job for this annotation

GitHub Actions / lint

too many arguments in call to f.onConfirm

Check failure on line 232 in form.go

View workflow job for this annotation

GitHub Actions / test (^1, macos-latest)

too many arguments in call to f.onConfirm

Check failure on line 232 in form.go

View workflow job for this annotation

GitHub Actions / lint-soft

too many arguments in call to f.onConfirm
}
f.paginator.NextPage()

Expand Down

0 comments on commit a8db65a

Please sign in to comment.