Skip to content

Commit

Permalink
fix: make errors blocking on input
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Dec 12, 2023
1 parent e7c5847 commit 77c4239
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/burger/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -124,6 +125,12 @@ func main() {
Value(&order.Name).
Title("What's your name?").
Placeholder("Margaret Thatcher").
Validate(func(s string) error {
if s == "Frank" {
return errors.New("no franks, sorry")
}
return nil
}).
Description("For when your order is ready."),

huh.NewText().
Expand Down
10 changes: 10 additions & 0 deletions field_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,18 @@ func (i *Input) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

switch {
case key.Matches(msg, i.keymap.Prev):
value := i.textinput.Value()
i.err = i.validate(value)
if i.err != nil {
return i, nil
}
cmds = append(cmds, prevField)
case key.Matches(msg, i.keymap.Next):
value := i.textinput.Value()
i.err = i.validate(value)
if i.err != nil {
return i, nil
}
cmds = append(cmds, nextField)
}
}
Expand Down

0 comments on commit 77c4239

Please sign in to comment.