11import { useLayoutEffect , useState } from 'react' ;
22
3- import { clampColumnWidth , getColumnWidthForMeasurement } from '../utils' ;
3+ import { getColumnWidthForMeasurement } from '../utils' ;
44import type { CalculatedColumn , StateSetter } from '../types' ;
55import type { DataGridProps } from '../DataGrid' ;
66
@@ -16,7 +16,10 @@ export function useColumnWidths<R, SR>(
1616 setMeasuredColumnWidths : StateSetter < ReadonlyMap < string , number > > ,
1717 onColumnResize : DataGridProps < R , SR > [ 'onColumnResize' ]
1818) {
19- const [ columnToAutoResize , setColumnToAutoResize ] = useState < string | null > ( null ) ;
19+ const [ columnToAutoResize , setColumnToAutoResize ] = useState < {
20+ readonly key : string ;
21+ readonly width : number | 'max-content' ;
22+ } | null > ( null ) ;
2023 const [ prevGridWidth , setPreviousGridWidth ] = useState ( gridWidth ) ;
2124 const columnsCanFlex : boolean = columns . length === viewportColumns . length ;
2225 // Allow columns to flex again when...
@@ -28,11 +31,10 @@ export function useColumnWidths<R, SR>(
2831
2932 for ( const column of viewportColumns ) {
3033 const { key, idx, width } = column ;
31- if ( key === columnToAutoResize ) {
32- newTemplateColumns [ idx ] = getColumnWidthForMeasurement ( 'max-content' , column ) ;
34+ if ( key === columnToAutoResize ?. key ) {
35+ newTemplateColumns [ idx ] = getColumnWidthForMeasurement ( columnToAutoResize . width , column ) ;
3336 columnsToMeasure . push ( key ) ;
3437 } else if (
35- typeof width === 'string' &&
3638 ( ignorePreviouslyMeasuredColumns || ! measuredColumnWidths . has ( key ) ) &&
3739 // If the column is resized by the user, we don't want to measure it again
3840 ! resizedColumnWidths . has ( key )
@@ -71,12 +73,13 @@ export function useColumnWidths<R, SR>(
7173 if ( columnToAutoResize !== null ) {
7274 setColumnToAutoResize ( null ) ;
7375 setResizedColumnWidths ( ( resizedColumnWidths ) => {
74- const oldWidth = resizedColumnWidths . get ( columnToAutoResize ) ;
75- const newWidth = measureColumnWidth ( gridRef , columnToAutoResize ) ;
76+ const resizingKey = columnToAutoResize . key ;
77+ const oldWidth = resizedColumnWidths . get ( resizingKey ) ;
78+ const newWidth = measureColumnWidth ( gridRef , resizingKey ) ;
7679 if ( newWidth !== undefined && oldWidth !== newWidth ) {
7780 const newResizedColumnWidths = new Map ( resizedColumnWidths ) ;
78- newResizedColumnWidths . set ( columnToAutoResize , newWidth ) ;
79- onColumnResize ?.( viewportColumns . find ( ( c ) => c . key === columnToAutoResize ) ! , newWidth ) ;
81+ newResizedColumnWidths . set ( resizingKey , newWidth ) ;
82+ onColumnResize ?.( viewportColumns . find ( ( c ) => c . key === resizingKey ) ! , newWidth ) ;
8083 return newResizedColumnWidths ;
8184 }
8285 return resizedColumnWidths ;
@@ -110,17 +113,10 @@ export function useColumnWidths<R, SR>(
110113 } ) ;
111114 }
112115
113- if ( typeof nextWidth === 'number' ) {
114- const clampedNextWidth = clampColumnWidth ( nextWidth , column ) ;
115- setResizedColumnWidths ( ( resizedColumnWidths ) => {
116- const newResizedColumnWidths = new Map ( resizedColumnWidths ) ;
117- newResizedColumnWidths . set ( resizingKey , clampedNextWidth ) ;
118- return newResizedColumnWidths ;
119- } ) ;
120- onColumnResize ?.( column , clampedNextWidth ) ;
121- } else {
122- setColumnToAutoResize ( resizingKey ) ;
123- }
116+ setColumnToAutoResize ( {
117+ key : resizingKey ,
118+ width : nextWidth
119+ } ) ;
124120 }
125121
126122 return {
0 commit comments