[Data Grid] Add new includeHeaderFilters flag to include header filters when autosizing columns
#20510
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
closes #13802
preview: https://deploy-preview-20510--material-ui-x.netlify.app/x/react-data-grid/column-dimensions/#autosizing-header-filters
🎯 Main Purpose
Adds a new
includeHeaderFiltersoption toautosizeOptionsthat makes the column autosize algorithm consider header filter widths, preventing filter inputs from being squeezed when columns have short content.🔍 Problem & Solution Context
Issue: When using header filters with columns that have short headers and short content, the autosize feature would make columns too narrow, causing the filter input to become unusable.
Example scenario: A column with header "ID" and values like "1", "2", "3" would autosize to a very narrow width, leaving no room for the filter input.
Solution: New
includeHeaderFilters: trueoption tells autosize to also measure the header filter cell width and include it in the column width calculation.🔧 Key Changes
includeHeaderFiltersboolean option inautosizeOptions(defaults tofalse)findGridHeaderFilter()utility to locate header filter elements📁 Important Files
useGridColumnResize.tsx- Core width measurement logic; adds header filter width to the pool of widths considered during autosizingGridRootStyles.ts- Adds CSS overrides for header filter cells during autosize measurement (uses!importantto override flex layout)📋 Usage Example
Or programmatically:
🚀 Impact
false💡 Implementation Notes
Why
flex: noneandwidth: unsetin CSS?Header filter cells normally use
flex: 1and a fixedwidthwhich constrains them to their column width. During autosizing, we need to measure the natural content width (how wide the filter input wants to be), not the constrained width. Settingflex: noneandwidth: unsetallows the element to expand to its intrinsic size soscrollWidthreturns the true content width.Why
window.getComputedStylefor padding instead ofgetBoundingClientRectorclientWidth?getBoundingClientRect().widthreturns the rendered width (constrained by flex/column width), not the content's natural widthclientWidthincludes padding but still reflects the constrained rendered widthscrollWidthgives the full content width including overflow, but excludes paddinggetComputedStyleis used specifically to get the padding values to add toscrollWidth, giving us:scrollWidth + paddingLeft + paddingRight = true natural widthThis matches the existing pattern used for header cells in the same file.
Note
The
!importantin GridRootStyles is scoped to the.MuiDataGrid--autosizingstate and only affects header filter cells during measurement.