mui-x icon indicating copy to clipboard operation
mui-x copied to clipboard

[Data Grid] Add new `includeHeaderFilters` flag to include header filters when autosizing columns

Open siriwatknp opened this issue 1 month ago • 1 comments

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 includeHeaderFilters option to autosizeOptions that 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: true option tells autosize to also measure the header filter cell width and include it in the column width calculation.

🔧 Key Changes

  • Feature: New includeHeaderFilters boolean option in autosizeOptions (defaults to false)
  • API: Added findGridHeaderFilter() utility to locate header filter elements
  • Styles: CSS adjustments to allow proper width measurement during autosizing
  • Docs: New section and demo showing the feature usage
  • Test: Verification that columns are wider when header filters are included

📁 Important Files

  • useGridColumnResize.tsx - Core width measurement logic; adds header filter width to the pool of widths considered during autosizing
  • GridRootStyles.ts - Adds CSS overrides for header filter cells during autosize measurement (uses !important to override flex layout)
📋 Usage Example
<DataGridPremium
  headerFilters
  autosizeOptions={{
    includeHeaderFilters: true,
  }}
/>

Or programmatically:

apiRef.current.autosizeColumns({
  includeHeaderFilters: true,
});

🚀 Impact

  • Non-breaking: New optional flag defaults to false
  • Plan: Pro/Premium only (header filters are a Pro feature)
  • Closes: #13802

💡 Implementation Notes

Why flex: none and width: unset in CSS?

Header filter cells normally use flex: 1 and a fixed width which 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. Setting flex: none and width: unset allows the element to expand to its intrinsic size so scrollWidth returns the true content width.

Why window.getComputedStyle for padding instead of getBoundingClientRect or clientWidth?

  • getBoundingClientRect().width returns the rendered width (constrained by flex/column width), not the content's natural width
  • clientWidth includes padding but still reflects the constrained rendered width
  • scrollWidth gives the full content width including overflow, but excludes padding
  • getComputedStyle is used specifically to get the padding values to add to scrollWidth, giving us: scrollWidth + paddingLeft + paddingRight = true natural width

This matches the existing pattern used for header cells in the same file.

[!NOTE] The !important in GridRootStyles is scoped to the .MuiDataGrid--autosizing state and only affects header filter cells during measurement.

siriwatknp avatar Dec 01 '25 08:12 siriwatknp

Deploy preview: https://deploy-preview-20510--material-ui-x.netlify.app/

Updated pages:

Bundle size report

Bundle Parsed size Gzip size
@mui/x-data-grid 🔺+450B(+0.11%) 🔺+106B(+0.09%)
@mui/x-data-grid-pro 🔺+478B(+0.10%) 🔺+81B(+0.05%)
@mui/x-data-grid-premium 🔺+450B(+0.07%) 🔺+55B(+0.03%)
@mui/x-charts 0B(0.00%) 0B(0.00%)
@mui/x-charts-pro 0B(0.00%) 0B(0.00%)
@mui/x-charts-premium 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers-pro 0B(0.00%) 0B(0.00%)
@mui/x-tree-view 0B(0.00%) 0B(0.00%)
@mui/x-tree-view-pro 0B(0.00%) 0B(0.00%)

Details of bundle changes

Generated by :no_entry_sign: dangerJS against 54beb7962f04e6fb4c15a1ba4c1024bd5789e6fd

mui-bot avatar Dec 01 '25 09:12 mui-bot