Skip to content

Commit

Permalink
feat(input): expose suggestions for autocomplete (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani authored Jan 4, 2024
1 parent 16fbf39 commit a92fbe9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/git/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {

huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().Title("Type").Value(&commit).Options(huh.NewOptions(types...)...),
huh.NewInput().Title("Type").Value(&commit).Placeholder("feat").Suggestions(types),
huh.NewInput().Title("Scope").Value(&scope).Placeholder("scope"),
),
huh.NewGroup(
Expand Down
12 changes: 12 additions & 0 deletions field_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ func (i *Input) CharLimit(charlimit int) *Input {
return i
}

// Suggestions sets the suggestions to display for autocomplete in the input
// field.
func (i *Input) Suggestions(suggestions []string) *Input {
i.textinput.ShowSuggestions = len(suggestions) > 0
i.textinput.SetSuggestions(suggestions)
return i
}

// Password sets whether or not to hide the input while the user is typing.
func (i *Input) Password(password bool) *Input {
if password {
Expand Down Expand Up @@ -139,6 +147,9 @@ func (i *Input) Blur() tea.Cmd {

// KeyBinds returns the help message for the input field.
func (i *Input) KeyBinds() []key.Binding {
if i.textinput.ShowSuggestions {
return []key.Binding{i.keymap.Next, i.keymap.Prev, i.keymap.AcceptSuggestion}
}
return []key.Binding{i.keymap.Next, i.keymap.Prev}
}

Expand Down Expand Up @@ -241,6 +252,7 @@ func (i *Input) runAccessible() error {
// WithKeyMap sets the keymap on an input field.
func (i *Input) WithKeyMap(k *KeyMap) Field {
i.keymap = &k.Input
i.textinput.KeyMap.AcceptSuggestion = i.keymap.AcceptSuggestion
return i
}

Expand Down
10 changes: 6 additions & 4 deletions keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ type KeyMap struct {

// InputKeyMap is the keybindings for input fields.
type InputKeyMap struct {
Next key.Binding
Prev key.Binding
AcceptSuggestion key.Binding
Next key.Binding
Prev key.Binding
}

// TextKeyMap is the keybindings for text fields.
Expand Down Expand Up @@ -69,8 +70,9 @@ func NewDefaultKeyMap() *KeyMap {
return &KeyMap{
Quit: key.NewBinding(key.WithKeys("ctrl+c")),
Input: InputKeyMap{
Next: key.NewBinding(key.WithKeys("enter", "tab"), key.WithHelp("enter", "next")),
Prev: key.NewBinding(key.WithKeys("shift+tab"), key.WithHelp("shift+tab", "back")),
AcceptSuggestion: key.NewBinding(key.WithKeys("ctrl+e"), key.WithHelp("ctrl+e", "complete")),
Next: key.NewBinding(key.WithKeys("enter", "tab"), key.WithHelp("enter", "next")),
Prev: key.NewBinding(key.WithKeys("shift+tab"), key.WithHelp("shift+tab", "back")),
},
Text: TextKeyMap{
Next: key.NewBinding(key.WithKeys("tab", "enter"), key.WithHelp("enter", "next")),
Expand Down

0 comments on commit a92fbe9

Please sign in to comment.