Skip to content

Commit

Permalink
subscribe to 'resize' asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
m4theushw committed Jan 6, 2022
1 parent 7c4cd44 commit 8828ace
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/grid/_modules_/grid/components/ErrorOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface ErrorOverlayProps extends GridOverlayProps {
errorInfo: any;
}

// TODO v6: rename to GridErrorOverlay
export const ErrorOverlay = React.forwardRef<HTMLDivElement, ErrorOverlayProps>(
function ErrorOverlay(props: ErrorOverlayProps, ref) {
const { message, hasError, errorInfo, ...other } = props;
Expand Down
4 changes: 3 additions & 1 deletion packages/grid/_modules_/grid/components/base/GridBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ function GridBody(props: GridBodyProps) {
apiRef.current.renderingZoneRef = renderingZoneRef; // TODO remove, nobody should have access to internal parts of the virtualization

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

Expand Down
25 changes: 21 additions & 4 deletions packages/grid/_modules_/grid/components/base/GridErrorHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useGridLogger } from '../../hooks/utils/useGridLogger';
import { GridMainContainer } from '../containers/GridMainContainer';
import { ErrorBoundary } from '../ErrorBoundary';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
import { GridAutoSizer, AutoSizerSize } from '../GridAutoSizer';
import { GridEvents } from '../../models/events/gridEvents';

export function GridErrorHandler(props) {
const { children } = props;
Expand All @@ -12,6 +14,13 @@ export function GridErrorHandler(props) {
const rootProps = useGridRootProps();
const error = apiRef.current.state.error;

const handleResize = React.useCallback(
(size: AutoSizerSize) => {
apiRef.current.publishEvent(GridEvents.resize, size);
},
[apiRef],
);

return (
<ErrorBoundary
hasError={error != null}
Expand All @@ -20,10 +29,18 @@ export function GridErrorHandler(props) {
logger={logger}
render={(errorProps) => (
<GridMainContainer>
<rootProps.components.ErrorOverlay
{...errorProps}
{...rootProps.componentsProps?.errorOverlay}
/>
<GridAutoSizer
nonce={rootProps.nonce}
disableHeight={rootProps.autoHeight}
onResize={handleResize}
>
{() => (
<rootProps.components.ErrorOverlay
{...errorProps}
{...rootProps.componentsProps?.errorOverlay}
/>
)}
</GridAutoSizer>
</GridMainContainer>
)}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ export function GridOverlays() {
if (rootProps.loading) {
return <rootProps.components.LoadingOverlay {...rootProps.componentsProps?.loadingOverlay} />;
}

return null;
}
16 changes: 13 additions & 3 deletions packages/grid/_modules_/grid/components/containers/GridOverlay.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/material/utils';
import clsx from 'clsx';
import { unstable_composeClasses as composeClasses } from '@mui/material';
import { alpha, styled } from '@mui/material/styles';
Expand All @@ -8,7 +9,6 @@ import { useGridApiContext } from '../../hooks/utils/useGridApiContext';
import { getDataGridUtilityClass } from '../../gridClasses';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
import { DataGridProcessedProps } from '../../models/props/DataGridProps';
import { useGridApiEventHandler } from '../../hooks/utils/useGridApiEventHandler';
import { GridEvents } from '../../models/events';

export type GridOverlayProps = React.HTMLAttributes<HTMLDivElement>;
Expand Down Expand Up @@ -57,16 +57,26 @@ export const GridOverlay = React.forwardRef<HTMLDivElement, GridOverlayProps>(fu
() => apiRef.current.getRootDimensions()?.viewportInnerSize ?? null,
);

const handleViewportSizeChangeTest = () =>
const handleViewportSizeChange = React.useCallback(() => {
setViewportInnerSize(apiRef.current.getRootDimensions()?.viewportInnerSize ?? null);
}, [apiRef]);

useGridApiEventHandler(apiRef, GridEvents.viewportInnerSizeChange, handleViewportSizeChangeTest);
useEnhancedEffect(() => {
return apiRef.current.subscribeEvent(
GridEvents.viewportInnerSizeChange,
handleViewportSizeChange,
);
}, [apiRef, handleViewportSizeChange]);

let height: React.CSSProperties['height'] = viewportInnerSize?.height ?? 0;
if (rootProps.autoHeight && height === 0) {
height = 'auto';
}

if (!viewportInnerSize) {
return null;
}

return (
<GridOverlayRoot
ref={ref}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ describe('<DataGridPro /> - Column Headers', () => {
);

const columnCell = getColumnHeaderCell(0);

const menuIconButton = columnCell.querySelector('button[aria-label="Menu"]');

fireEvent.click(menuIconButton);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ describe('<DataGridPro /> - Column pinning', () => {
this.skip();
}
render(<TestCase nbCols={3} initialState={{ pinnedColumns: { right: ['price1M'] } }} />);
clock.runToLast();
const columnHeader = getColumnHeaderCell(2);
// @ts-expect-error need to migrate helpers to TypeScript
expect(columnHeader).toHaveInlineStyle({ width: '100px' });
Expand All @@ -167,7 +166,6 @@ describe('<DataGridPro /> - Column pinning', () => {
this.skip();
}
render(<TestCase nbCols={3} initialState={{ pinnedColumns: { right: ['price1M'] } }} />);
clock.runToLast();
const columnHeader = getColumnHeaderCell(2);
// @ts-expect-error need to migrate helpers to TypeScript
expect(columnHeader).toHaveInlineStyle({ width: '100px' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useData } from 'packages/storybook/src/hooks/useData';
const isJSDOM = /jsdom/.test(window.navigator.userAgent);

describe('<DataGrid /> - Pagination', () => {
const { render, clock } = createRenderer({ clock: 'fake' });
const { render } = createRenderer({ clock: 'fake' });

const BaselineTestCase = (
props: Omit<DataGridProps, 'rows' | 'columns'> & { height?: number },
Expand Down Expand Up @@ -248,7 +248,6 @@ describe('<DataGrid /> - Pagination', () => {

it('should apply the new pageSize when clicking on a page size option and onPageSizeChanged is not defined and pageSize is not controlled', () => {
render(<BaselineTestCase rowsPerPageOptions={[1, 2, 3, 100]} />);
clock.runToLast(); // Run the timer to cleanup the listeners registered by StrictMode
fireEvent.mouseDown(screen.queryByLabelText('Rows per page:'));
expect(screen.queryAllByRole('option').length).to.equal(4);

Expand All @@ -265,7 +264,6 @@ describe('<DataGrid /> - Pagination', () => {
rowsPerPageOptions={[1, 2, 3, 100]}
/>,
);
clock.runToLast(); // Run the timer to cleanup the listeners registered by StrictMode
fireEvent.mouseDown(screen.queryByLabelText('Rows per page:'));
expect(screen.queryAllByRole('option').length).to.equal(4);

Expand Down Expand Up @@ -477,8 +475,6 @@ describe('<DataGrid /> - Pagination', () => {
/>,
);

clock.runToLast(); // Run the timer to cleanup the listeners registered by StrictMode

const footerHeight = document.querySelector('.MuiDataGrid-footerContainer')!.clientHeight;
const expectedViewportRowsLengthBefore = Math.floor(
(heightBefore - headerHeight - footerHeight) / rowHeight,
Expand Down Expand Up @@ -638,7 +634,6 @@ describe('<DataGrid /> - Pagination', () => {

expect(getColumnValues(0)).to.deep.equal(['0', '1']);

clock.runToLast(); // Run the timer to cleanup the listeners registered by StrictMode
fireEvent.mouseDown(screen.queryByLabelText('Rows per page:'));
expect(screen.queryAllByRole('option').length).to.equal(2);
fireEvent.click(screen.queryAllByRole('option')[1]);
Expand Down

0 comments on commit 8828ace

Please sign in to comment.