Skip to content

Commit

Permalink
fix: filter not works when using StyledCell (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
alswl authored Feb 13, 2023
1 parent 7701b14 commit 6973eda
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
15 changes: 9 additions & 6 deletions table/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,19 @@ func isRowMatched(columns []Column, row Row, filter string) bool {
continue
}

target := ""
switch dataV := data.(type) {
case string:
if strings.Contains(strings.ToLower(dataV), strings.ToLower(filter)) {
return true
}
target = dataV

case fmt.Stringer:
if strings.Contains(strings.ToLower(dataV.String()), strings.ToLower(filter)) {
return true
}
target = dataV.String()

case StyledCell:
target = dataV.Data.(string)
}
if strings.Contains(strings.ToLower(target), strings.ToLower(filter)) {
return true
}
}

Expand Down
19 changes: 19 additions & 0 deletions table/filter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package table

import (
"github.com/charmbracelet/lipgloss"
"testing"
"time"

Expand Down Expand Up @@ -67,6 +68,24 @@ func TestIsRowMatched(t *testing.T) {
))
}

func TestIsRowMatchedForStyled(t *testing.T) {
columns := []Column{
NewColumn("title", "title", 10).WithFiltered(true),
NewColumn("description", "description", 10)}

assert.True(t, isRowMatched(columns,
NewRow(RowData{
"title": "AAA",
"description": "",
}), "AA"))

assert.True(t, isRowMatched(columns,
NewRow(RowData{
"title": NewStyledCell("AAA", lipgloss.NewStyle()),
"description": "",
}), "AA"))
}

func TestGetFilteredRowsNoColumnFiltered(t *testing.T) {
columns := []Column{NewColumn("title", "title", 10)}
rows := []Row{
Expand Down

0 comments on commit 6973eda

Please sign in to comment.