Skip to content

Commit

Permalink
attachPinnedStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
lauri865 committed Dec 3, 2024
1 parent 7c30993 commit e357324
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -323,9 +324,7 @@ const GridHeaderFilterCell = React.forwardRef<HTMLDivElement, GridHeaderFilterCe
};

const pinnedSide = rtlFlipSide(pinnedPosition, isRtl);
if (pinnedSide && pinnedOffset !== undefined) {
style[pinnedSide] = pinnedOffset;
}
attachPinnedStyle(style, pinnedSide, pinnedOffset);

return (
<div
Expand Down
23 changes: 12 additions & 11 deletions packages/x-data-grid/src/components/GridSkeletonLoadingOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { shouldCellShowLeftBorder, shouldCellShowRightBorder } from '../utils/ce
import { escapeOperandAttributeSelector } from '../utils/domUtils';
import { GridScrollbarFillerCell } from './GridScrollbarFillerCell';
import { rtlFlipSide } from '../utils/rtlFlipSide';
import { attachPinnedStyle } from '../internals/utils';

const SkeletonOverlay = styled('div', {
name: 'MuiDataGrid',
Expand Down Expand Up @@ -108,17 +109,17 @@ const GridSkeletonLoadingOverlay = React.forwardRef<
const sectionIndex = pinnedSide
? pinnedColumns[pinnedSide].findIndex((col) => 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,
Expand Down
5 changes: 2 additions & 3 deletions packages/x-data-grid/src/components/cell/GridCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
gridRowSpanningHiddenCellsSelector,
gridRowSpanningSpannedCellsSelector,
} from '../../hooks/features/rows/gridRowSpanningSelectors';
import { attachPinnedStyle } from '../../internals/utils';

export const gridPinnedColumnPositionLookup = {
[PinnedColumnPosition.LEFT]: GridPinnedColumnPosition.LEFT,
Expand Down Expand Up @@ -336,9 +337,7 @@ const GridCell = React.forwardRef<HTMLDivElement, GridCellProps>(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})`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);

Expand Down
9 changes: 9 additions & 0 deletions packages/x-data-grid/src/internals/utils/attachPinnedStyle.ts
Original file line number Diff line number Diff line change
@@ -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;
};
1 change: 1 addition & 0 deletions packages/x-data-grid/src/internals/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './computeSlots';
export * from './useProps';
export * from './propValidation';
export * from './gridRowGroupingUtils';
export * from './attachPinnedStyle';

0 comments on commit e357324

Please sign in to comment.