From 2a19f7510ca8551930780ccb27319c78fb51119f Mon Sep 17 00:00:00 2001 From: jon4hz Date: Wed, 25 Jan 2023 23:59:09 +0100 Subject: [PATCH] feat(list): infinite scrolling --- list/list.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/list/list.go b/list/list.go index 68d0f27c..46a3bec2 100644 --- a/list/list.go +++ b/list/list.go @@ -132,8 +132,9 @@ type Model struct { itemNameSingular string itemNamePlural string - Title string - Styles Styles + Title string + Styles Styles + InfiniteScrolling bool // Key mappings for navigating the list. KeyMap KeyMap @@ -459,6 +460,13 @@ func (m *Model) CursorUp() { // If we're at the start, stop if m.cursor < 0 && m.Paginator.Page == 0 { + // if infinite scrolling is enabled, go to the last item + if m.InfiniteScrolling { + m.Paginator.Page = m.Paginator.TotalPages - 1 + m.cursor = m.Paginator.ItemsOnPage(len(m.VisibleItems())) - 1 + return + } + m.cursor = 0 return } @@ -501,6 +509,12 @@ func (m *Model) CursorDown() { } m.cursor = itemsOnPage - 1 + + // if infinite scrolling is enabled, go to the first item + if m.InfiniteScrolling { + m.Paginator.Page = 0 + m.cursor = 0 + } } // PrevPage moves to the previous page, if available.