@@ -7,6 +7,8 @@ interface IColumnMeta {
77 contentPxAvg : number ;
88 headerPx : number ;
99 headerDefinesWidth ?: boolean ;
10+ //todo: not optional?
11+ maxWidth ?: number ;
1012}
1113
1214const 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
321354const useColumnsDeps = (
0 commit comments