Skip to content

Commit

Permalink
Merge pull request #402 from ClickHouse/mw-grid-fix-visible-range-wit…
Browse files Browse the repository at this point in the history
…h-row-start

onItemsRendered takes rowStart into account
  • Loading branch information
mwoenker authored May 2, 2024
2 parents 981139b + 54b42df commit bda2e4e
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions src/components/Grid/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -140,6 +144,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
onCopyCallback,
onContextMenu: onContextMenuProp,
forwardedGridRef,
onItemsRendered: onItemsRenderedProp,
...props
},
forwardedRef
Expand Down Expand Up @@ -167,7 +172,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
outerRef: outerRef,
});

if (onCopyCallback) {
if (onCopyCallback) {
onCopyCallback(true);
}
if (showToast) {
Expand All @@ -180,7 +185,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
} catch (e) {
console.error(e);

if (onCopyCallback) {
if (onCopyCallback) {
onCopyCallback(false);
}

Expand All @@ -192,18 +197,28 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
});
}
}
}, [cell, columnCount, focus, focusProp, rowCount, selection, showToast, onCopyCallback]);
}, [
cell,
columnCount,
focus,
focusProp,
rowCount,
selection,
showToast,
onCopyCallback,
]);

const customOnCopy: () => Promise<void> = useMemo(() => {
const result = async () => {
if(onCopyProp) {
await onCopyProp(selection, focus)
if (onCopyProp) {
await onCopyProp(selection, focus);
}
}
};
return result;
}, [onCopyProp, selection, focus]);

const onCopy: () => Promise<void> = typeof onCopyProp === "function" ? customOnCopy: defaultOnCopy;
const onCopy: () => Promise<void> =
typeof onCopyProp === "function" ? customOnCopy : defaultOnCopy;

const defaultMenuOptions = [
{
Expand Down Expand Up @@ -729,6 +744,33 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
}
};

const lastItemsRenderedProps = useRef<GridOnItemsRenderedProps>();
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 (
<ContextMenu
modal={false}
Expand Down Expand Up @@ -778,6 +820,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
onScroll={onScroll}
outerRef={outerRef}
outerElementType={OuterElementType}
onItemsRendered={onItemsRendered}
{...props}
>
{Cell}
Expand Down

0 comments on commit bda2e4e

Please sign in to comment.