refactor grid list to support index offset

This commit is contained in:
jeffvli
2025-11-14 01:14:35 -08:00
parent 86d880ed6c
commit 03f6981c11
6 changed files with 39 additions and 9 deletions
@@ -8,6 +8,7 @@ import React, {
CSSProperties, CSSProperties,
memo, memo,
ReactNode, ReactNode,
Ref,
RefObject, RefObject,
useCallback, useCallback,
useEffect, useEffect,
@@ -150,10 +151,21 @@ const VirtualizedGridList = React.memo(
return null; return null;
} }
const calculateInitialScrollOffset = (): number => {
if (!initialTop) return 0;
if (initialTop.type === 'offset') {
return initialTop.to;
}
const rowIndex = Math.floor(initialTop.to / (tableMeta?.columnCount || 1));
return rowIndex * (tableMeta?.itemHeight || 0);
};
return ( return (
<FixedSizeList <FixedSizeList
height={height} height={height}
initialScrollOffset={initialTop || 0} initialScrollOffset={calculateInitialScrollOffset()}
itemCount={itemData.tableMeta?.rowCount || 0} itemCount={itemData.tableMeta?.rowCount || 0}
itemData={itemData} itemData={itemData}
itemSize={itemData.tableMeta?.itemHeight || 0} itemSize={itemData.tableMeta?.itemHeight || 0}
@@ -251,13 +263,16 @@ export interface ItemGridListProps {
enableSelection?: boolean; enableSelection?: boolean;
gap?: 'lg' | 'md' | 'sm' | 'xl' | 'xs'; gap?: 'lg' | 'md' | 'sm' | 'xl' | 'xs';
getRowId?: ((item: unknown) => string) | string; getRowId?: ((item: unknown) => string) | string;
initialTop?: number; initialTop?: {
to: number;
type: 'index' | 'offset';
};
itemsPerRow?: number; itemsPerRow?: number;
itemType: LibraryItem; itemType: LibraryItem;
onRangeChanged?: (range: { startIndex: number; stopIndex: number }) => void; onRangeChanged?: (range: { startIndex: number; stopIndex: number }) => void;
onScroll?: (offset: number, direction: 'down' | 'up') => void; onScroll?: (offset: number, direction: 'down' | 'up') => void;
onScrollEnd?: (offset: number, direction: 'down' | 'up') => void; onScrollEnd?: (offset: number, direction: 'down' | 'up') => void;
ref?: RefObject<ItemListHandle>; ref?: Ref<ItemListHandle>;
} }
export const ItemGridList = ({ export const ItemGridList = ({
@@ -503,7 +518,7 @@ export const ItemGridList = ({
internalState.extractRowId(rangeItem) internalState.extractRowId(rangeItem)
) { ) {
rangeItems.push( rangeItems.push(
rangeItem as ItemListStateItemWithRequiredProperties, rangeItem as unknown as ItemListStateItemWithRequiredProperties,
); );
} }
} }
@@ -56,7 +56,10 @@ export const AlbumListInfiniteGrid = forwardRef<any, AlbumListInfiniteGridProps>
<ItemGridList <ItemGridList
data={data} data={data}
gap={gap} gap={gap}
initialTop={scrollOffset ?? 0} initialTop={{
to: scrollOffset ?? 0,
type: 'offset',
}}
itemsPerRow={itemsPerRow} itemsPerRow={itemsPerRow}
itemType={LibraryItem.ALBUM} itemType={LibraryItem.ALBUM}
onRangeChanged={onRangeChanged} onRangeChanged={onRangeChanged}
@@ -57,7 +57,10 @@ export const AlbumArtistListInfiniteGrid = forwardRef<any, AlbumArtistListInfini
<ItemGridList <ItemGridList
data={data} data={data}
gap={gap} gap={gap}
initialTop={scrollOffset ?? 0} initialTop={{
to: scrollOffset ?? 0,
type: 'offset',
}}
itemsPerRow={itemsPerRow} itemsPerRow={itemsPerRow}
itemType={LibraryItem.ALBUM_ARTIST} itemType={LibraryItem.ALBUM_ARTIST}
onRangeChanged={onRangeChanged} onRangeChanged={onRangeChanged}
@@ -57,7 +57,10 @@ export const ArtistListInfiniteGrid = forwardRef<any, ArtistListInfiniteGridProp
<ItemGridList <ItemGridList
data={data} data={data}
gap={gap} gap={gap}
initialTop={scrollOffset ?? 0} initialTop={{
to: scrollOffset ?? 0,
type: 'offset',
}}
itemsPerRow={itemsPerRow} itemsPerRow={itemsPerRow}
itemType={LibraryItem.ARTIST} itemType={LibraryItem.ARTIST}
onRangeChanged={onRangeChanged} onRangeChanged={onRangeChanged}
@@ -57,7 +57,10 @@ export const GenreListInfiniteGrid = forwardRef<any, GenreListInfiniteGridProps>
<ItemGridList <ItemGridList
data={data} data={data}
gap={gap} gap={gap}
initialTop={scrollOffset ?? 0} initialTop={{
to: scrollOffset ?? 0,
type: 'offset',
}}
itemsPerRow={itemsPerRow} itemsPerRow={itemsPerRow}
itemType={LibraryItem.GENRE} itemType={LibraryItem.GENRE}
onRangeChanged={onRangeChanged} onRangeChanged={onRangeChanged}
@@ -52,7 +52,10 @@ export const SongListInfiniteGrid = forwardRef<any, SongListInfiniteGridProps>(
<ItemGridList <ItemGridList
data={data} data={data}
gap={gap} gap={gap}
initialTop={scrollOffset ?? 0} initialTop={{
to: scrollOffset ?? 0,
type: 'offset',
}}
itemsPerRow={itemsPerRow} itemsPerRow={itemsPerRow}
itemType={LibraryItem.SONG} itemType={LibraryItem.SONG}
onRangeChanged={onRangeChanged} onRangeChanged={onRangeChanged}