Skip to content

Commit

Permalink
fix: ensure gridStyle fontSize and cellPadding trigger recalculation …
Browse files Browse the repository at this point in the history
…of cell line height
  • Loading branch information
mgadewoll committed Jun 4, 2024
1 parent 742961a commit de2cfd5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ export class EuiDataGridCell extends Component<
this.recalculateLineHeight();
}

if (this.props?.shouldUpdateLineHeight === true) {
this.recalculateLineHeight();
}

if (
(this.props.rowHeightUtils as RowHeightVirtualizationUtils)
?.compensateForLayoutShift &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const CellWrapper: FunctionComponent<CellProps> = memo(
rowHeightsOptions,
rowHeightUtils,
rowManager,
shouldUpdateLineHeight,
...rest
}) => {
const popoverContext = useContext(DataGridCellPopoverContext);
Expand Down Expand Up @@ -155,6 +156,7 @@ export const CellWrapper: FunctionComponent<CellProps> = memo(
width={leadingColumn.width}
renderCellValue={rowCellRender}
isExpandable={false}
shouldUpdateLineHeight={shouldUpdateLineHeight}
{...rest}
/>
);
Expand All @@ -171,6 +173,7 @@ export const CellWrapper: FunctionComponent<CellProps> = memo(
width={trailingColumn.width}
renderCellValue={rowCellRender}
isExpandable={false}
shouldUpdateLineHeight={shouldUpdateLineHeight}
{...rest}
/>
);
Expand All @@ -194,6 +197,7 @@ export const CellWrapper: FunctionComponent<CellProps> = memo(
renderCellPopover={renderCellPopover}
interactiveCellId={interactiveCellId}
isExpandable={isExpandable}
shouldUpdateLineHeight={shouldUpdateLineHeight}
{...rest}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ import {
} from '../utils/grid_height_width';
import { useDefaultColumnWidth, useColumnWidths } from '../utils/col_widths';
import { useRowHeightUtils, useDefaultRowHeight } from '../utils/row_heights';
import { useShouldUpdateCellLineHeight } from '../utils/cell_heights';
import { useScrollBars, useScroll } from '../utils/scrolling';
import { IS_JEST_ENVIRONMENT } from '../../../utils';

export const Cell: FunctionComponent<GridChildComponentProps> = memo(
({ columnIndex, rowIndex, style, data }) => {
const { gridStyles, ...cellData } = data;
const memoizedStyles = useDeepEqual(style);
const cellStyles = useMemo(() => {
const { headerRowHeight } = data;
Expand All @@ -56,12 +58,15 @@ export const Cell: FunctionComponent<GridChildComponentProps> = memo(
};
}, [memoizedStyles, data]);

const shouldUpdateLineHeight = useShouldUpdateCellLineHeight(gridStyles);

return (
<CellWrapper
colIndex={columnIndex}
visibleRowIndex={rowIndex}
style={cellStyles}
{...data}
shouldUpdateLineHeight={shouldUpdateLineHeight}
{...cellData}
/>
);
}
Expand Down Expand Up @@ -334,6 +339,7 @@ export const EuiDataGridBodyVirtualized: FunctionComponent<EuiDataGridBodyProps>
rowManager,
pagination,
headerRowHeight,
gridStyles,
};
}, [
schemaDetectors,
Expand All @@ -354,6 +360,7 @@ export const EuiDataGridBodyVirtualized: FunctionComponent<EuiDataGridBodyProps>
rowManager,
pagination,
headerRowHeight,
gridStyles,
]);

const rowWrapperContextValue = useMemo(() => {
Expand Down
1 change: 1 addition & 0 deletions packages/eui/src/components/datagrid/data_grid_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ export interface EuiDataGridCellProps {
rowHeightUtils?: RowHeightUtilsType;
rowManager?: EuiDataGridRowManager;
pagination?: Required<EuiDataGridPaginationProps>;
shouldUpdateLineHeight?: boolean;
}

export interface EuiDataGridCellState {
Expand Down
38 changes: 38 additions & 0 deletions packages/eui/src/components/datagrid/utils/cell_heights.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { useEffect, useRef, useState } from 'react';

import { EuiDataGridBodyProps } from '../data_grid_types';

export const useShouldUpdateCellLineHeight = (
gridStyles: EuiDataGridBodyProps['gridStyles']
) => {
const _gridStyles = useRef(gridStyles);
const [shouldUpdateLineHeight, setShouldUpdateLineHeight] = useState(false);

useEffect(() => {
if (
gridStyles &&
(_gridStyles.current?.fontSize || _gridStyles.current?.cellPadding)
) {
const hasNewFontSize = _gridStyles.current?.fontSize
? _gridStyles.current?.fontSize !== gridStyles?.fontSize
: false;
const hasNewCellPadding = _gridStyles.current?.cellPadding
? _gridStyles.current?.cellPadding !== gridStyles?.cellPadding
: false;

setShouldUpdateLineHeight(hasNewFontSize || hasNewCellPadding);

_gridStyles.current = gridStyles;
}
}, [gridStyles]);

return shouldUpdateLineHeight;
};

0 comments on commit de2cfd5

Please sign in to comment.