From 5a20ae91150884b08276f3d5c7d4b8f892a6506e Mon Sep 17 00:00:00 2001 From: Matt Woenker Date: Wed, 1 May 2024 15:26:54 -0400 Subject: [PATCH] onItemsRendered takes rowStart into account --- src/components/Grid/Grid.tsx | 59 +++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/src/components/Grid/Grid.tsx b/src/components/Grid/Grid.tsx index af8fa684..d4ea9b56 100644 --- a/src/components/Grid/Grid.tsx +++ b/src/components/Grid/Grid.tsx @@ -11,7 +11,11 @@ import { useRef, useState, } from "react"; -import { GridOnScrollProps, VariableSizeGrid } from "react-window"; +import { + GridOnScrollProps, + VariableSizeGrid, + GridOnItemsRenderedProps, +} from "react-window"; import AutoSizer, { Size } from "react-virtualized-auto-sizer"; import RowNumberColumn from "./RowNumberColumn"; import Header from "./Header"; @@ -140,6 +144,7 @@ export const Grid = forwardRef( onCopyCallback, onContextMenu: onContextMenuProp, forwardedGridRef, + onItemsRendered: onItemsRenderedProp, ...props }, forwardedRef @@ -167,7 +172,7 @@ export const Grid = forwardRef( outerRef: outerRef, }); - if (onCopyCallback) { + if (onCopyCallback) { onCopyCallback(true); } if (showToast) { @@ -180,7 +185,7 @@ export const Grid = forwardRef( } catch (e) { console.error(e); - if (onCopyCallback) { + if (onCopyCallback) { onCopyCallback(false); } @@ -192,18 +197,28 @@ export const Grid = forwardRef( }); } } - }, [cell, columnCount, focus, focusProp, rowCount, selection, showToast, onCopyCallback]); + }, [ + cell, + columnCount, + focus, + focusProp, + rowCount, + selection, + showToast, + onCopyCallback, + ]); const customOnCopy: () => Promise = useMemo(() => { const result = async () => { - if(onCopyProp) { - await onCopyProp(selection, focus) + if (onCopyProp) { + await onCopyProp(selection, focus); } - } + }; return result; }, [onCopyProp, selection, focus]); - const onCopy: () => Promise = typeof onCopyProp === "function" ? customOnCopy: defaultOnCopy; + const onCopy: () => Promise = + typeof onCopyProp === "function" ? customOnCopy : defaultOnCopy; const defaultMenuOptions = [ { @@ -729,6 +744,33 @@ export const Grid = forwardRef( } }; + const lastItemsRenderedProps = useRef(); + const lastRowStart = useRef(rowStart); + + const onItemsRendered = useCallback( + (props: GridOnItemsRenderedProps) => { + lastItemsRenderedProps.current = props; + + return onItemsRenderedProp?.({ + ...props, + visibleRowStartIndex: props.visibleRowStartIndex + rowStart, + visibleRowStopIndex: props.visibleRowStopIndex + rowStart, + overscanRowStartIndex: props.overscanRowStartIndex + rowStart, + overscanRowStopIndex: props.overscanRowStopIndex + rowStart, + }); + }, + [onItemsRenderedProp, rowStart] + ); + + // handles the case where rowStart changes, make sure to inform caller + // of the new visible rows. + useEffect(() => { + if (lastItemsRenderedProps.current && rowStart !== lastRowStart.current) { + onItemsRendered(lastItemsRenderedProps.current); + lastRowStart.current = rowStart; + } + }, [rowStart, onItemsRendered]); + return ( ( onScroll={onScroll} outerRef={outerRef} outerElementType={OuterElementType} + onItemsRendered={onItemsRendered} {...props} > {Cell}