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 column dimensions import/export with flex and resizing #4311

Merged
merged 6 commits into from
Apr 11, 2022
Merged
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
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn't seem necessary to fix #4310 but makes sense to set 0.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I did not do this one, I had the flex coming back on re-import.
Maybe it only occurs in some scenario.

As 0 make sense, I propose to keep it.


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