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

[EuiDataGrid] Refactor row height calculations to no longer need computed styles on grid init #5284

Merged
merged 17 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 12 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
- Added `EuiAutoSizer` component for setting dimensions on virtualized lists ([#5278](https://github.com/elastic/eui/pull/5278))
- Added `testenv` mock for `EuiAutoSizer` ([#5278](https://github.com/elastic/eui/pull/5278))

**Bug fixes**

- Fixed an `EuiDataGrid` race condition where grid rows had incorrect heights if loaded in before CSS ([#5284](https://github.com/elastic/eui/pull/5284))

## [`40.1.0`](https://github.com/elastic/eui/tree/v40.1.0)

- Added styling support for `valign` prop on `EuiTableRowCell` ([#5283](https://github.com/elastic/eui/pull/5283))
Expand Down
33 changes: 9 additions & 24 deletions src/components/datagrid/__mocks__/row_height_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,26 @@
* Side Public License, v 1.
*/

import { EuiDataGridRowHeightOption } from '../data_grid_types';
import { RowHeightUtils as ActualRowHeightUtils } from '../row_height_utils';

import { RowHeightUtils } from '../row_height_utils';
const actual = new ActualRowHeightUtils();

export const mockRowHeightUtils = ({
computeStylesForGridCell: jest.fn(),
cacheStyles: jest.fn(),
setGrid: jest.fn(),
getStylesForCell: jest.fn(() => ({
wordWrap: 'break-word',
wordBreak: 'break-word',
flexGrow: 1,
})),
isDefinedHeight: jest.fn(() => true),
isAutoHeight: jest.fn(() => false),
setRowHeight: jest.fn(),
pruneHiddenColumnHeights: jest.fn(),
getRowHeight: jest.fn(() => 32),
getComputedCellStyles: jest.fn(() => {}),
getCalculatedHeight: jest.fn(
(heightOption: EuiDataGridRowHeightOption, defaultHeight: number) => {
if (typeof heightOption === 'object') {
if (heightOption.lineCount) {
return heightOption.lineCount;
}
getRowHeightOption: jest.fn(actual.getRowHeightOption),
getCalculatedHeight: jest.fn(actual.getCalculatedHeight),
getLineCount: jest.fn(actual.getLineCount),
calculateHeightForLineCount: jest.fn(() => 50),
} as unknown) as ActualRowHeightUtils;

if (heightOption.height) {
return heightOption.height;
}
}

if (heightOption) {
return heightOption;
}

return defaultHeight;
}
),
} as unknown) as RowHeightUtils;
export const RowHeightUtils = jest.fn(() => mockRowHeightUtils);
24 changes: 0 additions & 24 deletions src/components/datagrid/_data_grid_data_row.scss
Original file line number Diff line number Diff line change
Expand Up @@ -235,49 +235,25 @@
}
}

.euiDataGridRowCell--fontSizeSmall {
@include euiFontSizeXS;
}

@include euiDataGridStyles(fontSizeLarge) {
@include euiDataGridRowCell {
@include euiFontSize;
}
}

.euiDataGridRowCell--fontSizeLarge {
@include euiFontSize;
}

// Padding alternates
@include euiDataGridStyles(paddingSmall) {
@include euiDataGridRowCell {
padding: $euiDataGridCellPaddingS;
}
}

.euiDataGridRowCell--paddingSmall {
padding: $euiDataGridCellPaddingS;

.euiDataGridRowCell__contentByHeight + .euiDataGridRowCell__expandButton {
padding: 0;
}
}

@include euiDataGridStyles(paddingLarge) {
@include euiDataGridRowCell {
padding: $euiDataGridCellPaddingL;
}
}

.euiDataGridRowCell--paddingLarge {
padding: $euiDataGridCellPaddingL;

.euiDataGridRowCell__contentByHeight + .euiDataGridRowCell__expandButton {
padding: $euiDataGridCellPaddingL 0;
}
}

@keyframes euiDataGridCellButtonSlideIn {
from {
margin-left: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@ exports[`EuiDataGridCell renders 1`] = `
renderCellValue={[Function]}
rowHeightUtils={
Object {
"computeStylesForGridCell": [MockFunction],
"cacheStyles": [MockFunction],
"calculateHeightForLineCount": [MockFunction],
"getCalculatedHeight": [MockFunction],
"getComputedCellStyles": [MockFunction],
"getLineCount": [MockFunction],
"getRowHeight": [MockFunction],
"getRowHeightOption": [MockFunction] {
"calls": Array [
Array [
0,
undefined,
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
],
},
"getStylesForCell": [MockFunction],
"isAutoHeight": [MockFunction],
"isDefinedHeight": [MockFunction],
"pruneHiddenColumnHeights": [MockFunction],
"setGrid": [MockFunction],
"setRowHeight": [MockFunction],
Expand Down Expand Up @@ -57,13 +71,27 @@ exports[`EuiDataGridCell renders 1`] = `
renderCellValue={[Function]}
rowHeightUtils={
Object {
"computeStylesForGridCell": [MockFunction],
"cacheStyles": [MockFunction],
"calculateHeightForLineCount": [MockFunction],
"getCalculatedHeight": [MockFunction],
"getComputedCellStyles": [MockFunction],
"getLineCount": [MockFunction],
"getRowHeight": [MockFunction],
"getRowHeightOption": [MockFunction] {
"calls": Array [
Array [
0,
undefined,
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
],
},
"getStylesForCell": [MockFunction],
"isAutoHeight": [MockFunction],
"isDefinedHeight": [MockFunction],
"pruneHiddenColumnHeights": [MockFunction],
"setGrid": [MockFunction],
"setRowHeight": [MockFunction],
Expand Down
96 changes: 31 additions & 65 deletions src/components/datagrid/body/data_grid_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
useMutationObserver,
} from '../../observer/mutation_observer';
import { useResizeObserver } from '../../observer/resize_observer';
import { AUTO_HEIGHT } from '../row_height_utils';
import { DEFAULT_ROW_HEIGHT } from '../row_height_utils';
import { EuiDataGridCell } from './data_grid_cell';
import {
DataGridSortingContext,
Expand Down Expand Up @@ -70,7 +70,6 @@ export const Cell: FunctionComponent<GridChildComponentProps> = ({
setRowHeight,
schemaDetectors,
rowHeightsOptions,
getRowHeight,
rowHeightUtils,
} = data;

Expand Down Expand Up @@ -119,30 +118,33 @@ export const Cell: FunctionComponent<GridChildComponentProps> = ({
[`euiDataGridRowCell--${textTransform}`]: textTransform,
});

const sharedCellProps = {
rowIndex,
visibleRowIndex,
colIndex: columnIndex,
interactiveCellId,
className: classes,
style: {
...style,
top: `${parseFloat(style.top as string) + headerRowHeight}px`,
},
rowHeightsOptions,
rowHeightUtils,
setRowHeight: isFirstColumn ? setRowHeight : undefined,
};

if (isLeadingControlColumn) {
const leadingColumn = leadingControlColumns[columnIndex];
const { id, rowCellRender } = leadingColumn;

cellContent = (
<EuiDataGridCell
rowIndex={rowIndex}
visibleRowIndex={visibleRowIndex}
colIndex={columnIndex}
{...sharedCellProps}
columnId={id}
popoverContent={DefaultColumnFormatter}
width={leadingColumn.width}
renderCellValue={rowCellRender}
interactiveCellId={interactiveCellId}
isExpandable={false}
className={classes}
setRowHeight={setRowHeight}
getRowHeight={getRowHeight}
rowHeightsOptions={rowHeightsOptions}
rowHeightUtils={rowHeightUtils}
style={{
...style,
top: `${parseFloat(style.top as string) + headerRowHeight}px`,
}}
/>
);
} else if (isTrailingControlColumn) {
Expand All @@ -153,23 +155,12 @@ export const Cell: FunctionComponent<GridChildComponentProps> = ({

cellContent = (
<EuiDataGridCell
rowIndex={rowIndex}
visibleRowIndex={visibleRowIndex}
colIndex={columnIndex}
{...sharedCellProps}
columnId={id}
popoverContent={DefaultColumnFormatter}
width={trailingColumn.width}
renderCellValue={rowCellRender}
interactiveCellId={interactiveCellId}
isExpandable={false}
className={classes}
rowHeightsOptions={rowHeightsOptions}
getRowHeight={getRowHeight}
rowHeightUtils={rowHeightUtils}
style={{
...style,
top: `${parseFloat(style.top as string) + headerRowHeight}px`,
}}
/>
);
} else {
Expand All @@ -188,9 +179,7 @@ export const Cell: FunctionComponent<GridChildComponentProps> = ({

cellContent = (
<EuiDataGridCell
rowIndex={rowIndex}
visibleRowIndex={visibleRowIndex}
colIndex={columnIndex}
{...sharedCellProps}
columnId={columnId}
column={column}
columnType={columnType}
Expand All @@ -199,14 +188,6 @@ export const Cell: FunctionComponent<GridChildComponentProps> = ({
renderCellValue={renderCellValue}
interactiveCellId={interactiveCellId}
isExpandable={isExpandable}
className={classes}
rowHeightsOptions={rowHeightsOptions}
getRowHeight={getRowHeight}
rowHeightUtils={rowHeightUtils}
style={{
...style,
top: `${parseFloat(style.top as string) + headerRowHeight}px`,
}}
/>
);
}
Expand Down Expand Up @@ -240,7 +221,6 @@ const InnerElement: VariableSizeGridProps['innerElementType'] = forwardRef<
});
InnerElement.displayName = 'EuiDataGridInnerElement';

const INITIAL_ROW_HEIGHT = 34;
const IS_JEST_ENVIRONMENT = global.hasOwnProperty('_isJest');

/**
Expand Down Expand Up @@ -524,45 +504,32 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = (
[rowHeightUtils]
);

const [minRowHeight, setRowHeight] = useState(INITIAL_ROW_HEIGHT);

const computedCellStyles = rowHeightUtils.getComputedCellStyles();
const [minRowHeight, setRowHeight] = useState(DEFAULT_ROW_HEIGHT);

const defaultHeight = useMemo(() => {
// @ts-ignore we need to re-run this when computedCellStyles changes,
// but it isn't used directly; so let's make the hooks lint rule see
// that it is used, but we need to tell eslint to ignore
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _computedCellStyles = computedCellStyles;
return rowHeightsOptions?.defaultHeight
? rowHeightUtils.getCalculatedHeight(
rowHeightsOptions.defaultHeight,
minRowHeight
)
: minRowHeight;
}, [rowHeightsOptions, minRowHeight, rowHeightUtils, computedCellStyles]);
}, [rowHeightsOptions, minRowHeight, rowHeightUtils]);

const getRowHeight = useCallback(
(rowIndex) => {
const correctRowIndex = getCorrectRowIndex(rowIndex);
let height;

if (rowHeightsOptions) {
if (rowHeightsOptions.rowHeights) {
const initialHeight = rowHeightsOptions.rowHeights[correctRowIndex];

if (initialHeight) {
height = rowHeightUtils.getCalculatedHeight(
initialHeight,
minRowHeight,
correctRowIndex
);
}
}

if (!height && rowHeightsOptions.defaultHeight === AUTO_HEIGHT) {
height = rowHeightUtils.getRowHeight(correctRowIndex);
}
const rowHeightOption = rowHeightUtils.getRowHeightOption(
correctRowIndex,
rowHeightsOptions
);
if (rowHeightOption) {
height = rowHeightUtils.getCalculatedHeight(
rowHeightOption,
minRowHeight,
correctRowIndex
);
}

return height || defaultHeight;
Expand Down Expand Up @@ -707,7 +674,6 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = (
itemData={{
schemaDetectors,
setRowHeight,
getRowHeight,
getCorrectRowIndex,
rowMap,
rowOffset: pagination
Expand Down
Loading