Add ref controls to infinite grid
This commit is contained in:
parent
520b7ce136
commit
26ea4c0cc9
1 changed files with 133 additions and 113 deletions
|
@ -1,4 +1,12 @@
|
||||||
import { useState, useEffect, useRef, useMemo, useCallback } from 'react';
|
import {
|
||||||
|
useState,
|
||||||
|
useRef,
|
||||||
|
useMemo,
|
||||||
|
useCallback,
|
||||||
|
forwardRef,
|
||||||
|
Ref,
|
||||||
|
useImperativeHandle,
|
||||||
|
} from 'react';
|
||||||
import debounce from 'lodash/debounce';
|
import debounce from 'lodash/debounce';
|
||||||
import type { FixedSizeListProps } from 'react-window';
|
import type { FixedSizeListProps } from 'react-window';
|
||||||
import InfiniteLoader from 'react-window-infinite-loader';
|
import InfiniteLoader from 'react-window-infinite-loader';
|
||||||
|
@ -6,6 +14,12 @@ import { VirtualGridWrapper } from '/@/renderer/components/virtual-grid/virtual-
|
||||||
import type { CardRoute, CardRow, LibraryItem, PlayQueueAddOptions } from '/@/renderer/types';
|
import type { CardRoute, CardRow, LibraryItem, PlayQueueAddOptions } from '/@/renderer/types';
|
||||||
import { CardDisplayType } from '/@/renderer/types';
|
import { CardDisplayType } from '/@/renderer/types';
|
||||||
|
|
||||||
|
export type VirtualInfiniteGridRef = {
|
||||||
|
resetLoadMoreItemsCache: () => void;
|
||||||
|
scrollTo: (index: number) => void;
|
||||||
|
setItemData: (data: any[]) => void;
|
||||||
|
};
|
||||||
|
|
||||||
interface VirtualGridProps extends Omit<FixedSizeListProps, 'children' | 'itemSize'> {
|
interface VirtualGridProps extends Omit<FixedSizeListProps, 'children' | 'itemSize'> {
|
||||||
cardRows: CardRow<any>[];
|
cardRows: CardRow<any>[];
|
||||||
display?: CardDisplayType;
|
display?: CardDisplayType;
|
||||||
|
@ -15,7 +29,6 @@ interface VirtualGridProps extends Omit<FixedSizeListProps, 'children' | 'itemSi
|
||||||
itemSize: number;
|
itemSize: number;
|
||||||
itemType: LibraryItem;
|
itemType: LibraryItem;
|
||||||
minimumBatchSize?: number;
|
minimumBatchSize?: number;
|
||||||
refresh?: any;
|
|
||||||
route?: CardRoute;
|
route?: CardRoute;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +40,9 @@ const constrainWidth = (width: number) => {
|
||||||
return 1920;
|
return 1920;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const VirtualInfiniteGrid = ({
|
export const VirtualInfiniteGrid = forwardRef(
|
||||||
|
(
|
||||||
|
{
|
||||||
itemCount,
|
itemCount,
|
||||||
itemGap,
|
itemGap,
|
||||||
itemSize,
|
itemSize,
|
||||||
|
@ -42,8 +57,9 @@ export const VirtualInfiniteGrid = ({
|
||||||
initialScrollOffset,
|
initialScrollOffset,
|
||||||
height,
|
height,
|
||||||
width,
|
width,
|
||||||
refresh,
|
}: VirtualGridProps,
|
||||||
}: VirtualGridProps) => {
|
ref: Ref<VirtualInfiniteGridRef>,
|
||||||
|
) => {
|
||||||
const [itemData, setItemData] = useState<any[]>([]);
|
const [itemData, setItemData] = useState<any[]>([]);
|
||||||
const listRef = useRef<any>(null);
|
const listRef = useRef<any>(null);
|
||||||
const loader = useRef<InfiniteLoader>(null);
|
const loader = useRef<InfiniteLoader>(null);
|
||||||
|
@ -100,16 +116,20 @@ export const VirtualInfiniteGrid = ({
|
||||||
|
|
||||||
const debouncedLoadMoreItems = debounce(loadMoreItems, 500);
|
const debouncedLoadMoreItems = debounce(loadMoreItems, 500);
|
||||||
|
|
||||||
useEffect(() => {
|
useImperativeHandle(ref, () => ({
|
||||||
|
resetLoadMoreItemsCache: () => {
|
||||||
if (loader.current) {
|
if (loader.current) {
|
||||||
listRef.current.scrollTo(0);
|
|
||||||
loader.current.resetloadMoreItemsCache(false);
|
loader.current.resetloadMoreItemsCache(false);
|
||||||
setItemData(() => []);
|
setItemData(() => []);
|
||||||
|
|
||||||
loadMoreItems(0, minimumBatchSize! * 2);
|
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
},
|
||||||
}, [minimumBatchSize, fetchFn, refresh]);
|
scrollTo: (index: number) => {
|
||||||
|
listRef.current.scrollToItem(index);
|
||||||
|
},
|
||||||
|
setItemData: (data: any[]) => {
|
||||||
|
setItemData(data);
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<InfiniteLoader
|
<InfiniteLoader
|
||||||
|
@ -147,11 +167,11 @@ export const VirtualInfiniteGrid = ({
|
||||||
)}
|
)}
|
||||||
</InfiniteLoader>
|
</InfiniteLoader>
|
||||||
);
|
);
|
||||||
};
|
},
|
||||||
|
);
|
||||||
|
|
||||||
VirtualInfiniteGrid.defaultProps = {
|
VirtualInfiniteGrid.defaultProps = {
|
||||||
display: CardDisplayType.CARD,
|
display: CardDisplayType.CARD,
|
||||||
minimumBatchSize: 20,
|
minimumBatchSize: 20,
|
||||||
refresh: undefined,
|
|
||||||
route: undefined,
|
route: undefined,
|
||||||
};
|
};
|
||||||
|
|
Reference in a new issue