Skip to content

Commit

Permalink
[EuiDataGrid] Fix grid height when exiting full screen mode (#5580)
Browse files Browse the repository at this point in the history
* Track full screen grid height separately
- so grid height restores correctly after leaving full-screen mode

* Fix nested ternary eslint warning
  • Loading branch information
Constance authored Feb 1, 2022
1 parent 02fff02 commit 5804943
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/components/datagrid/body/data_grid_body.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('EuiDataGridBody', () => {
schemaDetectors,
popoverContents: providedPopoverContents,
rowHeightUtils: mockRowHeightUtils,
isFullScreen: false,
gridStyles: {},
gridWidth: 300,
gridRef: {
Expand Down
2 changes: 2 additions & 0 deletions src/components/datagrid/body/data_grid_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = (
onColumnResize,
rowHeightsOptions,
virtualizationOptions,
isFullScreen,
gridStyles,
gridWidth,
gridRef,
Expand Down Expand Up @@ -424,6 +425,7 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = (
unconstrainedWidth: 0, // unable to determine this until the container's size is known
wrapperDimensions,
wrapperRef,
isFullScreen,
rowCount,
});

Expand Down
1 change: 1 addition & 0 deletions src/components/datagrid/data_grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ export const EuiDataGrid: FunctionComponent<EuiDataGridProps> = (props) => {
interactiveCellId={interactiveCellId}
rowHeightsOptions={rowHeightsOptions}
virtualizationOptions={virtualizationOptions || {}}
isFullScreen={isFullScreen}
gridStyles={gridStyles}
gridWidth={gridWidth}
gridRef={gridRef}
Expand Down
1 change: 1 addition & 0 deletions src/components/datagrid/data_grid_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export interface EuiDataGridBodyProps {
onColumnResize?: EuiDataGridOnColumnResizeHandler;
virtualizationOptions?: Partial<VariableSizeGridProps>;
rowHeightsOptions?: EuiDataGridRowHeightsOptions;
isFullScreen: boolean;
gridStyles: EuiDataGridStyle;
gridWidth: number;
gridRef: MutableRefObject<Grid | null>;
Expand Down
28 changes: 20 additions & 8 deletions src/components/datagrid/utils/grid_height_width.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,58 @@ export const useFinalGridDimensions = ({
unconstrainedWidth,
wrapperDimensions,
wrapperRef,
isFullScreen,
rowCount,
}: {
unconstrainedHeight: number;
unconstrainedWidth: number;
wrapperDimensions: { width: number; height: number };
wrapperRef: MutableRefObject<HTMLDivElement | null>;
isFullScreen: boolean;
rowCount: number;
}) => {
// Used if the grid needs to scroll
const [height, setHeight] = useState<number | undefined>(undefined);
const [width, setWidth] = useState<number | undefined>(undefined);
// Tracking full screen height separately is necessary to correctly restore the grid back to non-full-screen height
const [fullScreenHeight, setFullScreenHeight] = useState(0);

// Set the wrapper height on load, whenever the grid wrapper resizes, and whenever rowCount changes
useEffect(() => {
const boundingRect = wrapperRef.current!.getBoundingClientRect();

if (boundingRect.height !== unconstrainedHeight) {
setHeight(boundingRect.height);
if (isFullScreen) {
setFullScreenHeight(boundingRect.height);
} else {
if (boundingRect.height !== unconstrainedHeight) {
setHeight(boundingRect.height);
}
}
if (boundingRect.width !== unconstrainedWidth) {
setWidth(boundingRect.width);
}
}, [
// Effects that should cause recalculations
rowCount,
isFullScreen,
wrapperDimensions,
// Dependencies
wrapperRef,
unconstrainedHeight,
unconstrainedWidth,
]);

const finalHeight = IS_JEST_ENVIRONMENT
? Number.MAX_SAFE_INTEGER
const finalHeight = isFullScreen
? fullScreenHeight
: height || unconstrainedHeight;
const finalWidth = IS_JEST_ENVIRONMENT
? Number.MAX_SAFE_INTEGER
: width || unconstrainedWidth;
const finalWidth = width || unconstrainedWidth;

return { finalHeight, finalWidth };
return IS_JEST_ENVIRONMENT
? {
finalHeight: Number.MAX_SAFE_INTEGER,
finalWidth: Number.MAX_SAFE_INTEGER,
}
: { finalHeight, finalWidth };
};

/**
Expand Down

0 comments on commit 5804943

Please sign in to comment.