Skip to content

Commit 15dfb3a

Browse files
authored
fix(AnalyticalTable): always scale cols correctly with scaleWidthMode "Grow" (#4362)
Fixes #4348
1 parent 29ec474 commit 15dfb3a

File tree

6 files changed

+23
-26
lines changed

6 files changed

+23
-26
lines changed

packages/charts/src/components/TimelineChart/TimeLineChart.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { TimelineChartAnnotation } from './TimelineChartAnnotation';
66

77
<Meta of={ComponentStories} />
88

9-
<DocsHeader />
9+
<DocsHeader since="1.10.0" />
1010

1111
<Canvas of={ComponentStories.Default} />
1212

packages/main/src/components/AnalyticalTable/AnalyticalTable.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const meta = {
9595
Header: 'Actions',
9696
accessor: '.',
9797
width: 100,
98+
minWidth: 100,
9899
disableResizing: true,
99100
disableGroupBy: true,
100101
disableFilters: true,

packages/main/src/components/AnalyticalTable/hooks/useDynamicColumnWidths.ts

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ const columns = (columns: AnalyticalTableColumnDefinition[], { instance }) => {
292292
return columns.map((column) => ({ ...column, width: column.width ?? defaultWidth }));
293293
}
294294

295+
// AnalyticalTableScaleWidthMode.Grow
296+
295297
const rowSample = rows.slice(0, ROW_SAMPLE_SIZE);
296298

297299
const columnMeta = visibleColumns.reduce((acc, column) => {
@@ -345,40 +347,31 @@ const columns = (columns: AnalyticalTableColumnDefinition[], { instance }) => {
345347
return acc;
346348
}, {});
347349

348-
const totalCharNum = Object.values(columnMeta).reduce((acc: number, item: any) => acc + item.contentCharAvg, 0);
349-
350350
let reservedWidth = visibleColumns.reduce((acc, column) => {
351351
const { minHeaderWidth, fullWidth } = columnMeta[column.id ?? column.accessor];
352-
return (
353-
acc +
354-
Math.max(
355-
column.minWidth || 0,
356-
column.width || 0,
357-
minHeaderWidth || 0,
358-
scaleWidthMode === AnalyticalTableScaleWidthMode.Grow ? fullWidth : 0
359-
) || 0
360-
);
352+
return acc + Math.max(column.minWidth || 0, column.width || 0, minHeaderWidth || 0, fullWidth) || 0;
361353
}, 0);
362354

363355
let availableWidth = totalWidth - reservedWidth;
364356

365357
if (availableWidth > 0) {
366-
if (scaleWidthMode === AnalyticalTableScaleWidthMode.Grow) {
367-
reservedWidth = visibleColumns.reduce((acc, column) => {
368-
const { minHeaderWidth } = columnMeta[column.id ?? column.accessor];
369-
return acc + Math.max(column.minWidth || 0, column.width || 0, minHeaderWidth || 0) || 0;
370-
}, 0);
371-
availableWidth = totalWidth - reservedWidth;
372-
}
358+
let notReservedCount = 0;
359+
reservedWidth = visibleColumns.reduce((acc, column) => {
360+
const reserved = Math.max(column.minWidth || 0, column.width || 0) || 0;
361+
if (!reserved) {
362+
notReservedCount++;
363+
}
364+
return acc + reserved;
365+
}, 0);
366+
availableWidth = totalWidth - reservedWidth;
373367

374368
return columns.map((column) => {
375369
const isColumnVisible = (column.isVisible ?? true) && !hiddenColumns.includes(column.id ?? column.accessor);
376370
const meta = columnMeta[column.id ?? (column.accessor as string)];
377371
if (isColumnVisible && meta) {
378-
const { minHeaderWidth, contentCharAvg } = meta;
379-
const additionalSpaceFactor = totalCharNum > 0 ? contentCharAvg / totalCharNum : 1 / visibleColumns.length;
372+
const { minHeaderWidth } = meta;
380373

381-
const targetWidth = additionalSpaceFactor * availableWidth + minHeaderWidth;
374+
const targetWidth = availableWidth / notReservedCount;
382375

383376
return {
384377
...column,
@@ -391,7 +384,6 @@ const columns = (columns: AnalyticalTableColumnDefinition[], { instance }) => {
391384
});
392385
}
393386

394-
// AnalyticalTableScaleWidthMode Grow
395387
return columns.map((column) => {
396388
const isColumnVisible = (column.isVisible ?? true) && !hiddenColumns.includes(column.id ?? column.accessor);
397389
const meta = columnMeta[column.id ?? (column.accessor as string)];

packages/main/src/components/AnalyticalTable/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ export interface AnalyticalTablePropTypes extends Omit<CommonProps, 'title'> {
418418
*
419419
* - **Default**: The available space of the table is distributed evenly for columns without fixed width. If the minimum width of all columns is reached, horizontal scrolling will be enabled.
420420
* - **Smart**: Every column gets the space it needs for displaying the full header text. If all header texts need more space than the available table width, horizontal scrolling will be enabled. If there is space left, columns with a long text will get more space until there is no more table space left.
421-
* - **Grow**: Every column gets the space it needs for displaying its full header text and full text content of all cells. If it requires more space than the table has, horizontal scrolling will be enabled. To prevent huge header text from polluting the table, a max-width of 700px is applied to each column. It can be overwritten by setting the respective column property.
421+
* - **Grow**: Every column gets the space it needs for displaying its full header text and full text content of all cells. If it requires more space than the table has, horizontal scrolling will be enabled. To prevent huge header text from polluting the table, a max-width of 700px is applied to each column. It can be overwritten by setting the respective column property. This mode adds a calculated `minWidth` to each column. If the internally calculated `minWidth` is larger than the `width` set in the column options, it can lead to an unwanted scrollbar. To prevent this, you can set the `minWidth` in the column options yourself.
422422
*
423423
* __Note:__ Custom cells with components instead of text as children are ignored by the `Smart` and `Grow` modes.
424424
* __Note:__ For performance reasons, the `Smart` and `Grow` modes base their calculation for table cell width on a subset of column cells. If the first 20 cells of a column are significantly smaller than the rest of the column cells, the content may still not be fully displayed for all cells.

packages/main/src/enums/AnalyticalTableScaleWidthMode.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export enum AnalyticalTableScaleWidthMode {
1414
*/
1515
Smart = 'Smart',
1616
/*
17-
* Every column gets the space it needs for displaying its full header text and full text content of all cells. If it requires more space than the table has, horizontal scrolling will be enabled. To prevent huge header text from polluting the table, a max-width of 700px is applied to each column. It can be overwritten by setting the respective column property.
17+
* Every column gets the space it needs for displaying its full header text and full text content of all cells. If it requires more space than the table has, horizontal scrolling will be enabled.
18+
*
19+
* __Note:__ To prevent huge header text from polluting the table, a max-width of 700px is applied to each column. It can be overwritten by setting the respective column property.
20+
*
21+
* __Note:__ This mode adds a calculated `minWidth` to each column. If the internally calculated `minWidth` is larger than the `width` set in the column options, it can lead to an unwanted scrollbar. To prevent this, you can set the `minWidth` in the column options yourself.
1822
*/
1923
Grow = 'Grow'
2024
}

packages/main/src/webComponents/Menu/Menu.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import MenuDomRef from './MenuDomRef.json';
1010

1111
<Meta of={ComponentStories} />
1212

13-
<DocsHeader />
13+
<DocsHeader since="0.23.0" />
1414

1515
## Example
1616

0 commit comments

Comments
 (0)