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] Replace GridAutoSizer with ResizeObserver #8091

Merged
merged 7 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ describe('<DataGridPro /> - Rows', () => {
});
await act(() => Promise.resolve());
clock.runToLast();
expect(getRows()).to.have.length(4);
expect(getRows()).to.have.length(3);
});

it('should render last row when scrolling to the bottom', () => {
Expand Down
193 changes: 0 additions & 193 deletions packages/grid/x-data-grid/src/components/GridAutoSizer.tsx

This file was deleted.

20 changes: 9 additions & 11 deletions packages/grid/x-data-grid/src/components/GridHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import * as React from 'react';
import { useGridRootProps } from '../hooks/utils/useGridRootProps';

export const GridHeader = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
function GridHeader(props, ref) {
const rootProps = useGridRootProps();
export function GridHeader() {
const rootProps = useGridRootProps();

return (
<div ref={ref} {...props}>
Copy link
Member Author

@m4theushw m4theushw Mar 21, 2023

Choose a reason for hiding this comment

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

This DIV is useless.

<rootProps.slots.preferencesPanel {...rootProps.slotProps?.preferencesPanel} />
{rootProps.slots.toolbar && <rootProps.slots.toolbar {...rootProps.slotProps?.toolbar} />}
</div>
);
},
);
return (
<React.Fragment>
<rootProps.slots.preferencesPanel {...rootProps.slotProps?.preferencesPanel} />
{rootProps.slots.toolbar && <rootProps.slots.toolbar {...rootProps.slotProps?.toolbar} />}
</React.Fragment>
);
}
54 changes: 35 additions & 19 deletions packages/grid/x-data-grid/src/components/base/GridBody.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
import { useGridPrivateApiContext } from '../../hooks/utils/useGridPrivateApiContext';
import { useGridSelector } from '../../hooks/utils/useGridSelector';
import { ElementSize } from '../../models/elementSize';
import { GridMainContainer } from '../containers/GridMainContainer';
import { GridAutoSizer } from '../GridAutoSizer';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
import {
gridColumnPositionsSelector,
Expand All @@ -28,7 +27,6 @@ import {
import { gridColumnMenuSelector } from '../../hooks/features/columnMenu/columnMenuSelector';

interface GridBodyProps {
children?: React.ReactNode;
ColumnHeadersProps?: Record<string, any>;
VirtualScrollerComponent: React.JSXElementConstructor<
React.HTMLAttributes<HTMLDivElement> & {
Expand All @@ -39,9 +37,10 @@ interface GridBodyProps {
}

function GridBody(props: GridBodyProps) {
const { children, VirtualScrollerComponent, ColumnHeadersProps } = props;
const { VirtualScrollerComponent, ColumnHeadersProps } = props;
const apiRef = useGridPrivateApiContext();
const rootProps = useGridRootProps();
const rootRef = React.useRef<HTMLDivElement>(null);

const visibleColumns = useGridSelector(apiRef, gridVisibleColumnDefinitionsSelector);
const filterColumnLookup = useGridSelector(apiRef, gridFilterActiveItemsLookupSelector);
Expand Down Expand Up @@ -80,6 +79,29 @@ function GridBody(props: GridBodyProps) {
rootProps.disableVirtualization,
);

useEnhancedEffect(() => {
apiRef.current.computeSizeAndPublishResizeEvent();

const elementToObserve = rootRef.current;
if (typeof ResizeObserver === 'undefined') {
return () => {};
}

const observer = new ResizeObserver(() => {
apiRef.current.computeSizeAndPublishResizeEvent();
});

if (elementToObserve) {
observer.observe(elementToObserve);
}

return () => {
if (elementToObserve) {
observer.unobserve(elementToObserve);
}
};
}, [apiRef]);

const disableVirtualization = React.useCallback(() => {
setIsVirtualizationDisabled(true);
}, []);
Expand Down Expand Up @@ -111,15 +133,10 @@ function GridBody(props: GridBodyProps) {
virtualScrollerRef,
});

const handleResize = React.useCallback(
(size: ElementSize) => {
apiRef.current.publishEvent('resize', size);
},
[apiRef],
);
const hasDimensions = !!apiRef.current.getRootDimensions();

return (
<GridMainContainer>
<GridMainContainer ref={rootRef}>
<rootProps.slots.columnHeaders
ref={columnsContainerRef}
innerRef={columnHeadersRef}
Expand All @@ -139,17 +156,17 @@ function GridBody(props: GridBodyProps) {
hasOtherElementInTabSequence={hasOtherElementInTabSequence}
{...ColumnHeadersProps}
/>
<GridAutoSizer
nonce={rootProps.nonce}
disableHeight={rootProps.autoHeight}
onResize={handleResize}
>
{hasDimensions && (
<VirtualScrollerComponent
// The content is only rendered after dimensions are computed because
// the lazy-loading hook is listening to `renderedRowsIntervalChange`,
// but only does something if the dimensions are also available.
// If this event is published while dimensions haven't been computed,
// the `onFetchRows` prop won't be called during mount.
ref={virtualScrollerRef}
disableVirtualization={isVirtualizationDisabled}
/>
</GridAutoSizer>
{children}
)}
</GridMainContainer>
);
}
Expand All @@ -159,7 +176,6 @@ GridBody.propTypes = {
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
children: PropTypes.node,
ColumnHeadersProps: PropTypes.object,
VirtualScrollerComponent: PropTypes.elementType.isRequired,
} as any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ const GridMainContainerRoot = styled('div', {
overflow: 'hidden',
}));

export function GridMainContainer(props: React.PropsWithChildren<{}>) {
const rootProps = useGridRootProps();
const classes = useUtilityClasses(rootProps);
return (
<GridMainContainerRoot className={classes.root} ownerState={rootProps}>
{props.children}
</GridMainContainerRoot>
);
}
export const GridMainContainer = React.forwardRef<HTMLDivElement, React.PropsWithChildren<{}>>(
(props, ref) => {
const rootProps = useGridRootProps();
const classes = useUtilityClasses(rootProps);
return (
<GridMainContainerRoot ref={ref} className={classes.root} ownerState={rootProps}>
{props.children}
</GridMainContainerRoot>
);
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ const GridRoot = React.forwardRef<HTMLDivElement, GridRootProps>(function GridRo
setMountedState(true);
}, []);

useEnhancedEffect(() => {
if (mountedState) {
apiRef.current.updateGridDimensionsRef();
}
}, [apiRef, mountedState]);

if (!mountedState) {
return null;
}
Expand Down
1 change: 0 additions & 1 deletion packages/grid/x-data-grid/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export * from './panel';
export * from './toolbar';

export * from './GridApiContext';
export * from './GridAutoSizer';
export * from './GridFooter';
export * from './GridHeader';
export * from './GridLoadingOverlay';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ export interface GridDimensionsPrivateApi {
* Forces a recalculation of all dimensions.
*/
updateGridDimensionsRef: () => void;
/**
* Computes the size and publishes a `resize` event with the new value.
*/
computeSizeAndPublishResizeEvent: () => void;
}
Loading