[DataGrid] Wait for rows before reacting on autosizeOnMount#22698
Conversation
Delay autosizeOnMount until the initial render context is ready and committed, so fitting rows are measured before column widths are extracted. Add a browser regression test covering a wide value in the last rendered row.
Deploy previewBundle size
Check out the code infra dashboard for more information about this PR. |
There was a problem hiding this comment.
Pull request overview
This PR adjusts the DataGrid autosizeOnMount behavior so it runs only after the initial render context is ready/committed, ensuring row cells are actually measurable before column widths are extracted (fixing the “last row wide value not considered” bug from #22505).
Changes:
- Add a render-context readiness gate for
autosizeOnMount, driven byrenderedRowsIntervalChange/stateChange, and schedule autosize on the next animation frame once ready. - Add a browser regression test that reproduces the issue by delaying the final row’s rendered cell content, then asserts the wide value is fully visible after autosizing.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/x-data-grid/src/hooks/features/columnResize/useGridColumnResize.tsx | Delays autosizeOnMount until dimensions + row render context indicate rows are committed/ready for measurement. |
| packages/x-data-grid-pro/src/tests/columns.DataGridPro.test.tsx | Adds a regression test for autosize measuring the last rendered row’s wide value. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
siriwatknp
left a comment
There was a problem hiding this comment.
Thanks, this looks solid. A few nitpicks, none blocking:
-
isRenderContextReadyForAutosizeOnMountno-scroll vs scroll branch asymmetry. Maybe add a one-line comment. -
Autosize now fires only when the readiness guard passes. Worth confirming a layout cannot get stuck.
| } | ||
| }; | ||
|
|
||
| unsubscribeStateChange = apiRef.current.subscribeEvent('stateChange', checkRenderContext); |
There was a problem hiding this comment.
The old code fired autosize once unconditionally. Now it fires only when the readiness guard passes. In a layout where the render context never commits, autosize never fires and the listeners stay subscribed until unmount.
Risk looks low (empty rows hit the early return, mounted grids emit renderedRowsIntervalChange), but there is no timeout fallback. Could a real layout get stuck here? If yes, maybe add a last-resort fallback to keep the old guaranteed-once behavior.
There was a problem hiding this comment.
I checked this again. I think we should avoid adding a timeout fallback for now because it would preserve the old “always run once” behavior, but it could also reintroduce the bug by measuring before the initial rows have committed.
The guard runs immediately on mount, then again on stateChange and renderedRowsIntervalChange. Empty rows return ready, and mounted grids should publish a render context once dimensions/rows are committed. So I don’t see a real layout that should get stuck here unless the grid never commits a render context at all, in which case measuring would likely be unreliable anyway.
In b193b65, I added a short comment to explain the scroll/no-scroll asymmetry and a expanded more in docs for the row virtualization limitation: when rows overflow the viewport, autosizing can still only measure the currently rendered row window.
|
Cherry-pick PRs will be created targeting branches: v8.x |
Delay
autosizeOnMountuntil the initial render context is ready and committed, so fitting rows are measured before column widths are extracted.Add a browser regression test covering a wide value in the last rendered row.
Fixes #22505
Closes #20551