Skip to content

Commit ae1428a

Browse files
author
҈҈҈Luiz Branco
authored
Fix pagination panic (#20)
1 parent 6b11c25 commit ae1428a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

list/list.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,12 @@ func (l *List) PageDown() {
9191
start := l.start + l.size
9292
max := len(l.items) - l.size
9393

94-
if start > max {
94+
switch {
95+
case len(l.items) < l.size:
96+
l.start = 0
97+
case start > max:
9598
l.start = max
96-
} else {
99+
default:
97100
l.start = start
98101
}
99102

list/list_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ func TestListMovement(t *testing.T) {
8989
}
9090
}
9191

92+
func TestListPageDown(t *testing.T) {
93+
t.Run("when list has fewer items than page size", func(t *testing.T) {
94+
letters := []rune{'a', 'b'}
95+
l, err := New(letters, 4)
96+
if err != nil {
97+
t.Fatalf("Expected no error, got %v", err)
98+
}
99+
100+
l.PageDown()
101+
list, idx := l.Items()
102+
103+
expected := 'b'
104+
selected := list[idx]
105+
106+
if selected != expected {
107+
t.Errorf("expected selected to be %q, got %q", expected, selected)
108+
}
109+
})
110+
}
111+
92112
func castList(list []interface{}) []rune {
93113
result := make([]rune, len(list))
94114
for i, l := range list {

0 commit comments

Comments
 (0)