Skip to content

Commit c80a645

Browse files
[DataGrid] Fix extending built-in column types (mui#4114)
Co-authored-by: Matheus Wichman <[email protected]>
1 parent deb6615 commit c80a645

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

packages/grid/x-data-grid/src/hooks/features/columns/gridColumnsUtils.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@ import { gridColumnsSelector, gridColumnVisibilityModelSelector } from './gridCo
1818
import { clamp } from '../../../utils/utils';
1919

2020
export const computeColumnTypes = (customColumnTypes: GridColumnTypesRecord = {}) => {
21-
const allColumnTypes = { ...getGridDefaultColumnTypes(), ...customColumnTypes };
22-
const mergedColumnTypes: GridColumnTypesRecord = {};
21+
const mergedColumnTypes: GridColumnTypesRecord = { ...getGridDefaultColumnTypes() };
2322

24-
Object.entries(allColumnTypes).forEach(([colType, colTypeDef]) => {
25-
colTypeDef = {
26-
...allColumnTypes[colTypeDef.extendType || DEFAULT_GRID_COL_TYPE_KEY],
27-
...colTypeDef,
28-
};
29-
mergedColumnTypes[colType] = colTypeDef;
23+
Object.entries(customColumnTypes).forEach(([colType, colTypeDef]) => {
24+
if (mergedColumnTypes[colType]) {
25+
mergedColumnTypes[colType] = {
26+
...mergedColumnTypes[colType],
27+
...colTypeDef,
28+
};
29+
} else {
30+
mergedColumnTypes[colType] = {
31+
...mergedColumnTypes[colTypeDef.extendType || DEFAULT_GRID_COL_TYPE_KEY],
32+
...colTypeDef,
33+
};
34+
}
3035
});
3136

3237
return mergedColumnTypes;

packages/grid/x-data-grid/src/tests/layout.DataGrid.test.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,49 @@ describe('<DataGrid /> - Layout & Warnings', () => {
858858
) as Element;
859859
expect(virtualScrollerContent.clientHeight).to.equal(virtualScroller.clientHeight);
860860
});
861+
862+
// See https://github.com/mui/mui-x/issues/4113
863+
it('should preserve default width constraints when extending default column type', () => {
864+
const rows = [{ id: 1, value: 1 }];
865+
const columns = [{ field: 'id', type: 'number' }];
866+
867+
render(
868+
<div style={{ width: 300, height: 300 }}>
869+
<DataGrid
870+
rows={rows}
871+
columns={columns}
872+
columnTypes={{
873+
number: {},
874+
}}
875+
/>
876+
</div>,
877+
);
878+
879+
// default `width` should be used
880+
expect(getCell(0, 0).offsetWidth).to.equal(100);
881+
});
882+
883+
it('should allow to override default width constraints when extending default column type', () => {
884+
const rows = [{ id: 1, value: 1 }];
885+
const columns = [{ field: 'id', type: 'number' }];
886+
887+
render(
888+
<div style={{ width: 300, height: 300 }}>
889+
<DataGrid
890+
rows={rows}
891+
columns={columns}
892+
columnTypes={{
893+
number: {
894+
width: 10,
895+
minWidth: 200,
896+
},
897+
}}
898+
/>
899+
</div>,
900+
);
901+
902+
expect(getCell(0, 0).offsetWidth).to.equal(200);
903+
});
861904
});
862905

863906
describe('warnings', () => {

0 commit comments

Comments
 (0)