@@ -15,7 +15,7 @@ export function useColumnWidths<R, SR>(
1515 setMeasuredColumnWidths : StateSetter < ReadonlyMap < string , number > > ,
1616 onColumnResize : DataGridProps < R , SR > [ 'onColumnResize' ]
1717) {
18- const [ columnToAutoresize , setColumnToAutoResize ] = useState < string | null > ( null ) ;
18+ const [ columnToAutoResize , setColumnToAutoResize ] = useState < string | null > ( null ) ;
1919 const prevGridWidthRef = useRef ( gridWidth ) ;
2020 const columnsCanFlex : boolean = columns . length === viewportColumns . length ;
2121 // Allow columns to flex again when...
@@ -26,7 +26,7 @@ export function useColumnWidths<R, SR>(
2626 const columnsToMeasure : string [ ] = [ ] ;
2727
2828 for ( const { key, idx, width } of viewportColumns ) {
29- if ( key === columnToAutoresize ) {
29+ if ( key === columnToAutoResize ) {
3030 newTemplateColumns [ idx ] = 'max-content' ;
3131 columnsToMeasure . push ( key ) ;
3232 } else if (
@@ -47,34 +47,37 @@ export function useColumnWidths<R, SR>(
4747 } ) ;
4848
4949 function updateMeasuredWidths ( columnsToMeasure : readonly string [ ] ) {
50- if ( columnsToMeasure . length === 0 ) return ;
51-
52- setMeasuredColumnWidths ( ( measuredColumnWidths ) => {
53- const newMeasuredColumnWidths = new Map ( measuredColumnWidths ) ;
54- let hasChanges = false ;
55-
56- for ( const key of columnsToMeasure ) {
57- const measuredWidth = measureColumnWidth ( gridRef , key ) ;
58- hasChanges ||= measuredWidth !== measuredColumnWidths . get ( key ) ;
59- if ( measuredWidth === undefined ) {
60- newMeasuredColumnWidths . delete ( key ) ;
61- } else {
62- newMeasuredColumnWidths . set ( key , measuredWidth ) ;
50+ if ( columnsToMeasure . length > 0 ) {
51+ setMeasuredColumnWidths ( ( measuredColumnWidths ) => {
52+ const newMeasuredColumnWidths = new Map ( measuredColumnWidths ) ;
53+ let hasChanges = false ;
54+
55+ for ( const key of columnsToMeasure ) {
56+ const measuredWidth = measureColumnWidth ( gridRef , key ) ;
57+ hasChanges ||= measuredWidth !== measuredColumnWidths . get ( key ) ;
58+ if ( measuredWidth === undefined ) {
59+ newMeasuredColumnWidths . delete ( key ) ;
60+ } else {
61+ newMeasuredColumnWidths . set ( key , measuredWidth ) ;
62+ }
6363 }
64- }
6564
66- return hasChanges ? newMeasuredColumnWidths : measuredColumnWidths ;
67- } ) ;
65+ return hasChanges ? newMeasuredColumnWidths : measuredColumnWidths ;
66+ } ) ;
67+ }
6868
69- if ( columnToAutoresize !== null ) {
69+ if ( columnToAutoResize !== null ) {
7070 setColumnToAutoResize ( null ) ;
7171 setResizedColumnWidths ( ( resizedColumnWidths ) => {
72- const newResizedColumnWidths = new Map ( resizedColumnWidths ) ;
73- newResizedColumnWidths . set (
74- columnToAutoresize ,
75- measureColumnWidth ( gridRef , columnToAutoresize ) !
76- ) ;
77- return newResizedColumnWidths ;
72+ const oldWidth = resizedColumnWidths . get ( columnToAutoResize ) ;
73+ const newWidth = measureColumnWidth ( gridRef , columnToAutoResize ) ;
74+ if ( newWidth !== undefined && oldWidth !== newWidth ) {
75+ const newResizedColumnWidths = new Map ( resizedColumnWidths ) ;
76+ newResizedColumnWidths . set ( columnToAutoResize , newWidth ) ;
77+ onColumnResize ?.( viewportColumns . find ( ( c ) => c . key === columnToAutoResize ) ! , newWidth ) ;
78+ return newResizedColumnWidths ;
79+ }
80+ return resizedColumnWidths ;
7881 } ) ;
7982 }
8083 }
@@ -94,13 +97,15 @@ export function useColumnWidths<R, SR>(
9497 }
9598 }
9699
97- setMeasuredColumnWidths ( ( measuredColumnWidths ) => {
98- const newMeasuredColumnWidths = new Map ( measuredColumnWidths ) ;
99- for ( const columnKey of columnsToMeasure ) {
100- newMeasuredColumnWidths . delete ( columnKey ) ;
101- }
102- return newMeasuredColumnWidths ;
103- } ) ;
100+ if ( columnsToMeasure . length > 0 ) {
101+ setMeasuredColumnWidths ( ( measuredColumnWidths ) => {
102+ const newMeasuredColumnWidths = new Map ( measuredColumnWidths ) ;
103+ for ( const columnKey of columnsToMeasure ) {
104+ newMeasuredColumnWidths . delete ( columnKey ) ;
105+ }
106+ return newMeasuredColumnWidths ;
107+ } ) ;
108+ }
104109
105110 if ( typeof nextWidth === 'number' ) {
106111 setResizedColumnWidths ( ( resizedColumnWidths ) => {
0 commit comments