Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGrid] Fix horizontal scroll not working on empty grid #3821

Merged
merged 10 commits into from
Feb 14, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
const scrollPosition = React.useRef({ top: 0, left: 0 });
const [containerWidth, setContainerWidth] = React.useState<number | null>(null);
const prevTotalWidth = React.useRef(columnsMeta.totalWidth);
const [shouldExtendContent, setShouldExtendContent] = React.useState(false);

const computeRenderContext = React.useCallback(() => {
if (disableVirtualization) {
Expand Down Expand Up @@ -150,6 +151,26 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {

useGridApiEventHandler(apiRef, GridEvents.resize, handleResize);

const handleContentSizeChange = React.useCallback(() => {
if (!apiRef.current.windowRef?.current) {
return;
}
setShouldExtendContent(
apiRef.current.windowRef.current.scrollHeight <=
apiRef.current.windowRef.current.clientHeight,
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This callback will be called when the virtualScrollerContentSizeChange event is fired and the event is fired when contentSize changes, which happens inside the hook. We can remove this state update and migrate the minHeight to contentSize, avoiding one rerender.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!
I noticed that the event is emitted in the same hook, but didn't think of removing unnecessary state

}, [apiRef]);

const contentStyle = {
minHeight: shouldExtendContent ? '100%' : 'auto',
};

useGridApiEventHandler(
apiRef,
GridEvents.virtualScrollerContentSizeChange,
handleContentSizeChange,
);

const getRenderableIndexes = ({ firstIndex, lastIndex, buffer, minFirstIndex, maxLastIndex }) => {
return [
clamp(firstIndex - buffer, minFirstIndex, maxLastIndex),
Expand Down Expand Up @@ -371,7 +392,10 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
style: { ...style, ...rootStyle },
...other,
}),
getContentProps: ({ style = {} } = {}) => ({ style: { ...style, ...contentSize } }),
getContentProps: ({ style = {} } = {}) => ({
style: { ...contentStyle, ...style, ...contentSize },
}),
getRenderZoneProps: () => ({ ref: renderZoneRef }),
shouldExtendContent,
};
};
24 changes: 2 additions & 22 deletions packages/grid/x-data-grid-pro/src/DataGridProVirtualScroller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ const DataGridProVirtualScroller = React.forwardRef<
const visibleColumnFields = useGridSelector(apiRef, gridVisibleColumnFieldsSelector);
const leftColumns = React.useRef<HTMLDivElement>(null);
const rightColumns = React.useRef<HTMLDivElement>(null);
const [shouldExtendContent, setShouldExtendContent] = React.useState(false);

const handleRenderZonePositioning = React.useCallback(({ top }) => {
if (leftColumns.current) {
Expand All @@ -144,6 +143,7 @@ const DataGridProVirtualScroller = React.forwardRef<
getContentProps,
getRenderZoneProps,
updateRenderZonePosition,
shouldExtendContent,
} = useGridVirtualScroller({
ref,
renderZoneMinColumnIndex: leftPinnedColumns.length,
Expand All @@ -165,22 +165,6 @@ const DataGridProVirtualScroller = React.forwardRef<
refreshRenderZonePosition();
}, [refreshRenderZonePosition]);

const handleContentSizeChange = React.useCallback(() => {
if (!apiRef.current.windowRef?.current) {
return;
}
setShouldExtendContent(
apiRef.current.windowRef.current.scrollHeight <=
apiRef.current.windowRef.current.clientHeight,
);
}, [apiRef]);

useGridApiEventHandler(
apiRef,
GridEvents.virtualScrollerContentSizeChange,
handleContentSizeChange,
);

const leftRenderContext =
renderContext && leftPinnedColumns.length > 0
? {
Expand All @@ -199,17 +183,13 @@ const DataGridProVirtualScroller = React.forwardRef<
}
: null;

const contentStyle = {
minHeight: shouldExtendContent ? '100%' : 'auto',
};

const pinnedColumnsStyle = {
minHeight: shouldExtendContent ? '100%' : 'auto',
cherniavskii marked this conversation as resolved.
Show resolved Hide resolved
};

return (
<GridVirtualScroller {...getRootProps(other)}>
<GridVirtualScrollerContent {...getContentProps({ style: contentStyle })}>
<GridVirtualScrollerContent {...getContentProps()}>
{leftRenderContext && (
<VirtualScrollerPinnedColumns
ref={leftColumns}
Expand Down