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

[Data Grid] Fix row, cell and header memoizations #15666

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions docs/pages/x/api/data-grid/selectors.json
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,13 @@
"description": "Get the visible pinned columns.",
"supportsApiRef": true
},
{
"name": "gridVisibleRowsSelector",
"returnType": "{ rows: GridRowEntry<GridValidRowModel>[]; range: { firstRowIndex: number; lastRowIndex: number } | null; rowToIndexMap: Map<GridValidRowModel, number> }",
"category": "Pagination",
"description": "Get the rows, range and rowIndex lookup map after filtering and sorting.\nDoes not contain the collapsed children.",
"supportsApiRef": true
},
{
"name": "selectedGridRowsCountSelector",
"returnType": "number",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { useLicenseVerifier, Watermark } from '@mui/x-license';
import {
GridBody,
GridFooterPlaceholder,
GridHeader,
GridRoot,
GridContextProvider,
GridValidRowModel,
} from '@mui/x-data-grid-pro';
import { GridRoot, GridContextProvider, GridValidRowModel } from '@mui/x-data-grid-pro';
import {
propValidatorsDataGrid,
propValidatorsDataGridPro,
Expand Down Expand Up @@ -62,11 +55,7 @@ const DataGridPremiumRaw = React.forwardRef(function DataGridPremium<R extends G
ref={ref}
{...props.slotProps?.root}
>
<GridHeader />
<GridBody>
<Watermark packageName="x-data-grid-premium" releaseInfo={releaseInfo} />
</GridBody>
<GridFooterPlaceholder />
<Watermark packageName="x-data-grid-premium" releaseInfo={releaseInfo} />
</GridRoot>
</GridContextProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export const useDataGridPremiumComponent = (
useGridInitializeState(columnPinningStateInitializer, apiRef, props);
useGridInitializeState(columnsStateInitializer, apiRef, props);
useGridInitializeState(rowPinningStateInitializer, apiRef, props);
useGridInitializeState(paginationStateInitializer, apiRef, props);
useGridInitializeState(rowsStateInitializer, apiRef, props);
useGridInitializeState(editingStateInitializer, apiRef, props);
useGridInitializeState(focusStateInitializer, apiRef, props);
Expand All @@ -142,7 +143,6 @@ export const useDataGridPremiumComponent = (
useGridInitializeState(densityStateInitializer, apiRef, props);
useGridInitializeState(columnReorderStateInitializer, apiRef, props);
useGridInitializeState(columnResizeStateInitializer, apiRef, props);
useGridInitializeState(paginationStateInitializer, apiRef, props);
useGridInitializeState(rowsMetaStateInitializer, apiRef, props);
useGridInitializeState(columnMenuStateInitializer, apiRef, props);
useGridInitializeState(columnGroupsStateInitializer, apiRef, props);
Expand Down
15 changes: 2 additions & 13 deletions packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { useLicenseVerifier, Watermark } from '@mui/x-license';
import {
GridBody,
GridFooterPlaceholder,
GridHeader,
GridRoot,
GridContextProvider,
GridValidRowModel,
} from '@mui/x-data-grid';
import { GridRoot, GridContextProvider, GridValidRowModel } from '@mui/x-data-grid';
import { validateProps } from '@mui/x-data-grid/internals';
import { useDataGridProComponent } from './useDataGridProComponent';
import { DataGridProProps } from '../models/dataGridProProps';
Expand Down Expand Up @@ -49,11 +42,7 @@ const DataGridProRaw = React.forwardRef(function DataGridPro<R extends GridValid
ref={ref}
{...props.slotProps?.root}
>
<GridHeader />
<GridBody>
<Watermark packageName="x-data-grid-pro" releaseInfo={releaseInfo} />
</GridBody>
<GridFooterPlaceholder />
<Watermark packageName="x-data-grid-pro" releaseInfo={releaseInfo} />
</GridRoot>
</GridContextProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const useDataGridProComponent = (
useGridInitializeState(columnPinningStateInitializer, apiRef, props);
useGridInitializeState(columnsStateInitializer, apiRef, props);
useGridInitializeState(rowPinningStateInitializer, apiRef, props);
useGridInitializeState(paginationStateInitializer, apiRef, props);
useGridInitializeState(rowsStateInitializer, apiRef, props);
useGridInitializeState(editingStateInitializer, apiRef, props);
useGridInitializeState(focusStateInitializer, apiRef, props);
Expand All @@ -129,7 +130,6 @@ export const useDataGridProComponent = (
useGridInitializeState(densityStateInitializer, apiRef, props);
useGridInitializeState(columnReorderStateInitializer, apiRef, props);
useGridInitializeState(columnResizeStateInitializer, apiRef, props);
useGridInitializeState(paginationStateInitializer, apiRef, props);
useGridInitializeState(rowsMetaStateInitializer, apiRef, props);
useGridInitializeState(columnMenuStateInitializer, apiRef, props);
useGridInitializeState(columnGroupsStateInitializer, apiRef, props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ import {
GridFilterInputSingleSelect,
gridFilterModelSelector,
gridFilterableColumnLookupSelector,
GridPinnedColumnPosition,
} from '@mui/x-data-grid';
import {
PinnedColumnPosition,
GridStateColDef,
useGridPrivateApiContext,
gridHeaderFilteringEditFieldSelector,
gridHeaderFilteringMenuSelector,
isNavigationKey,
shouldCellShowLeftBorder,
shouldCellShowRightBorder,
rtlFlipSide,
attachPinnedStyle,
} from '@mui/x-data-grid/internals';
import { useRtl } from '@mui/system/RtlProvider';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
import { DataGridProProcessedProps } from '../../models/dataGridProProps';
import { GridHeaderFilterMenuContainer } from './GridHeaderFilterMenuContainer';
Expand All @@ -54,16 +55,16 @@ export interface GridHeaderFilterCellProps extends Pick<GridStateColDef, 'header
item: GridFilterItem;
showClearIcon?: boolean;
InputComponentProps: GridFilterOperator['InputComponentProps'];
pinnedPosition?: GridPinnedColumnPosition;
pinnedPosition?: PinnedColumnPosition;
pinnedOffset?: number;
style?: React.CSSProperties;
indexInSection: number;
sectionLength: number;
gridHasFiller: boolean;
showLeftBorder: boolean;
showRightBorder: boolean;
}

type OwnerState = DataGridProProcessedProps & {
colDef: GridColDef;
pinnedPosition?: GridPinnedColumnPosition;
pinnedPosition?: PinnedColumnPosition;
showRightBorder: boolean;
showLeftBorder: boolean;
};
Expand All @@ -80,8 +81,8 @@ const useUtilityClasses = (ownerState: OwnerState) => {
'withBorderColor',
showRightBorder && 'columnHeader--withRightBorder',
showLeftBorder && 'columnHeader--withLeftBorder',
pinnedPosition === 'left' && 'columnHeader--pinnedLeft',
pinnedPosition === 'right' && 'columnHeader--pinnedRight',
pinnedPosition === PinnedColumnPosition.LEFT && 'columnHeader--pinnedLeft',
pinnedPosition === PinnedColumnPosition.RIGHT && 'columnHeader--pinnedRight',
],
};

Expand Down Expand Up @@ -115,14 +116,15 @@ const GridHeaderFilterCell = React.forwardRef<HTMLDivElement, GridHeaderFilterCe
InputComponentProps,
showClearIcon = true,
pinnedPosition,
pinnedOffset,
style: styleProp,
indexInSection,
sectionLength,
gridHasFiller,
showLeftBorder,
showRightBorder,
...other
} = props;

const apiRef = useGridPrivateApiContext();
const isRtl = useRtl();
const columnFields = useGridSelector(apiRef, gridVisibleColumnFieldsSelector);
const rootProps = useGridRootProps();
const cellRef = React.useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -293,15 +295,6 @@ const GridHeaderFilterCell = React.forwardRef<HTMLDivElement, GridHeaderFilterCe
[onMouseDown, onKeyDown, publish],
);

const showLeftBorder = shouldCellShowLeftBorder(pinnedPosition, indexInSection);
const showRightBorder = shouldCellShowRightBorder(
pinnedPosition,
indexInSection,
sectionLength,
rootProps.showCellVerticalBorder,
gridHasFiller,
);

const ownerState: OwnerState = {
...rootProps,
pinnedPosition,
Expand All @@ -324,15 +317,20 @@ const GridHeaderFilterCell = React.forwardRef<HTMLDivElement, GridHeaderFilterCe

const isFilterActive = isApplied || hasFocus;

const style = {
height,
width,
...styleProp,
};

const pinnedSide = rtlFlipSide(pinnedPosition, isRtl);
attachPinnedStyle(style, pinnedSide, pinnedOffset);

return (
<div
className={clsx(classes.root, headerClassName)}
ref={handleRef}
style={{
height,
width,
...styleProp,
}}
style={style}
role="columnheader"
aria-colindex={colIndex + 1}
aria-label={headerFilterComponent == null ? (colDef.headerName ?? colDef.field) : undefined}
Expand Down Expand Up @@ -405,7 +403,6 @@ GridHeaderFilterCell.propTypes = {
// ----------------------------------------------------------------------
colDef: PropTypes.object.isRequired,
colIndex: PropTypes.number.isRequired,
gridHasFiller: PropTypes.bool.isRequired,
hasFocus: PropTypes.bool,
/**
* Class name added to the column header cell.
Expand All @@ -415,17 +412,18 @@ GridHeaderFilterCell.propTypes = {
current: PropTypes.object,
}).isRequired,
height: PropTypes.number.isRequired,
indexInSection: PropTypes.number.isRequired,
InputComponentProps: PropTypes.object,
item: PropTypes.shape({
field: PropTypes.string.isRequired,
id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
operator: PropTypes.string.isRequired,
value: PropTypes.any,
}).isRequired,
pinnedPosition: PropTypes.oneOf(['left', 'right']),
sectionLength: PropTypes.number.isRequired,
pinnedOffset: PropTypes.number,
pinnedPosition: PropTypes.oneOf([0, 1, 2, 3]),
showClearIcon: PropTypes.bool,
showLeftBorder: PropTypes.bool.isRequired,
showRightBorder: PropTypes.bool.isRequired,
sortIndex: PropTypes.number,
style: PropTypes.object,
tabIndex: PropTypes.oneOf([-1, 0]).isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
gridTabIndexColumnHeaderFilterSelector,
getDataGridUtilityClass,
GridFilterItem,
GridPinnedColumnPosition,
gridDimensionsSelector,
} from '@mui/x-data-grid';
import {
Expand All @@ -17,6 +16,9 @@ import {
getGridFilter,
GridStateColDef,
GridColumnHeaderRow,
shouldCellShowLeftBorder,
shouldCellShowRightBorder,
PinnedColumnPosition,
} from '@mui/x-data-grid/internals';
import composeClasses from '@mui/utils/composeClasses';
import { useGridRootProps } from '../../utils/useGridRootProps';
Expand Down Expand Up @@ -46,12 +48,13 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
);
const {
getColumnsToRender,
getPinnedCellOffset,
renderContext,
leftRenderContext,
rightRenderContext,
pinnedColumns,
visibleColumns,
getCellOffsetStyle,
columnPositions,
...otherProps
} = useGridColumnHeadersCommunity({
...props,
Expand Down Expand Up @@ -117,11 +120,27 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
const item = getFilterItem(colDef);

const pinnedPosition = params?.position;
const style = getCellOffsetStyle({
const scrollbarWidth = dimensions.hasScrollY ? dimensions.scrollbarSize : 0;
const pinnedOffset = getPinnedCellOffset(
pinnedPosition,
colDef.computedWidth,
columnIndex,
computedWidth: colDef.computedWidth,
});
columnPositions,
dimensions.columnsTotalWidth,
scrollbarWidth,
);

const indexInSection = i;
const sectionLength = renderedColumns.length;

const showLeftBorder = shouldCellShowLeftBorder(pinnedPosition, indexInSection);
const showRightBorder = shouldCellShowRightBorder(
pinnedPosition,
indexInSection,
sectionLength,
rootProps.showCellVerticalBorder,
gridHasFiller,
);

filters.push(
<rootProps.slots.headerFilterCell
Expand All @@ -137,10 +156,9 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
data-field={colDef.field}
item={item}
pinnedPosition={pinnedPosition}
style={style}
indexInSection={i}
sectionLength={renderedColumns.length}
gridHasFiller={gridHasFiller}
pinnedOffset={pinnedOffset}
showLeftBorder={showLeftBorder}
showRightBorder={showRightBorder}
{...rootProps.slotProps?.headerFilterCell}
/>,
);
Expand All @@ -164,7 +182,7 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
>
{leftRenderContext &&
getColumnFilters({
position: GridPinnedColumnPosition.LEFT,
position: PinnedColumnPosition.LEFT,
renderContext: leftRenderContext,
maxLastColumn: leftRenderContext.lastColumnIndex,
})}
Expand All @@ -174,7 +192,7 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
})}
{rightRenderContext &&
getColumnFilters({
position: GridPinnedColumnPosition.RIGHT,
position: PinnedColumnPosition.RIGHT,
renderContext: rightRenderContext,
maxLastColumn: rightRenderContext.lastColumnIndex,
})}
Expand Down
8 changes: 2 additions & 6 deletions packages/x-data-grid/src/DataGrid/DataGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { GridBody, GridFooterPlaceholder, GridHeader, GridRoot } from '../components';
import { GridRoot } from '../components';
import { useGridAriaAttributes } from '../hooks/utils/useGridAriaAttributes';
import { useGridRowAriaAttributes } from '../hooks/features/rows/useGridRowAriaAttributes';
import { DataGridProcessedProps, DataGridProps } from '../models/props/DataGridProps';
Expand Down Expand Up @@ -60,11 +60,7 @@ const DataGridRaw = React.forwardRef(function DataGrid<R extends GridValidRowMod
sx={props.sx}
ref={ref}
{...props.slotProps?.root}
>
<GridHeader />
<GridBody />
<GridFooterPlaceholder />
</GridRoot>
/>
</GridContextProvider>
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/x-data-grid/src/DataGrid/useDataGridComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const useDataGridComponent = (
useGridInitializeState(dimensionsStateInitializer, apiRef, props);
useGridInitializeState(rowSelectionStateInitializer, apiRef, props);
useGridInitializeState(columnsStateInitializer, apiRef, props);
useGridInitializeState(paginationStateInitializer, apiRef, props);
useGridInitializeState(rowsStateInitializer, apiRef, props);
useGridInitializeState(editingStateInitializer, apiRef, props);
useGridInitializeState(focusStateInitializer, apiRef, props);
Expand All @@ -92,7 +93,6 @@ export const useDataGridComponent = (
useGridInitializeState(rowSpanningStateInitializer, apiRef, props);
useGridInitializeState(densityStateInitializer, apiRef, props);
useGridInitializeState(columnResizeStateInitializer, apiRef, props);
useGridInitializeState(paginationStateInitializer, apiRef, props);
useGridInitializeState(rowsMetaStateInitializer, apiRef, props);
useGridInitializeState(columnMenuStateInitializer, apiRef, props);
useGridInitializeState(columnGroupsStateInitializer, apiRef, props);
Expand Down
Loading
Loading