Skip to content

Commit

Permalink
[DataGrid] Fix autosizing with virtualized columns (@k-rajat19) (#15929)
Browse files Browse the repository at this point in the history
Co-authored-by: Rajat <robertdoweny2301@gmail.com>
Co-authored-by: Andrew Cherniavskii <andrew@mui.com>
  • Loading branch information
3 people authored Dec 18, 2024
1 parent 0b84c6c commit a035187
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/pages/x/api/data-grid/data-grid-premium.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"autosizeOptions": {
"type": {
"name": "shape",
"description": "{ columns?: Array&lt;string&gt;, expand?: bool, includeHeaders?: bool, includeOutliers?: bool, outliersFactor?: number }"
"description": "{ columns?: Array&lt;string&gt;, disableColumnVirtualization?: bool, expand?: bool, includeHeaders?: bool, includeOutliers?: bool, outliersFactor?: number }"
}
},
"cellModesModel": { "type": { "name": "object" } },
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/x/api/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"autosizeOptions": {
"type": {
"name": "shape",
"description": "{ columns?: Array&lt;string&gt;, expand?: bool, includeHeaders?: bool, includeOutliers?: bool, outliersFactor?: number }"
"description": "{ columns?: Array&lt;string&gt;, disableColumnVirtualization?: bool, expand?: bool, includeHeaders?: bool, includeOutliers?: bool, outliersFactor?: number }"
}
},
"cellModesModel": { "type": { "name": "object" } },
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/x/api/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"autosizeOptions": {
"type": {
"name": "shape",
"description": "{ columns?: Array&lt;string&gt;, expand?: bool, includeHeaders?: bool, includeOutliers?: bool, outliersFactor?: number }"
"description": "{ columns?: Array&lt;string&gt;, disableColumnVirtualization?: bool, expand?: bool, includeHeaders?: bool, includeOutliers?: bool, outliersFactor?: number }"
}
},
"cellModesModel": { "type": { "name": "object" } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ DataGridPremiumRaw.propTypes = {
*/
autosizeOptions: PropTypes.shape({
columns: PropTypes.arrayOf(PropTypes.string),
disableColumnVirtualization: PropTypes.bool,
expand: PropTypes.bool,
includeHeaders: PropTypes.bool,
includeOutliers: PropTypes.bool,
Expand Down
1 change: 1 addition & 0 deletions packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ DataGridProRaw.propTypes = {
*/
autosizeOptions: PropTypes.shape({
columns: PropTypes.arrayOf(PropTypes.string),
disableColumnVirtualization: PropTypes.bool,
expand: PropTypes.bool,
includeHeaders: PropTypes.bool,
includeOutliers: PropTypes.bool,
Expand Down
1 change: 1 addition & 0 deletions packages/x-data-grid/src/DataGrid/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ DataGridRaw.propTypes = {
*/
autosizeOptions: PropTypes.shape({
columns: PropTypes.arrayOf(PropTypes.string),
disableColumnVirtualization: PropTypes.bool,
expand: PropTypes.bool,
includeHeaders: PropTypes.bool,
includeOutliers: PropTypes.bool,
Expand Down
14 changes: 12 additions & 2 deletions packages/x-data-grid/src/components/GridRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { GridScrollbarFillerCell as ScrollbarFiller } from './GridScrollbarFille
import { getPinnedCellOffset } from '../internals/utils/getPinnedCellOffset';
import { useGridConfiguration } from '../hooks/utils/useGridConfiguration';
import { useGridPrivateApiContext } from '../hooks/utils/useGridPrivateApiContext';
import { gridVirtualizationColumnEnabledSelector } from '../hooks';

export interface GridRowProps extends React.HTMLAttributes<HTMLDivElement> {
row: GridRowModel;
Expand Down Expand Up @@ -133,7 +134,7 @@ const GridRow = React.forwardRef<HTMLDivElement, GridRowProps>(function GridRow(
const gridHasFiller = dimensions.columnsTotalWidth < dimensions.viewportOuterSize.width;
const editing = apiRef.current.getRowMode(rowId) === GridRowModes.Edit;
const editable = rootProps.editMode === GridEditModes.Row;

const hasColumnVirtualization = useGridSelector(apiRef, gridVirtualizationColumnEnabledSelector);
const hasFocusCell = focusedColumnIndex !== undefined;
const hasVirtualFocusCellLeft =
hasFocusCell &&
Expand Down Expand Up @@ -418,8 +419,17 @@ const GridRow = React.forwardRef<HTMLDivElement, GridRowProps>(function GridRow(
),
);
}
let firstColumnIndex;
let lastColumnIndex;
if (!rootProps.disableVirtualization && !hasColumnVirtualization) {
firstColumnIndex = 0;
lastColumnIndex = visibleColumns.length;
} else {
firstColumnIndex = renderContext.firstColumnIndex;
lastColumnIndex = renderContext.lastColumnIndex;
}

for (let i = renderContext.firstColumnIndex; i < renderContext.lastColumnIndex; i += 1) {
for (let i = firstColumnIndex; i < lastColumnIndex; i += 1) {
const column = visibleColumns[i];
const indexInSection = i - pinnedColumns.left.length;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
const isRtl = useRtl();
const rootProps = useGridRootProps();
const dimensions = useGridSelector(apiRef, gridDimensionsSelector);
const hasVirtualization = useGridSelector(apiRef, gridVirtualizationColumnEnabledSelector);
const hasColumnVirtualization = useGridSelector(apiRef, gridVirtualizationColumnEnabledSelector);
const columnGroupsModel = useGridSelector(apiRef, gridColumnGroupsUnwrappedModelSelector);
const columnPositions = useGridSelector(apiRef, gridColumnPositionsSelector);
const renderContext = useGridSelector(apiRef, gridRenderContextColumnsSelector);
Expand Down Expand Up @@ -162,8 +162,15 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
const { renderContext: currentContext = renderContext, maxLastColumn = visibleColumns.length } =
params || {};

const firstColumnToRender = currentContext.firstColumnIndex;
const lastColumnToRender = !hasVirtualization ? maxLastColumn : currentContext.lastColumnIndex;
let firstColumnToRender;
let lastColumnToRender;
if (!rootProps.disableVirtualization && !hasColumnVirtualization) {
firstColumnToRender = 0;
lastColumnToRender = maxLastColumn;
} else {
firstColumnToRender = currentContext.firstColumnIndex;
lastColumnToRender = currentContext.lastColumnIndex;
}
const renderedColumns = visibleColumns.slice(firstColumnToRender, lastColumnToRender);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const DEFAULT_GRID_AUTOSIZE_OPTIONS = {
includeOutliers: false,
outliersFactor: 1.5,
expand: false,
disableColumnVirtualization: true,
};

export type GridAutosizeOptions = {
Expand Down Expand Up @@ -32,6 +33,11 @@ export type GridAutosizeOptions = {
* @default false
*/
expand?: boolean;
/**
* If false, column virtualization is not disabled while resizing.
* @default true
*/
disableColumnVirtualization?: boolean;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export const useGridColumnResize = (
| 'disableAutosize'
| 'onColumnResize'
| 'onColumnWidthChange'
| 'disableVirtualization'
>,
) => {
const isRtl = useRtl();
Expand Down Expand Up @@ -685,6 +686,7 @@ export const useGridColumnResize = (

apiRef.current.autosizeColumns({
...props.autosizeOptions,
disableColumnVirtualization: false,
columns: [column.field],
});
});
Expand Down Expand Up @@ -717,8 +719,10 @@ export const useGridColumnResize = (
const columns = options.columns.map((c) => apiRef.current.state.columns.lookup[c]);

try {
apiRef.current.unstable_setColumnVirtualization(false);
await columnVirtualizationDisabled();
if (!props.disableVirtualization && options.disableColumnVirtualization) {
apiRef.current.unstable_setColumnVirtualization(false);
await columnVirtualizationDisabled();
}

const widthByField = extractColumnWidths(apiRef, options, columns);

Expand Down Expand Up @@ -764,11 +768,14 @@ export const useGridColumnResize = (
}
});
} finally {
apiRef.current.unstable_setColumnVirtualization(true);
if (!props.disableVirtualization) {
apiRef.current.unstable_setColumnVirtualization(true);
}

isAutosizingRef.current = false;
}
},
[apiRef, columnVirtualizationDisabled],
[apiRef, columnVirtualizationDisabled, props.disableVirtualization],
);

/**
Expand Down

0 comments on commit a035187

Please sign in to comment.