Skip to content

Commit f2ffdd3

Browse files
authored
TableNG: Reduce how often we re-measure row height (#107604)
1 parent 41014f2 commit f2ffdd3

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

packages/grafana-ui/src/components/Table/TableNG/TableNG.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function TableNG(props: TableNGProps) {
6464
const {
6565
cellHeight,
6666
data,
67-
enablePagination,
67+
enablePagination = false,
6868
enableSharedCrosshair = false,
6969
enableVirtualization,
7070
footerOptions,
@@ -265,6 +265,9 @@ export function TableNG(props: TableNGProps) {
265265
cellRootRenderers: {},
266266
};
267267

268+
let lastRowIdx = -1;
269+
let _rowHeight = 0;
270+
268271
f.forEach((field, i) => {
269272
const justifyContent = getTextAlign(field);
270273
const footerStyles = getFooterStyles(justifyContent);
@@ -292,9 +295,6 @@ export function TableNG(props: TableNGProps) {
292295
const shouldOverflow = shouldTextOverflow(field);
293296
const shouldWrap = shouldTextWrap(field);
294297

295-
let lastRowIdx = -1;
296-
let _rowHeight = 0;
297-
298298
// this fires first
299299
const renderCellRoot = (key: Key, props: CellRendererProps<TableRow, TableSummaryRow>): ReactNode => {
300300
const rowIdx = props.row.__index;

packages/grafana-ui/src/components/Table/TableNG/hooks.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export interface PaginatedRowsOptions {
140140
headerHeight: number;
141141
footerHeight: number;
142142
paginationHeight?: number;
143-
enabled?: boolean;
143+
enabled: boolean;
144144
}
145145

146146
export interface PaginatedRowsResult {
@@ -167,11 +167,17 @@ export function usePaginatedRows(
167167

168168
// calculate average row height if row height is variable.
169169
const avgRowHeight = useMemo(() => {
170+
if (!enabled) {
171+
return 0;
172+
}
173+
170174
if (typeof rowHeight === 'number') {
171175
return rowHeight;
172176
}
173-
return rows.reduce((avg, row, _, { length }) => avg + rowHeight(row) / length, 0);
174-
}, [rows, rowHeight]);
177+
178+
// we'll just measure 100 rows to estimate
179+
return rows.slice(0, 100).reduce((avg, row, _, { length }) => avg + rowHeight(row) / length, 0);
180+
}, [rows, rowHeight, enabled]);
175181

176182
// using dimensions of the panel, calculate pagination parameters
177183
const { numPages, rowsPerPage, pageRangeStart, pageRangeEnd, smallPagination } = useMemo((): {
@@ -325,6 +331,7 @@ export function useTypographyCtx(): TypographyCtx {
325331
const txtWidth = ctx.measureText(txt).width;
326332
const avgCharWidth = txtWidth / txt.length + letterSpacing;
327333
const { count } = varPreLine(ctx);
334+
328335
const calcRowHeight = (text: string, cellWidth: number, defaultHeight: number) => {
329336
if (text === '') {
330337
return defaultHeight;

0 commit comments

Comments
 (0)