diff --git a/packages/x-data-grid-pro/src/components/headerFiltering/GridHeaderFilterCell.tsx b/packages/x-data-grid-pro/src/components/headerFiltering/GridHeaderFilterCell.tsx index 722018a766d65..0c28cfed8e91e 100644 --- a/packages/x-data-grid-pro/src/components/headerFiltering/GridHeaderFilterCell.tsx +++ b/packages/x-data-grid-pro/src/components/headerFiltering/GridHeaderFilterCell.tsx @@ -31,6 +31,7 @@ import { gridHeaderFilteringMenuSelector, isNavigationKey, rtlFlipSide, + attachPinnedStyle, } from '@mui/x-data-grid/internals'; import { useRtl } from '@mui/system/RtlProvider'; import { useGridRootProps } from '../../hooks/utils/useGridRootProps'; @@ -323,9 +324,7 @@ const GridHeaderFilterCell = React.forwardRef col.field === column.field) // pinned section : colIndex - pinnedColumns.left.length; // middle section - const pinnedStyle = pinnedSide - ? { - [pinnedSide]: getPinnedCellOffset( - pinnedPosition, - column.computedWidth, - colIndex, - positions, - dimensions, - ), - } - : {}; + const pinnedStyle = attachPinnedStyle( + {}, + pinnedSide, + getPinnedCellOffset( + pinnedPosition, + column.computedWidth, + colIndex, + positions, + dimensions, + ), + ); const gridHasFiller = dimensions.columnsTotalWidth < dimensions.viewportOuterSize.width; const showRightBorder = shouldCellShowRightBorder( pinnedPosition, diff --git a/packages/x-data-grid/src/components/cell/GridCell.tsx b/packages/x-data-grid/src/components/cell/GridCell.tsx index 0b5a011cf76f7..413f7c717f5df 100644 --- a/packages/x-data-grid/src/components/cell/GridCell.tsx +++ b/packages/x-data-grid/src/components/cell/GridCell.tsx @@ -38,6 +38,7 @@ import { gridRowSpanningHiddenCellsSelector, gridRowSpanningSpannedCellsSelector, } from '../../hooks/features/rows/gridRowSpanningSelectors'; +import { attachPinnedStyle } from '../../internals/utils'; export const gridPinnedColumnPositionLookup = { [PinnedColumnPosition.LEFT]: GridPinnedColumnPosition.LEFT, @@ -336,9 +337,7 @@ const GridCell = React.forwardRef(function GridCe const isRightPinned = pinnedPosition === PinnedColumnPosition.RIGHT; const pinnedSide = rtlFlipSide(pinnedPosition, isRtl); - if (pinnedSide && pinnedOffset !== undefined) { - cellStyle[pinnedSide] = pinnedOffset; - } + attachPinnedStyle(cellStyle, pinnedSide, pinnedOffset); if (rowSpan > 1) { cellStyle.height = `calc(var(--height) * ${rowSpan})`; diff --git a/packages/x-data-grid/src/components/columnHeaders/GridColumnGroupHeader.tsx b/packages/x-data-grid/src/components/columnHeaders/GridColumnGroupHeader.tsx index 5b9e5147fda4e..08e0ad02ff137 100644 --- a/packages/x-data-grid/src/components/columnHeaders/GridColumnGroupHeader.tsx +++ b/packages/x-data-grid/src/components/columnHeaders/GridColumnGroupHeader.tsx @@ -15,6 +15,7 @@ import { GridColumnGroupHeaderEventLookup } from '../../models/events'; import { GridColumnGroupHeaderParams } from '../../models/params'; import { isEventTargetInPortal } from '../../utils/domUtils'; import { PinnedColumnPosition } from '../../internals/constants'; +import { attachPinnedStyle } from '../../internals/utils'; interface GridColumnGroupHeaderProps { groupId: string | null; @@ -178,11 +179,9 @@ function GridColumnGroupHeader(props: GridColumnGroupHeaderProps) { : group.headerClassName; const style = React.useMemo(() => { - let styleProp = props.style; + const styleProp = { ...props.style }; const pinnedSide = rtlFlipSide(pinnedPosition, isRtl); - if (pinnedSide && pinnedOffset !== undefined) { - styleProp = { ...styleProp, [pinnedSide]: pinnedOffset }; - } + attachPinnedStyle(styleProp, pinnedSide, pinnedOffset); return styleProp; }, [pinnedPosition, pinnedOffset, props.style, isRtl]); diff --git a/packages/x-data-grid/src/components/columnHeaders/GridColumnHeaderItem.tsx b/packages/x-data-grid/src/components/columnHeaders/GridColumnHeaderItem.tsx index 69d18aa8f4a90..804fb174748b0 100644 --- a/packages/x-data-grid/src/components/columnHeaders/GridColumnHeaderItem.tsx +++ b/packages/x-data-grid/src/components/columnHeaders/GridColumnHeaderItem.tsx @@ -18,6 +18,7 @@ import { GridGenericColumnHeaderItem } from './GridGenericColumnHeaderItem'; import { GridColumnHeaderEventLookup } from '../../models/events'; import { isEventTargetInPortal } from '../../utils/domUtils'; import { PinnedColumnPosition } from '../../internals/constants'; +import { attachPinnedStyle } from '../../internals/utils'; interface GridColumnHeaderItemProps { colIndex: number; @@ -280,11 +281,9 @@ function GridColumnHeaderItem(props: GridColumnHeaderItemProps) { const label = colDef.headerName ?? colDef.field; const style = React.useMemo(() => { - let styleProp = props.style; + const styleProp = { ...props.style }; const pinnedSide = rtlFlipSide(pinnedPosition, isRtl); - if (pinnedSide && pinnedOffset !== undefined) { - styleProp = { ...styleProp, [pinnedSide]: pinnedOffset }; - } + attachPinnedStyle(styleProp, pinnedSide, pinnedOffset); return styleProp; }, [pinnedPosition, pinnedOffset, props.style, isRtl]); diff --git a/packages/x-data-grid/src/internals/utils/attachPinnedStyle.ts b/packages/x-data-grid/src/internals/utils/attachPinnedStyle.ts new file mode 100644 index 0000000000000..bd24b45d6ab4b --- /dev/null +++ b/packages/x-data-grid/src/internals/utils/attachPinnedStyle.ts @@ -0,0 +1,9 @@ +export const attachPinnedStyle = ( + style: React.CSSProperties, + position: 'left' | 'right' | undefined, + pinnedOffset?: number, +) => { + if (!position || pinnedOffset === undefined) return style; + style[position] = pinnedOffset; + return style; +}; diff --git a/packages/x-data-grid/src/internals/utils/index.ts b/packages/x-data-grid/src/internals/utils/index.ts index 4dd26999637b1..8c4f1160dfb40 100644 --- a/packages/x-data-grid/src/internals/utils/index.ts +++ b/packages/x-data-grid/src/internals/utils/index.ts @@ -2,3 +2,4 @@ export * from './computeSlots'; export * from './useProps'; export * from './propValidation'; export * from './gridRowGroupingUtils'; +export * from './attachPinnedStyle';