11import { useLayoutEffect , useRef , useState } from 'react' ;
22
3+ import { clampColumnWidth , getColumnWidthForMeasurement } from '../utils' ;
34import type { CalculatedColumn , StateSetter } from '../types' ;
45import type { DataGridProps } from '../DataGrid' ;
56
@@ -25,16 +26,18 @@ export function useColumnWidths<R, SR>(
2526 const newTemplateColumns = [ ...templateColumns ] ;
2627 const columnsToMeasure : string [ ] = [ ] ;
2728
28- for ( const { key, idx, width } of viewportColumns ) {
29+ for ( const column of viewportColumns ) {
30+ const { key, idx, width } = column ;
2931 if ( key === columnToAutoResize ) {
30- newTemplateColumns [ idx ] = 'max-content' ;
32+ newTemplateColumns [ idx ] = getColumnWidthForMeasurement ( 'max-content' , column ) ;
3133 columnsToMeasure . push ( key ) ;
3234 } else if (
3335 typeof width === 'string' &&
3436 ( ignorePreviouslyMeasuredColumns || ! measuredColumnWidths . has ( key ) ) &&
37+ // If the column is resized by the user, we don't want to measure it again
3538 ! resizedColumnWidths . has ( key )
3639 ) {
37- newTemplateColumns [ idx ] = width ;
40+ newTemplateColumns [ idx ] = getColumnWidthForMeasurement ( width , column ) ;
3841 columnsToMeasure . push ( key ) ;
3942 }
4043 }
@@ -109,12 +112,13 @@ export function useColumnWidths<R, SR>(
109112 }
110113
111114 if ( typeof nextWidth === 'number' ) {
115+ const clampedNextWidth = clampColumnWidth ( nextWidth , column ) ;
112116 setResizedColumnWidths ( ( resizedColumnWidths ) => {
113117 const newResizedColumnWidths = new Map ( resizedColumnWidths ) ;
114- newResizedColumnWidths . set ( resizingKey , nextWidth ) ;
118+ newResizedColumnWidths . set ( resizingKey , clampedNextWidth ) ;
115119 return newResizedColumnWidths ;
116120 } ) ;
117- onColumnResize ?.( column , nextWidth ) ;
121+ onColumnResize ?.( column , clampedNextWidth ) ;
118122 } else {
119123 setColumnToAutoResize ( resizingKey ) ;
120124 }
0 commit comments