Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGrid] Fix autosizing with virtualized columns (@k-rajat19) #15929

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<string>, expand?: bool, includeHeaders?: bool, includeOutliers?: bool, outliersFactor?: number }"
"description": "{ columns?: Array<string>, 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<string>, expand?: bool, includeHeaders?: bool, includeOutliers?: bool, outliersFactor?: number }"
"description": "{ columns?: Array<string>, 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<string>, expand?: bool, includeHeaders?: bool, includeOutliers?: bool, outliersFactor?: number }"
"description": "{ columns?: Array<string>, 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