Skip to content

Commit

Permalink
Allow setting filter value directly with string (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evertras committed Jul 4, 2022
1 parent 5cbb890 commit 39fc51b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
31 changes: 31 additions & 0 deletions table/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,34 @@ func TestFilterWithExternalTextInput(t *testing.T) {

assert.Len(t, filteredRows, 1)
}

func TestFilterWithSetValue(t *testing.T) {
columns := []Column{NewColumn("title", "title", 10).WithFiltered(true)}
rows := []Row{
NewRow(RowData{
"title": "AAA",
"description": "",
}),
NewRow(RowData{
"title": "BBB",
"description": "",
}),
// Empty
NewRow(RowData{}),
}
model := New(columns).WithRows(rows).Filtered(true)
model = model.WithFilterInputValue("AaA")

filteredRows := model.getFilteredRows(rows)
assert.Len(t, filteredRows, 1)

// Make sure it holds true after an update
model, _ = model.Update(tea.KeyRight)
filteredRows = model.getFilteredRows(rows)
assert.Len(t, filteredRows, 1)

// Remove filter
model = model.WithFilterInputValue("")
filteredRows = model.getFilteredRows(rows)
assert.Len(t, filteredRows, 3)
}
9 changes: 9 additions & 0 deletions table/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,15 @@ func (m Model) WithFilterInput(input textinput.Model) Model {
return m
}

// WithFilterInputValue sets the filter value to the given string, immediately
// applying it as if the user had typed it in. Useful for external filter inputs
// that are not necessarily a text input.
func (m Model) WithFilterInputValue(value string) Model {
m.filterTextInput.SetValue(value)

return m
}

// WithFooterVisibility sets the visibility of the footer.
func (m Model) WithFooterVisibility(visibility bool) Model {
m.footerVisible = visibility
Expand Down

0 comments on commit 39fc51b

Please sign in to comment.