Skip to content

Commit 50f084d

Browse files
authored
[DataGrid] Fix error overlay not visible when autoHeight is enabled (mui#4110)
1 parent 3d85cc0 commit 50f084d

File tree

7 files changed

+97
-40
lines changed

7 files changed

+97
-40
lines changed

packages/grid/x-data-grid/src/components/ErrorOverlay.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as React from 'react';
22
import { useGridApiContext } from '../hooks/utils/useGridApiContext';
33
import { GridOverlay, GridOverlayProps } from './containers/GridOverlay';
4+
import { useGridSelector } from '../hooks/utils/useGridSelector';
5+
import { gridDensityRowHeightSelector } from '../hooks/features/density/densitySelector';
46

57
export interface ErrorOverlayProps extends GridOverlayProps {
68
message?: string;
@@ -14,9 +16,10 @@ export const ErrorOverlay = React.forwardRef<HTMLDivElement, ErrorOverlayProps>(
1416
const { message, hasError, errorInfo, ...other } = props;
1517
const apiRef = useGridApiContext();
1618
const defaultLabel = apiRef.current.getLocaleText('errorOverlayDefaultLabel');
19+
const rowHeight = useGridSelector(apiRef, gridDensityRowHeightSelector);
1720

1821
return (
19-
<GridOverlay ref={ref} {...other}>
22+
<GridOverlay ref={ref} sx={{ width: '100%', minHeight: 2 * rowHeight }} {...other}>
2023
{message || defaultLabel}
2124
</GridOverlay>
2225
);
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as React from 'react';
2+
import PropTypes from 'prop-types';
23
import CircularProgress from '@mui/material/CircularProgress';
34
import { GridOverlay, GridOverlayProps } from './containers/GridOverlay';
45

5-
export const GridLoadingOverlay = React.forwardRef<HTMLDivElement, GridOverlayProps>(
6+
const GridLoadingOverlay = React.forwardRef<HTMLDivElement, GridOverlayProps>(
67
function GridLoadingOverlay(props, ref) {
78
return (
89
<GridOverlay ref={ref} {...props}>
@@ -11,3 +12,17 @@ export const GridLoadingOverlay = React.forwardRef<HTMLDivElement, GridOverlayPr
1112
);
1213
},
1314
);
15+
16+
GridLoadingOverlay.propTypes = {
17+
// ----------------------------- Warning --------------------------------
18+
// | These PropTypes are generated from the TypeScript type definitions |
19+
// | To update them edit the TypeScript types and run "yarn proptypes" |
20+
// ----------------------------------------------------------------------
21+
sx: PropTypes.oneOfType([
22+
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
23+
PropTypes.func,
24+
PropTypes.object,
25+
]),
26+
} as any;
27+
28+
export { GridLoadingOverlay };

packages/grid/x-data-grid/src/components/GridNoRowsOverlay.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as React from 'react';
2+
import PropTypes from 'prop-types';
23
import { useGridApiContext } from '../hooks/utils/useGridApiContext';
34
import { GridOverlay, GridOverlayProps } from './containers/GridOverlay';
45

5-
export const GridNoRowsOverlay = React.forwardRef<HTMLDivElement, GridOverlayProps>(
6+
const GridNoRowsOverlay = React.forwardRef<HTMLDivElement, GridOverlayProps>(
67
function GridNoRowsOverlay(props, ref) {
78
const apiRef = useGridApiContext();
89
const noRowsLabel = apiRef.current.getLocaleText('noRowsLabel');
@@ -14,3 +15,17 @@ export const GridNoRowsOverlay = React.forwardRef<HTMLDivElement, GridOverlayPro
1415
);
1516
},
1617
);
18+
19+
GridNoRowsOverlay.propTypes = {
20+
// ----------------------------- Warning --------------------------------
21+
// | These PropTypes are generated from the TypeScript type definitions |
22+
// | To update them edit the TypeScript types and run "yarn proptypes" |
23+
// ----------------------------------------------------------------------
24+
sx: PropTypes.oneOfType([
25+
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
26+
PropTypes.func,
27+
PropTypes.object,
28+
]),
29+
} as any;
30+
31+
export { GridNoRowsOverlay };

packages/grid/x-data-grid/src/components/base/GridErrorHandler.tsx

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
import * as React from 'react';
22
import PropTypes from 'prop-types';
3-
import { styled } from '@mui/material/styles';
43
import { useGridApiContext } from '../../hooks/utils/useGridApiContext';
54
import { useGridLogger } from '../../hooks/utils/useGridLogger';
65
import { GridMainContainer } from '../containers/GridMainContainer';
76
import { ErrorBoundary } from '../ErrorBoundary';
87
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
9-
import { GridAutoSizer, AutoSizerSize } from '../GridAutoSizer';
10-
import { GridEvents } from '../../models/events/gridEvents';
11-
12-
const ErrorOverlayWrapper = styled('div')({
13-
position: 'absolute',
14-
top: 0,
15-
width: '100%',
16-
height: '100%',
17-
});
188

199
function GridErrorHandler(props: { children: React.ReactNode }) {
2010
const { children } = props;
@@ -23,13 +13,6 @@ function GridErrorHandler(props: { children: React.ReactNode }) {
2313
const rootProps = useGridRootProps();
2414
const error = apiRef.current.state.error;
2515

26-
const handleResize = React.useCallback(
27-
(size: AutoSizerSize) => {
28-
apiRef.current.publishEvent(GridEvents.resize, size);
29-
},
30-
[apiRef],
31-
);
32-
3316
return (
3417
<ErrorBoundary
3518
hasError={error != null}
@@ -38,20 +21,10 @@ function GridErrorHandler(props: { children: React.ReactNode }) {
3821
logger={logger}
3922
render={(errorProps) => (
4023
<GridMainContainer>
41-
<GridAutoSizer
42-
nonce={rootProps.nonce}
43-
disableHeight={rootProps.autoHeight}
44-
onResize={handleResize}
45-
>
46-
{() => (
47-
<ErrorOverlayWrapper>
48-
<rootProps.components.ErrorOverlay
49-
{...errorProps}
50-
{...rootProps.componentsProps?.errorOverlay}
51-
/>
52-
</ErrorOverlayWrapper>
53-
)}
54-
</GridAutoSizer>
24+
<rootProps.components.ErrorOverlay
25+
{...errorProps}
26+
{...rootProps.componentsProps?.errorOverlay}
27+
/>
5528
</GridMainContainer>
5629
)}
5730
>

packages/grid/x-data-grid/src/components/containers/GridOverlay.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import * as React from 'react';
2+
import PropTypes from 'prop-types';
23
import clsx from 'clsx';
34
import { unstable_composeClasses as composeClasses } from '@mui/material';
4-
import { alpha, styled } from '@mui/material/styles';
5+
import { SxProps } from '@mui/system';
6+
import { Theme, alpha, styled } from '@mui/material/styles';
57
import { getDataGridUtilityClass } from '../../constants/gridClasses';
68
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
79
import { DataGridProcessedProps } from '../../models/props/DataGridProps';
810

9-
export type GridOverlayProps = React.HTMLAttributes<HTMLDivElement>;
11+
export type GridOverlayProps = React.HTMLAttributes<HTMLDivElement> & {
12+
sx?: SxProps<Theme>;
13+
};
1014

1115
type OwnerState = { classes: DataGridProcessedProps['classes'] };
1216

@@ -33,7 +37,7 @@ const GridOverlayRoot = styled('div', {
3337
backgroundColor: alpha(theme.palette.background.default, theme.palette.action.disabledOpacity),
3438
}));
3539

36-
export const GridOverlay = React.forwardRef<HTMLDivElement, GridOverlayProps>(function GridOverlay(
40+
const GridOverlay = React.forwardRef<HTMLDivElement, GridOverlayProps>(function GridOverlay(
3741
props: GridOverlayProps,
3842
ref,
3943
) {
@@ -44,3 +48,17 @@ export const GridOverlay = React.forwardRef<HTMLDivElement, GridOverlayProps>(fu
4448

4549
return <GridOverlayRoot ref={ref} className={clsx(classes.root, className)} {...other} />;
4650
});
51+
52+
GridOverlay.propTypes = {
53+
// ----------------------------- Warning --------------------------------
54+
// | These PropTypes are generated from the TypeScript type definitions |
55+
// | To update them edit the TypeScript types and run "yarn proptypes" |
56+
// ----------------------------------------------------------------------
57+
sx: PropTypes.oneOfType([
58+
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
59+
PropTypes.func,
60+
PropTypes.object,
61+
]),
62+
} as any;
63+
64+
export { GridOverlay };

packages/grid/x-data-grid/src/tests/layout.DataGrid.test.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,9 +809,29 @@ describe('<DataGrid /> - Layout & Warnings', () => {
809809
<DataGrid {...baselineProps} rows={[]} rowHeight={rowHeight} autoHeight />
810810
</div>,
811811
);
812-
expect(document.querySelectorAll('.MuiDataGrid-overlay')[0].clientHeight).to.equal(
813-
rowHeight * 2,
812+
expect(
813+
(document.querySelector('.MuiDataGrid-overlay') as HTMLElement).clientHeight,
814+
).to.equal(rowHeight * 2);
815+
});
816+
817+
it('should expand content height to one row height when there is an error', () => {
818+
const error = { message: 'ERROR' };
819+
const rowHeight = 50;
820+
821+
render(
822+
<div style={{ width: 150 }}>
823+
<DataGrid
824+
columns={[{ field: 'brand' }]}
825+
rows={[]}
826+
autoHeight
827+
error={error}
828+
rowHeight={rowHeight}
829+
/>
830+
</div>,
814831
);
832+
const errorOverlayElement = document.querySelector('.MuiDataGrid-overlay') as HTMLElement;
833+
expect(errorOverlayElement.textContent).to.equal(error.message);
834+
expect(errorOverlayElement.offsetHeight).to.equal(2 * rowHeight);
815835
});
816836
});
817837

@@ -984,7 +1004,9 @@ describe('<DataGrid /> - Layout & Warnings', () => {
9841004
<DataGrid {...baselineProps} error={{ message }} />
9851005
</div>,
9861006
);
987-
expect(document.querySelectorAll('.MuiDataGrid-overlay')[0].textContent).to.equal(message);
1007+
expect((document.querySelector('.MuiDataGrid-overlay') as HTMLElement).textContent).to.equal(
1008+
message,
1009+
);
9881010
});
9891011
});
9901012

packages/storybook/src/stories/grid-error.stories.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ export const ThrowException = () => {
112112
);
113113
};
114114

115+
export const ErrorWithAutoHeight = () => {
116+
const rows = React.useMemo(() => getRows(), []);
117+
const cols = React.useMemo(() => getColumns(), []);
118+
119+
return (
120+
<div style={{ width: '100%' }}>
121+
<DataGridPro rows={rows} columns={cols} autoHeight error={{ message: 'ERROR' }} />
122+
</div>
123+
);
124+
};
125+
115126
export const ShowErrorApi = () => {
116127
const api = useGridApiRef();
117128
const rows = React.useMemo(() => getRows(), []);

0 commit comments

Comments
 (0)