|
1 | 1 | import { useLayoutEffect, useState } from 'react'; |
| 2 | +import { flushSync } from 'react-dom'; |
2 | 3 |
|
3 | 4 | import type { CalculatedColumn, ResizedWidth, StateSetter } from '../types'; |
4 | 5 | import type { DataGridProps } from '../DataGrid'; |
@@ -49,63 +50,72 @@ export function useColumnWidths<R, SR>( |
49 | 50 |
|
50 | 51 | function updateMeasuredAndResizedWidths() { |
51 | 52 | setPreviousGridWidth(gridWidth); |
52 | | - |
53 | | - if (columnsToMeasure.length > 0) { |
54 | | - setMeasuredColumnWidths((measuredColumnWidths) => { |
55 | | - const newMeasuredColumnWidths = new Map(measuredColumnWidths); |
56 | | - let hasChanges = false; |
57 | | - |
58 | | - for (const key of columnsToMeasure) { |
59 | | - const measuredWidth = measureColumnWidth(gridRef, key); |
60 | | - hasChanges ||= measuredWidth !== measuredColumnWidths.get(key); |
61 | | - if (measuredWidth === undefined) { |
62 | | - newMeasuredColumnWidths.delete(key); |
63 | | - } else { |
64 | | - newMeasuredColumnWidths.set(key, measuredWidth); |
65 | | - } |
| 53 | + if (columnsToMeasure.length === 0) return; |
| 54 | + |
| 55 | + setMeasuredColumnWidths((measuredColumnWidths) => { |
| 56 | + const newMeasuredColumnWidths = new Map(measuredColumnWidths); |
| 57 | + let hasChanges = false; |
| 58 | + |
| 59 | + for (const key of columnsToMeasure) { |
| 60 | + const measuredWidth = measureColumnWidth(gridRef, key); |
| 61 | + hasChanges ||= measuredWidth !== measuredColumnWidths.get(key); |
| 62 | + if (measuredWidth === undefined) { |
| 63 | + newMeasuredColumnWidths.delete(key); |
| 64 | + } else { |
| 65 | + newMeasuredColumnWidths.set(key, measuredWidth); |
66 | 66 | } |
| 67 | + } |
67 | 68 |
|
68 | | - return hasChanges ? newMeasuredColumnWidths : measuredColumnWidths; |
69 | | - }); |
70 | | - } |
| 69 | + return hasChanges ? newMeasuredColumnWidths : measuredColumnWidths; |
| 70 | + }); |
71 | 71 |
|
72 | 72 | if (columnToAutoResize !== null) { |
73 | | - setColumnToAutoResize(null); |
| 73 | + const resizingKey = columnToAutoResize.key; |
74 | 74 | setResizedColumnWidths((resizedColumnWidths) => { |
75 | | - const resizingKey = columnToAutoResize.key; |
76 | 75 | const oldWidth = resizedColumnWidths.get(resizingKey); |
77 | 76 | const newWidth = measureColumnWidth(gridRef, resizingKey); |
78 | 77 | if (newWidth !== undefined && oldWidth !== newWidth) { |
79 | 78 | const newResizedColumnWidths = new Map(resizedColumnWidths); |
80 | 79 | newResizedColumnWidths.set(resizingKey, newWidth); |
81 | | - onColumnResize?.(viewportColumns.find((c) => c.key === resizingKey)!, newWidth); |
82 | 80 | return newResizedColumnWidths; |
83 | 81 | } |
84 | 82 | return resizedColumnWidths; |
85 | 83 | }); |
| 84 | + setColumnToAutoResize(null); |
86 | 85 | } |
87 | 86 | } |
88 | 87 |
|
89 | 88 | function handleColumnResize(column: CalculatedColumn<R, SR>, nextWidth: ResizedWidth) { |
90 | 89 | const { key: resizingKey } = column; |
91 | 90 |
|
92 | | - if (columnsCanFlex) { |
93 | | - // delete measured column widths for all other flex columns so they can be recalculated |
94 | | - setMeasuredColumnWidths((measuredColumnWidths) => { |
95 | | - const newMeasuredColumnWidths = new Map(measuredColumnWidths); |
96 | | - for (const { key, width } of viewportColumns) { |
97 | | - if (resizingKey !== key && typeof width === 'string' && !resizedColumnWidths.has(key)) { |
98 | | - newMeasuredColumnWidths.delete(key); |
| 91 | + flushSync(() => { |
| 92 | + if (columnsCanFlex) { |
| 93 | + // remeasure all the columns that can flex and are not resized by the user |
| 94 | + setMeasuredColumnWidths((measuredColumnWidths) => { |
| 95 | + const newMeasuredColumnWidths = new Map(measuredColumnWidths); |
| 96 | + for (const { key, width } of viewportColumns) { |
| 97 | + if (resizingKey !== key && typeof width === 'string' && !resizedColumnWidths.has(key)) { |
| 98 | + newMeasuredColumnWidths.delete(key); |
| 99 | + } |
99 | 100 | } |
100 | | - } |
101 | | - return newMeasuredColumnWidths; |
102 | | - }); |
103 | | - } |
| 101 | + return newMeasuredColumnWidths; |
| 102 | + }); |
| 103 | + } |
104 | 104 |
|
105 | | - setColumnToAutoResize({ |
106 | | - key: resizingKey, |
107 | | - width: nextWidth |
| 105 | + setColumnToAutoResize({ |
| 106 | + key: resizingKey, |
| 107 | + width: nextWidth |
| 108 | + }); |
108 | 109 | }); |
| 110 | + |
| 111 | + if (onColumnResize) { |
| 112 | + const previousWidth = resizedColumnWidths.get(resizingKey); |
| 113 | + const newWidth = |
| 114 | + typeof nextWidth === 'number' ? nextWidth : measureColumnWidth(gridRef, resizingKey); |
| 115 | + if (newWidth !== undefined && newWidth !== previousWidth) { |
| 116 | + onColumnResize(column, newWidth); |
| 117 | + } |
| 118 | + } |
109 | 119 | } |
110 | 120 |
|
111 | 121 | return { |
|
0 commit comments