Skip to content

Commit

Permalink
Add shortcut to clear filter and escape filtering (#65)
Browse files Browse the repository at this point in the history
* Add shortcut to clear filter and escape filtering

* Make blur keybind configurable
  • Loading branch information
Evertras authored Apr 19, 2022
1 parent ddc8f61 commit 3e8eabe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions examples/filter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc", "q":
case "ctrl+c", "q":
cmds = append(cmds, tea.Quit)
}

Expand All @@ -84,7 +84,7 @@ func (m Model) View() string {
body := strings.Builder{}

body.WriteString("A filtered simple default table\n" +
"Currently filter by Title and Author, press / to filter.\nPress q or ctrl+c to quit\n\n")
"Currently filter by Title and Author, press / + letters to start filtering, and escape to clear filter.\nPress q or ctrl+c to quit\n\n")

body.WriteString(m.table.View())

Expand Down
13 changes: 13 additions & 0 deletions table/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ type KeyMap struct {
PageFirst key.Binding
PageLast key.Binding

// Filter allows the user to start typing and filter the rows.
Filter key.Binding

// FilterBlur is the key that stops the user's input from typing into the filter.
FilterBlur key.Binding

// FilterClear will clear the filter while it's blurred.
FilterClear key.Binding
}

// DefaultKeyMap returns a set of sensible defaults for controlling a focused table.
Expand Down Expand Up @@ -44,5 +51,11 @@ func DefaultKeyMap() KeyMap {
Filter: key.NewBinding(
key.WithKeys("/"),
),
FilterBlur: key.NewBinding(
key.WithKeys("enter", "esc"),
),
FilterClear: key.NewBinding(
key.WithKeys("esc"),
),
}
}
5 changes: 4 additions & 1 deletion table/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (m Model) updateFilterTextInput(msg tea.Msg) (Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.Type == tea.KeyEnter {
if key.Matches(msg, m.keyMap.FilterBlur) {
m.filterTextInput.Blur()
}
}
Expand Down Expand Up @@ -84,6 +84,9 @@ func (m *Model) handleKeypress(msg tea.KeyMsg) {

case key.Matches(msg, m.keyMap.Filter):
m.filterTextInput.Focus()

case key.Matches(msg, m.keyMap.FilterClear):
m.filterTextInput.Reset()
}
}

Expand Down
12 changes: 12 additions & 0 deletions table/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ func TestFilterWithKeypresses(t *testing.T) {
model, _ = model.Update(tea.KeyMsg{Type: tea.KeyEnter})
}

hitEscape := func() {
model, _ = model.Update(tea.KeyMsg{Type: tea.KeyEscape})
}

visible := model.GetVisibleRows()

assert.Len(t, visible, 2)
Expand All @@ -310,5 +314,13 @@ func TestFilterWithKeypresses(t *testing.T) {

hitKey('x')

visible = model.GetVisibleRows()

assert.Len(t, visible, 1)

hitEscape()

visible = model.GetVisibleRows()

assert.Len(t, visible, 2)
}

0 comments on commit 3e8eabe

Please sign in to comment.