Skip to content

Commit 9f2bfeb

Browse files
authored
Add events for filter input being focused/unfocused (#124)
1 parent b0b31f8 commit 9f2bfeb

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

table/events.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,13 @@ type UserEventRowSelectToggled struct {
4848
RowIndex int
4949
IsSelected bool
5050
}
51+
52+
// UserEventFilterInputFocused indicates that the user has focused the filter
53+
// text input, so that any other typing will type into the filter field. Only
54+
// activates for the built-in filter text box.
55+
type UserEventFilterInputFocused struct{}
56+
57+
// UserEventFilterInputUnfocused indicates that the user has unfocused the filter
58+
// text input, which means the user is done typing into the filter field. Only
59+
// activates for the built-in filter text box.
60+
type UserEventFilterInputUnfocused struct{}

table/events_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,36 @@ func TestUserEventRowSelectToggled(t *testing.T) {
172172
events = model.GetLastUpdateUserEvents()
173173
assert.Len(t, events, 0, "There's no row to select for an empty table, event shouldn't exist")
174174
}
175+
176+
func TestFilterFocusEvents(t *testing.T) {
177+
model := New([]Column{}).Filtered(true).Focused(true)
178+
179+
events := model.GetLastUpdateUserEvents()
180+
181+
assert.Empty(t, events, "Unexpected events to start")
182+
183+
// Start filter
184+
model, _ = model.Update(tea.KeyMsg{
185+
Type: tea.KeyRunes,
186+
Runes: []rune{'/'},
187+
})
188+
events = model.GetLastUpdateUserEvents()
189+
assert.Len(t, events, 1, "Only expected one event")
190+
switch events[0].(type) {
191+
case UserEventFilterInputFocused:
192+
default:
193+
assert.FailNow(t, "Unexpected event type")
194+
}
195+
196+
// Stop filter
197+
model, _ = model.Update(tea.KeyMsg{
198+
Type: tea.KeyEnter,
199+
})
200+
events = model.GetLastUpdateUserEvents()
201+
assert.Len(t, events, 1, "Only expected one event")
202+
switch events[0].(type) {
203+
case UserEventFilterInputUnfocused:
204+
default:
205+
assert.FailNow(t, "Unexpected event type")
206+
}
207+
}

table/update.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func (m *Model) handleKeypress(msg tea.KeyMsg) {
9393

9494
if key.Matches(msg, m.keyMap.Filter) {
9595
m.filterTextInput.Focus()
96+
m.appendUserEvent(UserEventFilterInputFocused{})
9697
}
9798

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

126127
if m.filterTextInput.Focused() {
127-
return m.updateFilterTextInput(msg)
128+
var cmd tea.Cmd
129+
m, cmd = m.updateFilterTextInput(msg)
130+
131+
if !m.filterTextInput.Focused() {
132+
m.appendUserEvent(UserEventFilterInputUnfocused{})
133+
}
134+
135+
return m, cmd
128136
}
129137

130138
switch msg := msg.(type) {

0 commit comments

Comments
 (0)