Skip to content

Commit

Permalink
[DataGrid] Fix flex column width when used with pinned columns (#12849)
Browse files Browse the repository at this point in the history
  • Loading branch information
romgrk authored Apr 19, 2024
1 parent 4b82c29 commit 6e11e1e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { GridRowEntry } from '../../../models/gridRows';
import { gridDensityFactorSelector } from '../density/densitySelector';
import { gridHeaderFilteringEnabledSelector } from '../headerFiltering/gridHeaderFilteringSelectors';
import { gridColumnGroupsHeaderMaxDepthSelector } from '../columnGrouping/gridColumnGroupsSelector';
import type { GridDimensions } from '../dimensions/gridDimensionsApi';

export const COLUMNS_DIMENSION_PROPERTIES = ['maxWidth', 'minWidth', 'width', 'flex'] as const;

Expand Down Expand Up @@ -161,7 +162,7 @@ export function computeFlexColumnsWidth({
*/
export const hydrateColumnsWidth = (
rawState: GridColumnsRawState,
viewportInnerWidth: number,
dimensions: GridDimensions | undefined,
): GridColumnsState => {
const columnsLookup: GridColumnLookup = {};
let totalFlexUnits = 0;
Expand Down Expand Up @@ -196,10 +197,14 @@ export const hydrateColumnsWidth = (
columnsLookup[columnField] = newColumn;
});

const initialFreeSpace = Math.max(viewportInnerWidth - widthAllocatedBeforeFlex, 0);
const availableWidth =
dimensions === undefined
? 0
: dimensions.viewportOuterSize.width - (dimensions.hasScrollY ? dimensions.scrollbarSize : 0);
const initialFreeSpace = Math.max(availableWidth - widthAllocatedBeforeFlex, 0);

// Allocate the remaining space to the flex columns
if (totalFlexUnits > 0 && viewportInnerWidth > 0) {
if (totalFlexUnits > 0 && availableWidth > 0) {
const computedColumnWidths = computeFlexColumnsWidth({
initialFreeSpace,
totalFlexUnits,
Expand Down Expand Up @@ -395,7 +400,7 @@ export const createColumnsState = ({

return hydrateColumnsWidth(
columnsStateWithPortableColumns,
apiRef.current.getRootDimensions?.().viewportInnerSize.width ?? 0,
apiRef.current.getRootDimensions?.() ?? undefined,
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export function useGridColumns(
[field]: newColumn,
},
},
apiRef.current.getRootDimensions().viewportInnerSize.width,
apiRef.current.getRootDimensions(),
),
);

Expand Down Expand Up @@ -386,9 +386,10 @@ export function useGridColumns(
useGridRegisterPipeProcessor(apiRef, 'restoreState', stateRestorePreProcessing);
useGridRegisterPipeProcessor(apiRef, 'preferencePanel', preferencePanelPreProcessing);

/**
/*
* EVENTS
*/

const prevInnerWidth = React.useRef<number | null>(null);
const handleGridSizeChange: GridEventListener<'viewportInnerSizeChange'> = (
viewportInnerSize,
Expand All @@ -398,7 +399,7 @@ export function useGridColumns(
setGridColumnsState(
hydrateColumnsWidth(
gridColumnsStateSelector(apiRef.current.state),
viewportInnerSize.width,
apiRef.current.getRootDimensions(),
),
);
}
Expand All @@ -423,7 +424,7 @@ export function useGridColumns(

useGridRegisterPipeApplier(apiRef, 'hydrateColumns', hydrateColumns);

/**
/*
* EFFECTS
*/
// The effect do not track any value defined synchronously during the 1st render by hooks called after `useGridColumns`
Expand Down

0 comments on commit 6e11e1e

Please sign in to comment.