Skip to content

Commit

Permalink
Persist selected column when columns are regenerated. (#166)
Browse files Browse the repository at this point in the history
* Add test to reproduce bug

* Add the selectable column back in when columns are redone
  • Loading branch information
Evertras authored Feb 25, 2024
1 parent 53205ee commit b32efb0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions table/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ func (m Model) WithColumns(columns []Column) Model {

m.recalculateWidth()

if m.selectableRows {
// Re-add the selectable column
m = m.SelectableRows(true)
}

return m
}

Expand Down
41 changes: 41 additions & 0 deletions table/view_selectable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,44 @@ func TestSimple3x3WithCustomSelectableTextAndFooter(t *testing.T) {

assert.Equal(t, expectedTable, rendered)
}

func TestRegeneratingColumnsKeepsSelectableText(t *testing.T) {
columns := []Column{
NewColumn("1", "1", 4),
NewColumn("2", "2", 4),
NewColumn("3", "3", 4),
}

model := New(columns)

rows := []Row{}

for rowIndex := 1; rowIndex <= 3; rowIndex++ {
rowData := RowData{}

for columnIndex := 1; columnIndex <= 3; columnIndex++ {
id := fmt.Sprintf("%d", columnIndex)

rowData[id] = fmt.Sprintf("%d,%d", columnIndex, rowIndex)
}

rows = append(rows, NewRow(rowData))
}

model = model.WithRows(rows).
SelectableRows(true).
WithSelectedText(" ", "✓").
WithColumns(columns)

const expectedTable = `┏━┳━━━━┳━━━━┳━━━━┓
┃✓┃ 1┃ 2┃ 3┃
┣━╋━━━━╋━━━━╋━━━━┫
┃ ┃ 1,1┃ 2,1┃ 3,1┃
┃ ┃ 1,2┃ 2,2┃ 3,2┃
┃ ┃ 1,3┃ 2,3┃ 3,3┃
┗━┻━━━━┻━━━━┻━━━━┛`

rendered := model.View()

assert.Equal(t, expectedTable, rendered)
}

0 comments on commit b32efb0

Please sign in to comment.