Skip to content

Commit

Permalink
[data grid] Fix column dimensions import/export with flex and resizing (
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle authored and alexfauquette committed Aug 26, 2022
1 parent 1a392f8 commit 15118e1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const useGridColumnResize = (

colDefRef.current!.computedWidth = newWidth;
colDefRef.current!.width = newWidth;
colDefRef.current!.flex = undefined;
colDefRef.current!.flex = 0;

colElementRef.current!.style.width = `${newWidth}px`;
colElementRef.current!.style.minWidth = `${newWidth}px`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const FULL_INITIAL_STATE: GridInitialState = {
dimensions: {
idBis: {
width: 75,
maxWidth: Infinity,
maxWidth: -1,
minWidth: 50,
flex: undefined,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface GridColumnsState {
columnVisibilityModel: GridColumnVisibilityModel;
}

export type GridColumnDimensions = Pick<GridStateColDef, GridColumnDimensionProperties>;
export type GridColumnDimensions = { [key in GridColumnDimensionProperties]?: number };

export interface GridColumnsInitialState {
columnVisibilityModel?: GridColumnVisibilityModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function computeFlexColumnsWidth({
totalFlexUnits: number;
flexColumns: {
field: GridColDef['field'];
flex?: number;
flex?: number | null;
minWidth?: number;
maxWidth?: number;
}[];
Expand Down Expand Up @@ -266,11 +266,16 @@ export const applyInitialState = (
for (let i = 0; i < columnsWithUpdatedDimensions.length; i += 1) {
const field = columnsWithUpdatedDimensions[i];

newColumnLookup[field] = {
const newColDef: Omit<GridStateColDef, 'computedWidth'> = {
...newColumnLookup[field],
...dimensions[field],
hasBeenResized: true,
};

Object.entries(dimensions[field]).forEach(([key, value]) => {
newColDef[key as GridColumnDimensionProperties] = value === -1 ? Infinity : value;
});

newColumnLookup[field] = newColDef;
}

const newColumnsState: Omit<GridColumnsRawState, 'columnVisibilityModel'> = {
Expand Down Expand Up @@ -376,16 +381,22 @@ export const createColumnsState = ({
columnsStateWithoutColumnVisibilityModel.all.push(field);
}

let hasValidDimension = false;
if (!existingState.hasBeenResized) {
hasValidDimension = COLUMNS_DIMENSION_PROPERTIES.some((key) => newColumn[key] !== undefined);
}
let hasBeenResized = existingState.hasBeenResized;
COLUMNS_DIMENSION_PROPERTIES.forEach((key) => {
if (newColumn[key] !== undefined) {
hasBeenResized = true;

if (newColumn[key] === -1) {
newColumn[key] = Infinity;
}
}
});

columnsStateWithoutColumnVisibilityModel.lookup[field] = {
...existingState,
hide: newColumn.hide == null ? false : newColumn.hide,
...newColumn,
hasBeenResized: existingState.hasBeenResized || hasValidDimension,
hasBeenResized,
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,11 @@ export function useGridColumns(
if (colDef.hasBeenResized) {
const colDefDimensions: GridColumnDimensions = {};
COLUMNS_DIMENSION_PROPERTIES.forEach((propertyName) => {
colDefDimensions[propertyName] = colDef[propertyName];
let propertyValue: number | undefined = colDef[propertyName];
if (propertyValue === Infinity) {
propertyValue = -1;
}
colDefDimensions[propertyName] = propertyValue;
});
dimensions[colDef.field] = colDefDimensions;
}
Expand Down

0 comments on commit 15118e1

Please sign in to comment.