Skip to content

Commit b412040

Browse files
committed
fix smart mode
1 parent 4b1fc11 commit b412040

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

packages/main/src/components/AnalyticalTable/AnalyticalTable.module.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@
328328
position: fixed;
329329
white-space: nowrap;
330330
height: 0;
331+
font-family: var(--sapFontFamily);
332+
font-size: var(--sapFontSize);
333+
line-height: normal;
331334
}
332335

333336
.hiddenSmartColMeasureHeader {

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ interface IColumnMeta {
77
contentPxAvg: number;
88
headerPx: number;
99
headerDefinesWidth?: boolean;
10+
//todo: not optional?
11+
maxWidth?: number;
1012
}
1113

1214
const ROW_SAMPLE_SIZE = 20;
@@ -215,7 +217,7 @@ const calculateSmartColumns = (columns: AnalyticalTableColumnDefinition[], insta
215217
return metadata;
216218
}
217219

218-
let headerPx, contentPxAvg;
220+
let headerPx: number, contentPxAvg: number;
219221

220222
if (column.scaleWidthModeOptions?.cellString) {
221223
contentPxAvg =
@@ -240,6 +242,7 @@ const calculateSmartColumns = (columns: AnalyticalTableColumnDefinition[], insta
240242
metadata[columnIdOrAccessor] = {
241243
headerPx,
242244
contentPxAvg,
245+
maxWidth: column.maxWidth,
243246
};
244247
return metadata;
245248
},
@@ -295,27 +298,57 @@ const calculateSmartColumns = (columns: AnalyticalTableColumnDefinition[], insta
295298
}
296299
return column;
297300
});
301+
302+
const columnWithMaxWidthIndex: number[] = [];
303+
let maxWidthDifference = 0;
304+
let fullWidthOfAllColumns = 0;
298305
// Step 2: Give all columns more space (priority 2)
299-
return visibleColumnsAdaptedPrio1.map((column) => {
306+
const visibleColumnsAdaptedPrio2 = visibleColumnsAdaptedPrio1.map((column, index) => {
300307
const columnIdOrAccessor = (column.id ?? column.accessor) as string;
301308
const meta = columnMeta[columnIdOrAccessor];
302309
const { headerPx } = meta;
310+
311+
if (column.maxWidth) {
312+
columnWithMaxWidthIndex.push(index);
313+
}
303314
if (meta && !column.minWidth && !column.width) {
304315
let targetWidth = column.nextWidth || headerPx;
305316
if (availableWidthPrio2 > 0) {
306317
targetWidth = targetWidth + availableWidthPrio2 * (1 / totalNumberColPrio2);
307318
}
319+
fullWidthOfAllColumns += targetWidth;
320+
if (targetWidth >= column.maxWidth) {
321+
maxWidthDifference += targetWidth - column.maxWidth;
322+
}
308323
return {
309324
...column,
310325
width: targetWidth,
311326
};
312327
} else {
328+
const targetWidth = Math.max(column.width || 0, 60, headerPx, column.minWidth);
329+
fullWidthOfAllColumns += Math.max(targetWidth, column.minWidth ?? 0);
313330
return {
314331
...column,
315332
width: Math.max(column.width || 0, 60, headerPx),
316333
};
317334
}
318335
});
336+
337+
// Step 3: Only if any column has maxWidth defined and there is still space to distribute, distribute the exceeding width to the other columns (priority 3)
338+
if (columnWithMaxWidthIndex.length && totalWidth >= fullWidthOfAllColumns) {
339+
return visibleColumnsAdaptedPrio2.map((column, index, arr) => {
340+
if (!columnWithMaxWidthIndex.includes(index)) {
341+
return {
342+
...column,
343+
width:
344+
Math.max(column.width, column.minWidth ?? 0) +
345+
maxWidthDifference / (arr.length - columnWithMaxWidthIndex.length),
346+
};
347+
}
348+
return column;
349+
});
350+
}
351+
return visibleColumnsAdaptedPrio2;
319352
};
320353

321354
const useColumnsDeps = (

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import {
5151
UNSELECT_PRESS_SPACE,
5252
} from '../../i18n/i18n-defaults.js';
5353
import { BusyIndicator } from '../../webComponents/BusyIndicator/index.js';
54-
import { Text } from '../../webComponents/Text/index.js';
5554
import { FlexBox } from '../FlexBox/index.js';
5655
import { classNames, styleData } from './AnalyticalTable.module.css.js';
5756
import { ColumnHeaderContainer } from './ColumnHeader/ColumnHeaderContainer.js';
@@ -921,22 +920,22 @@ const AnalyticalTable = forwardRef<AnalyticalTableDomRef, AnalyticalTablePropTyp
921920
/>
922921
)}
923922
</div>
924-
<Text
923+
<span
925924
aria-hidden="true"
926925
id={`scaleModeHelper-${uniqueId}`}
927926
className={classNames.hiddenSmartColMeasure}
928927
data-component-name="AnalyticalTableScaleModeHelper"
929928
>
930929
{''}
931-
</Text>
932-
<Text
930+
</span>
931+
<span
933932
aria-hidden="true"
934933
id={`scaleModeHelperHeader-${uniqueId}`}
935934
className={clsx(classNames.hiddenSmartColMeasure, classNames.hiddenSmartColMeasureHeader)}
936935
data-component-name="AnalyticalTableScaleModeHelperHeader"
937936
>
938937
{''}
939-
</Text>
938+
</span>
940939
</>
941940
);
942941
});

0 commit comments

Comments
 (0)