Skip to content

Commit 6b870fd

Browse files
committed
[Proposed refactors]
- Rename `recalculateRowHeight` to `setAutoRowHeight` - DRY out setAutoRowHeight to be reusable by both the resize observer and during props update (NB: This leads to some repetition with the height being obtained twice, but will not be an issue shortly as I'll be refactoring/removing the cell wrapper observer in a future PR) - Refactor rowHeightUtils.isAutoHeight to accept an undefined rowHeightsOptions
1 parent a553005 commit 6b870fd

2 files changed

Lines changed: 22 additions & 43 deletions

File tree

src/components/datagrid/body/data_grid_cell.tsx

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -194,28 +194,24 @@ export class EuiDataGridCell extends Component<
194194
}
195195
};
196196

197-
recalculateRowHeight() {
198-
const cellRef = this.cellRef.current;
199-
const { getRowHeight, rowHeightUtils, rowHeightsOptions } = this.props;
200-
201-
if (cellRef && getRowHeight && rowHeightUtils && rowHeightsOptions) {
202-
const { rowIndex, colIndex, visibleRowIndex } = this.props;
197+
setAutoRowHeight = () => {
198+
const { rowHeightUtils, rowHeightsOptions, rowIndex } = this.props;
199+
if (
200+
this.cellContentsRef &&
201+
rowHeightUtils &&
202+
rowHeightUtils.isAutoHeight(rowIndex, rowHeightsOptions)
203+
) {
204+
const { colIndex, visibleRowIndex } = this.props;
205+
const rowHeight = this.cellContentsRef.offsetHeight;
203206

204-
const isAutoHeight = rowHeightUtils.isAutoHeight(
207+
rowHeightUtils.setRowHeight(
205208
rowIndex,
206-
rowHeightsOptions
209+
colIndex,
210+
rowHeight,
211+
visibleRowIndex
207212
);
208-
209-
if (isAutoHeight) {
210-
rowHeightUtils.setRowHeight(
211-
rowIndex,
212-
colIndex,
213-
this.cellContentsRef?.offsetHeight,
214-
visibleRowIndex
215-
);
216-
}
217213
}
218-
}
214+
};
219215

220216
componentDidMount() {
221217
this.unsubscribeCell = this.context.onFocusUpdate(
@@ -240,7 +236,7 @@ export class EuiDataGridCell extends Component<
240236
}
241237

242238
componentDidUpdate(prevProps: EuiDataGridCellProps) {
243-
this.recalculateRowHeight();
239+
this.setAutoRowHeight();
244240

245241
if (this.props.columnId !== prevProps.columnId) {
246242
this.setCellProps({});
@@ -291,27 +287,10 @@ export class EuiDataGridCell extends Component<
291287

292288
setCellContentsRef = (ref: HTMLDivElement | null) => {
293289
this.cellContentsRef = ref;
294-
const { rowHeightUtils, rowHeightsOptions, rowIndex } = this.props;
295-
if (
296-
hasResizeObserver &&
297-
rowHeightUtils &&
298-
rowHeightsOptions &&
299-
rowHeightUtils.isAutoHeight(rowIndex, rowHeightsOptions)
300-
) {
301-
if (ref) {
302-
const { colIndex, visibleRowIndex } = this.props;
303-
304-
const setRowHeight = (rowHeight: number) =>
305-
rowHeightUtils.setRowHeight(
306-
rowIndex,
307-
colIndex,
308-
rowHeight,
309-
visibleRowIndex
310-
);
311-
this.contentObserver = this.observeHeight(ref, setRowHeight);
312-
} else if (this.contentObserver) {
313-
this.contentObserver.disconnect();
314-
}
290+
if (ref && hasResizeObserver) {
291+
this.contentObserver = this.observeHeight(ref, this.setAutoRowHeight);
292+
} else if (this.contentObserver) {
293+
this.contentObserver.disconnect();
315294
}
316295
this.preventTabbing();
317296
};

src/components/datagrid/row_height_utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ export class RowHeightUtils {
109109

110110
isAutoHeight(
111111
rowIndex: number,
112-
rowHeightsOptions: EuiDataGridRowHeightsOptions
112+
rowHeightsOptions?: EuiDataGridRowHeightsOptions
113113
) {
114-
if (rowHeightsOptions.rowHeights?.[rowIndex] === AUTO_HEIGHT) {
114+
if (rowHeightsOptions?.rowHeights?.[rowIndex] === AUTO_HEIGHT) {
115115
return true;
116116
}
117117

118-
if (rowHeightsOptions.defaultHeight === AUTO_HEIGHT) {
118+
if (rowHeightsOptions?.defaultHeight === AUTO_HEIGHT) {
119119
return true;
120120
}
121121

0 commit comments

Comments
 (0)