Skip to content

Commit bd7f3ff

Browse files
committed
Invalidate cache when filter func applied and add test
1 parent c487e82 commit bd7f3ff

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

table/filter_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,50 @@ func TestFilterWithSetValue(t *testing.T) {
294294
assert.Len(t, filteredRows, 3)
295295
}
296296

297+
func TestFilterFunc(t *testing.T) {
298+
const (
299+
colTitle = "title"
300+
colDesc = "description"
301+
)
302+
303+
columns := []Column{NewColumn("title", "title", 10).WithFiltered(true)}
304+
rows := []Row{
305+
NewRow(RowData{
306+
colTitle: "AAA",
307+
colDesc: "",
308+
}),
309+
NewRow(RowData{
310+
colTitle: "BBB",
311+
colDesc: "",
312+
}),
313+
// Empty
314+
NewRow(RowData{}),
315+
}
316+
317+
filterFunc := func(r Row, s string) bool {
318+
// Completely arbitrary check for testing purposes
319+
title := fmt.Sprintf("%v", r.Data["title"])
320+
return title == "AAA" && s == "x"
321+
}
322+
323+
// First check that the table won't match with different case
324+
model := New(columns).WithRows(rows).Filtered(true)
325+
model = model.WithFilterInputValue("x")
326+
327+
filteredRows := model.getFilteredRows(rows)
328+
assert.Len(t, filteredRows, 0)
329+
330+
// The filter func should then match the one row
331+
model = model.WithFilterFunc(filterFunc)
332+
filteredRows = model.getFilteredRows(rows)
333+
assert.Len(t, filteredRows, 1)
334+
335+
// Remove filter
336+
model = model.WithFilterInputValue("")
337+
filteredRows = model.getFilteredRows(rows)
338+
assert.Len(t, filteredRows, 3)
339+
}
340+
297341
func BenchmarkFilteredScrolling(b *testing.B) {
298342
// Scrolling through a filtered table with many rows should be quick
299343
// https://github.com/Evertras/bubble-table/issues/135

table/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ func (m Model) WithFilterInputValue(value string) Model {
371371
func (m Model) WithFilterFunc(shouldInclude func(row Row, filterInput string) bool) Model {
372372
m.filterFunc = shouldInclude
373373

374+
m.visibleRowCacheUpdated = false
375+
374376
return m
375377
}
376378

0 commit comments

Comments
 (0)