Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Default filter value (#146)
Browse files Browse the repository at this point in the history
* added test

* fixed bug causing select to use default value even if list is filtered

* fixed typo in test file
  • Loading branch information
AlecAivazis committed Jul 4, 2018
1 parent d54a5d5 commit 17861e1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,26 @@ func (s *Select) OnChange(line []rune, pos int, key rune) (newLine []rune, newPo
// only show the help message if we have one
} else if key == core.HelpInputRune && s.Help != "" {
s.showingHelp = true
// if the user wants to toggle vim mode on/off
} else if key == terminal.KeyEscape {
s.VimMode = !s.VimMode
// if the user hits any of the keys that clear the filter
} else if key == terminal.KeyDeleteWord || key == terminal.KeyDeleteLine {
s.filter = ""
// if the user is deleting a character in the filter
} else if key == terminal.KeyDelete || key == terminal.KeyBackspace {
// if there is content in the filter to delete
if s.filter != "" {
// subtract a line from the current filter
s.filter = s.filter[0 : len(s.filter)-1]
// we removed the last value in the filter
}
} else if key >= terminal.KeySpace {
s.filter += string(key)
// make sure vim mode is disabled
s.VimMode = false
// make sure that we use the current value in the filtered list
s.useDefault = false
}

s.FilterMessage = ""
Expand Down
15 changes: 15 additions & 0 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ func TestSelectPrompt(t *testing.T) {
},
"green",
},
{
"Can select the first result in a filtered list if there is a default",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
Default: "blue",
},
func(c *expect.Console) {
c.ExpectString("Choose a color:")
// Make sure only red is showing
c.SendLine("red")
c.ExpectEOF()
},
"red",
},
}

for _, test := range tests {
Expand Down

0 comments on commit 17861e1

Please sign in to comment.