From e63a51e0603cf3f70325f615f0926c87d2f7df71 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sun, 25 Jan 2026 03:25:39 +0100 Subject: [PATCH] Fix bug caused by GroupedVirtuoso If there are only empty groups, "computeItemKey" gets called with a none-existent index which leads to "Virtuoso.util::convertIndex" to throw an error --- src/lib/virtuoso/Virtuoso.util.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/lib/virtuoso/Virtuoso.util.tsx b/src/lib/virtuoso/Virtuoso.util.tsx index 05ebe359..99d74f3e 100644 --- a/src/lib/virtuoso/Virtuoso.util.tsx +++ b/src/lib/virtuoso/Virtuoso.util.tsx @@ -71,7 +71,7 @@ export class VirtuosoUtil { } } - throw new Error(`Unexpected "${index}" (${index}) and "groupCounts" (${groupCounts})`); + throw new Error(`Unexpected "index" (${index}) and "groupCounts" (${groupCounts})`); } static useCreateConvertIndex( @@ -97,8 +97,15 @@ export class VirtuosoUtil { ): (index: number) => React.Key { const convertIndex = this.useCreateConvertIndex(groupCounts); + const totalGroupItems = useMemo(() => groupCounts.reduce((acc, count) => acc + count, 0), [groupCounts]); + return useCallback( (index) => { + // GroupedVirtuoso bug: If there are only empty groups, computeItemKey gets called with a none-existent index which leads to "convertIndex" to throw an error + if (index > groupCounts.length - 1 && totalGroupItems === 0) { + return -1; + } + const { type, index: convertedIndex, groupIndex } = convertIndex(index); switch (type) {