Skip to content

Commit

Permalink
Add events for filter input being focused/unfocused (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evertras authored Jul 20, 2022
1 parent b0b31f8 commit 9f2bfeb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
10 changes: 10 additions & 0 deletions table/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@ type UserEventRowSelectToggled struct {
RowIndex int
IsSelected bool
}

// UserEventFilterInputFocused indicates that the user has focused the filter
// text input, so that any other typing will type into the filter field. Only
// activates for the built-in filter text box.
type UserEventFilterInputFocused struct{}

// UserEventFilterInputUnfocused indicates that the user has unfocused the filter
// text input, which means the user is done typing into the filter field. Only
// activates for the built-in filter text box.
type UserEventFilterInputUnfocused struct{}
33 changes: 33 additions & 0 deletions table/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,36 @@ func TestUserEventRowSelectToggled(t *testing.T) {
events = model.GetLastUpdateUserEvents()
assert.Len(t, events, 0, "There's no row to select for an empty table, event shouldn't exist")
}

func TestFilterFocusEvents(t *testing.T) {
model := New([]Column{}).Filtered(true).Focused(true)

events := model.GetLastUpdateUserEvents()

assert.Empty(t, events, "Unexpected events to start")

// Start filter
model, _ = model.Update(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune{'/'},
})
events = model.GetLastUpdateUserEvents()
assert.Len(t, events, 1, "Only expected one event")
switch events[0].(type) {
case UserEventFilterInputFocused:
default:
assert.FailNow(t, "Unexpected event type")
}

// Stop filter
model, _ = model.Update(tea.KeyMsg{
Type: tea.KeyEnter,
})
events = model.GetLastUpdateUserEvents()
assert.Len(t, events, 1, "Only expected one event")
switch events[0].(type) {
case UserEventFilterInputUnfocused:
default:
assert.FailNow(t, "Unexpected event type")
}
}
10 changes: 9 additions & 1 deletion table/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (m *Model) handleKeypress(msg tea.KeyMsg) {

if key.Matches(msg, m.keyMap.Filter) {
m.filterTextInput.Focus()
m.appendUserEvent(UserEventFilterInputFocused{})
}

if key.Matches(msg, m.keyMap.FilterClear) {
Expand Down Expand Up @@ -124,7 +125,14 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
}

if m.filterTextInput.Focused() {
return m.updateFilterTextInput(msg)
var cmd tea.Cmd
m, cmd = m.updateFilterTextInput(msg)

if !m.filterTextInput.Focused() {
m.appendUserEvent(UserEventFilterInputUnfocused{})
}

return m, cmd
}

switch msg := msg.(type) {
Expand Down

0 comments on commit 9f2bfeb

Please sign in to comment.