From dab9e294f9775703a29d61600580e6cb627496a2 Mon Sep 17 00:00:00 2001 From: JulienIzz Date: Wed, 18 Sep 2024 11:59:53 +0200 Subject: [PATCH] chore: handle undefined index accesses --- .../getNumberOfItemsVisibleOnScreen.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/lib/src/spatial-navigation/components/virtualizedList/helpers/getNumberOfItemsVisibleOnScreen.ts b/packages/lib/src/spatial-navigation/components/virtualizedList/helpers/getNumberOfItemsVisibleOnScreen.ts index cb9bff03..edb9aaff 100644 --- a/packages/lib/src/spatial-navigation/components/virtualizedList/helpers/getNumberOfItemsVisibleOnScreen.ts +++ b/packages/lib/src/spatial-navigation/components/virtualizedList/helpers/getNumberOfItemsVisibleOnScreen.ts @@ -9,17 +9,21 @@ const getMinSizeOfItems = ({ return itemSize; } - let minSize = itemSize(data[0]); // Initialize with the size of the first item + let minSize: number | undefined = undefined; - for (let i = 1; i < data.length; i++) { - const currentSize = itemSize(data[i]); - if (currentSize < minSize) { - minSize = currentSize; + for (let i = 0; i < data.length; i++) { + const item = data[i]; + if (item !== undefined) { + const currentSize = itemSize(item); + if (minSize === undefined || currentSize < minSize) { + minSize = currentSize; + } } } return minSize; }; + export const getNumberOfItemsVisibleOnScreen = ({ data, listSizeInPx, @@ -30,5 +34,10 @@ export const getNumberOfItemsVisibleOnScreen = ({ itemSize: number | ((item: T) => number); }) => { const itemSizeToComputeRanges = getMinSizeOfItems({ data, itemSize }); + + if (!itemSizeToComputeRanges) { + return 0; + } + return Math.floor(listSizeInPx / itemSizeToComputeRanges); };