Skip to content

Commit 402787c

Browse files
committed
simplify
1 parent 0531129 commit 402787c

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

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

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -337,22 +337,17 @@ const calculateSmartAndGrowColumns = (
337337
return column;
338338
});
339339

340-
const columnWithMaxWidthIndex: number[] = [];
341-
let maxWidthDifference = 0;
342340
let fullWidthOfAllColumns = 0;
343341
let lessThanMaxWidthCount = 0;
344342

345343
/**
346344
* Step 2: Give all columns more space (priority 2)
347345
*/
348-
const visibleColumnsAdaptedPrio2 = visibleColumnsAdaptedPrio1.map((column, index) => {
346+
const visibleColumnsAdaptedPrio2 = visibleColumnsAdaptedPrio1.map((column) => {
349347
const columnIdOrAccessor = (column.id ?? column.accessor) as string;
350348
const meta = columnMeta[columnIdOrAccessor];
351349
const { headerPx } = meta;
352350

353-
if (column.maxWidth && column.maxWidth !== Infinity) {
354-
columnWithMaxWidthIndex.push(index);
355-
}
356351
if (meta && !column.minWidth && !column.width) {
357352
let targetWidth = column.nextWidth || headerPx;
358353
if (availableWidthPrio2 > 0) {
@@ -365,48 +360,29 @@ const calculateSmartAndGrowColumns = (
365360
}
366361
}
367362
fullWidthOfAllColumns += targetWidth;
368-
if (targetWidth >= column.maxWidth) {
369-
maxWidthDifference += targetWidth - column.maxWidth;
370-
}
371363
return {
372364
...column,
373365
width: targetWidth,
374366
};
375367
} else {
376-
const targetWidth = Math.max(column.width || 0, 60, headerPx, column.minWidth);
377-
fullWidthOfAllColumns += Math.max(targetWidth, column.minWidth ?? 0);
368+
const targetWidth = Math.max(column.width || 0, 60, headerPx, column.minWidth ?? 0);
369+
if (targetWidth < (column.maxWidth ?? Infinity)) {
370+
lessThanMaxWidthCount++;
371+
}
372+
fullWidthOfAllColumns += targetWidth;
378373
return {
379374
...column,
380-
width: Math.max(column.width || 0, 60, headerPx),
375+
width: targetWidth,
381376
};
382377
}
383378
});
384379

385-
// In Grow mode, all columns implement a maxWidth
386-
if (isGrow) {
387-
const remainingWidth = totalWidth - fullWidthOfAllColumns;
388-
if (remainingWidth > 0) {
389-
//Step 3 (Grow): Distribute remaining width to all columns that are not at their maxWidth
390-
return visibleColumnsAdaptedPrio2.map((column) => {
391-
if (column.width !== column.maxWidth) {
392-
return { ...column, width: column.width + remainingWidth / lessThanMaxWidthCount };
393-
}
394-
return column;
395-
});
396-
}
397-
return visibleColumnsAdaptedPrio2;
398-
}
399-
400-
// Step 3 (Smart): Only if any column has maxWidth defined and there is still space to distribute, distribute the exceeding width to the other columns (priority 3)
401-
if (columnWithMaxWidthIndex.length && totalWidth >= fullWidthOfAllColumns) {
402-
return visibleColumnsAdaptedPrio2.map((column, index, arr) => {
403-
if (!columnWithMaxWidthIndex.includes(index)) {
404-
return {
405-
...column,
406-
width:
407-
Math.max(column.width, column.minWidth ?? 0) +
408-
maxWidthDifference / (arr.length - columnWithMaxWidthIndex.length),
409-
};
380+
// Step 3: Distribute remaining width to all columns that are not at their maxWidth
381+
const remainingWidth = totalWidth - fullWidthOfAllColumns;
382+
if (remainingWidth > 0) {
383+
return visibleColumnsAdaptedPrio2.map((column) => {
384+
if (column.width !== column.maxWidth) {
385+
return { ...column, width: column.width + remainingWidth / lessThanMaxWidthCount };
410386
}
411387
return column;
412388
});

0 commit comments

Comments
 (0)