Skip to content

Commit

Permalink
[DataGrid] Reduce bundle size with error messages (#12992)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored May 10, 2024
1 parent 80a8b66 commit b66c1e5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ export type { GridPremiumSlotsComponent as GridSlots } from '../models';

const releaseInfo = getReleaseInfo();

const dataGridPremiumPropValidators: PropValidator<DataGridPremiumProcessedProps>[] = [
...propValidatorsDataGrid,
...propValidatorsDataGridPro,
];
let dataGridPremiumPropValidators: PropValidator<DataGridPremiumProcessedProps>[];

if (process.env.NODE_ENV !== 'production') {
dataGridPremiumPropValidators = [...propValidatorsDataGrid, ...propValidatorsDataGridPro];
}

const DataGridPremiumRaw = React.forwardRef(function DataGridPremium<R extends GridValidRowModel>(
inProps: DataGridPremiumProps<R>,
Expand All @@ -42,7 +43,9 @@ const DataGridPremiumRaw = React.forwardRef(function DataGridPremium<R extends G

useLicenseVerifier('x-data-grid-premium', releaseInfo);

validateProps(props, dataGridPremiumPropValidators);
if (process.env.NODE_ENV !== 'production') {
validateProps(props, dataGridPremiumPropValidators);
}
return (
<GridContextProvider privateApiRef={privateApiRef} props={props}>
<GridRoot
Expand Down
4 changes: 3 additions & 1 deletion packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ const DataGridProRaw = React.forwardRef(function DataGridPro<R extends GridValid
const privateApiRef = useDataGridProComponent(props.apiRef, props);
useLicenseVerifier('x-data-grid-pro', releaseInfo);

validateProps(props, propValidatorsDataGridPro);
if (process.env.NODE_ENV !== 'production') {
validateProps(props, propValidatorsDataGridPro);
}
return (
<GridContextProvider privateApiRef={privateApiRef} props={props}>
<GridRoot
Expand Down
37 changes: 21 additions & 16 deletions packages/x-data-grid/src/DataGrid/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ import {

export type { GridSlotsComponent as GridSlots } from '../models';

const propValidators: PropValidator<DataGridProcessedProps>[] = [
...propValidatorsDataGrid,
// Only validate in MIT version
(props) =>
(props.columns &&
props.columns.some((column) => column.resizable) &&
[
`MUI X: \`column.resizable = true\` is not a valid prop.`,
'Column resizing is not available in the MIT version.',
'',
'You need to upgrade to DataGridPro or DataGridPremium component to unlock this feature.',
].join('\n')) ||
undefined,
];
let propValidators: PropValidator<DataGridProcessedProps>[];

if (process.env.NODE_ENV !== 'production') {
propValidators = [
...propValidatorsDataGrid,
// Only validate in MIT version
(props) =>
(props.columns &&
props.columns.some((column) => column.resizable) &&
[
`MUI X: \`column.resizable = true\` is not a valid prop.`,
'Column resizing is not available in the MIT version.',
'',
'You need to upgrade to DataGridPro or DataGridPremium component to unlock this feature.',
].join('\n')) ||
undefined,
];
}

const DataGridRaw = React.forwardRef(function DataGrid<R extends GridValidRowModel>(
inProps: DataGridProps<R>,
Expand All @@ -37,8 +41,9 @@ const DataGridRaw = React.forwardRef(function DataGrid<R extends GridValidRowMod
const props = useDataGridProps(inProps);
const privateApiRef = useDataGridComponent(props.apiRef, props);

validateProps(props, propValidators);

if (process.env.NODE_ENV !== 'production') {
validateProps(props, propValidators);
}
return (
<GridContextProvider privateApiRef={privateApiRef} props={props}>
<GridRoot
Expand Down
15 changes: 6 additions & 9 deletions packages/x-data-grid/src/internals/utils/propValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,22 @@ export const propValidatorsDataGrid: PropValidator<DataGridProcessedProps>[] = [
];

const warnedOnceCache = new Set();
const warnOnce = (message: string) => {
function warnOnce(message: string) {
if (!warnedOnceCache.has(message)) {
console.error(message);
warnedOnceCache.add(message);
}
};
}

export const validateProps = <TProps>(props: TProps, validators: PropValidator<TProps>[]) => {
if (process.env.NODE_ENV === 'production') {
return;
}
export function validateProps<TProps>(props: TProps, validators: PropValidator<TProps>[]) {
validators.forEach((validator) => {
const warning = validator(props);
if (warning) {
warnOnce(warning);
}
});
};
}

export const clearWarningsCache = () => {
export function clearWarningsCache() {
warnedOnceCache.clear();
};
}

0 comments on commit b66c1e5

Please sign in to comment.