From bd9d1394759a399696d97041e2aee97e575aa256 Mon Sep 17 00:00:00 2001 From: Pierre Poupin Date: Mon, 29 Apr 2024 18:33:22 +0200 Subject: [PATCH] fix(lists): fix lists not working properly with few elements with stick-to-start --- .../virtualizedList/helpers/getLastItemIndex.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/lib/src/spatial-navigation/components/virtualizedList/helpers/getLastItemIndex.ts b/packages/lib/src/spatial-navigation/components/virtualizedList/helpers/getLastItemIndex.ts index 653e9cf9..be6d7ee5 100644 --- a/packages/lib/src/spatial-navigation/components/virtualizedList/helpers/getLastItemIndex.ts +++ b/packages/lib/src/spatial-navigation/components/virtualizedList/helpers/getLastItemIndex.ts @@ -41,7 +41,13 @@ export const getLastLeftItemIndex = ( return 0; } - return data.length - Math.floor(listSizeInPx / itemSizeInPx); + + const result = data.length - Math.floor(listSizeInPx / itemSizeInPx); + + if (result < 0) { + return 0; + } + return result; }; /** @@ -88,6 +94,11 @@ export const getLastRightItemIndex = ( return data.length - 1; } - // We substract 1 because index starts from 0 - return Math.floor(listSizeInPx / itemSizeInPx) - 1; + const result = Math.floor(listSizeInPx / itemSizeInPx) - 1; + + if (result > data.length - 1) { + // We substract 1 because index starts from 0 + return data.length - 1; + } + return result; };