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

[DataGrid] Warn when pageSize is not present in rowsPerPageOptions #2014

Merged
merged 16 commits into from
Jul 29, 2021
Merged
25 changes: 23 additions & 2 deletions packages/grid/_modules_/grid/components/GridPagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { gridPaginationSelector } from '../hooks/features/pagination/gridPaginat
import { optionsSelector } from '../hooks/utils/optionsSelector';
import { useGridApiContext } from '../hooks/root/useGridApiContext';
import { getMuiVersion, createTheme } from '../utils';
import { GridRootPropsContext } from '../context/GridRootPropsContext';

const defaultTheme = createTheme();
// Used to hide the Rows per page selector on small devices
Expand Down Expand Up @@ -43,6 +44,7 @@ export const GridPagination = React.forwardRef<
>(function GridPagination(props, ref) {
const classes = useStyles();
const apiRef = useGridApiContext();
const rootProps = React.useContext(GridRootPropsContext)!;
const paginationState = useGridSelector(apiRef, gridPaginationSelector);

const lastPage = React.useMemo(
Expand Down Expand Up @@ -80,6 +82,26 @@ export const GridPagination = React.forwardRef<
};
};

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
const warnedOnceMissingPageSizeInRowsPerPageOptions = React.useRef(false);
if (
!warnedOnceMissingPageSizeInRowsPerPageOptions.current &&
!rootProps.autoPageSize &&
!rootProps.rowsPerPageOptions!.includes(rootProps.pageSize ?? paginationState.pageSize)
) {
console.warn(
[
`Material-UI: The page size \`${
rootProps.pageSize ?? paginationState.pageSize
}\` is not preset in the \`rowsPerPageOptions\``,
`Add it to show the pagination select.`,
].join('\n'),
);
warnedOnceMissingPageSizeInRowsPerPageOptions.current = true;
}
}

return (
// @ts-ignore TODO remove once upgraded v4 support is dropped
<TablePagination
Expand All @@ -94,8 +116,7 @@ export const GridPagination = React.forwardRef<
count={paginationState.rowCount}
page={paginationState.page <= lastPage ? paginationState.page : lastPage}
rowsPerPageOptions={
options.rowsPerPageOptions &&
options.rowsPerPageOptions.indexOf(paginationState.pageSize) > -1
options.rowsPerPageOptions?.includes(paginationState.pageSize)
? options.rowsPerPageOptions
: []
}
Expand Down
9 changes: 2 additions & 7 deletions packages/grid/_modules_/grid/hooks/features/core/gridState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
getInitialGridRenderingState,
InternalRenderingState,
} from '../virtualization/renderingState';
import { GridPaginationState } from '../pagination/gridPaginationState';
import { getInitialPaginationState, GridPaginationState } from '../pagination/gridPaginationState';

export interface GridState {
rows: InternalGridRowsState;
Expand Down Expand Up @@ -60,12 +60,7 @@ export interface GridState {
export const getInitialGridState = (): GridState => ({
rows: getInitialGridRowState(),
editRows: {},
pagination: {
page: 0,
pageCount: 0,
rowCount: 0,
pageSize: 100,
},
pagination: getInitialPaginationState(),
options: DEFAULT_GRID_OPTIONS,
isScrolling: false,
columns: getInitialGridColumnsState(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ export interface GridPaginationState {
pageCount: number;
rowCount: number;
}

export const getInitialPaginationState = (): GridPaginationState => ({
page: 0,
pageCount: 0,
rowCount: 0,
pageSize: 100,
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,72 @@
import * as React from 'react';
import MUICheckbox from '@material-ui/core/Checkbox';
import { GridApiRef } from '../../models/api/gridApiRef';
import { GridApiRefComponentsProperty } from '../../models/api/gridComponentsApi';
import { DEFAULT_GRID_SLOTS_COMPONENTS, GridSlotsComponent } from '../../models/gridSlotsComponent';
import { GridSlotsComponent } from '../../models/gridSlotsComponent';
import { GridSlotsComponentsProps } from '../../models/gridSlotsComponentsProps';
import { GridIconSlotsComponent } from '../../models';
import {
GridArrowDownwardIcon,
GridArrowUpwardIcon,
GridCheckIcon,
GridCloseIcon,
GridColumnIcon,
GridColumnMenu,
GridColumnsPanel,
GridFilterAltIcon,
GridFilterListIcon,
GridFilterPanel,
GridFooter,
GridHeader,
GridLoadingOverlay,
GridNoRowsOverlay,
GridPagination,
GridPanel,
GridPreferencesPanel,
GridSaveAltIcon,
GridSeparatorIcon,
GridTableRowsIcon,
GridTripleDotsVerticalIcon,
GridViewHeadlineIcon,
GridViewStreamIcon,
} from '../../components';
import { GridColumnUnsortedIcon } from '../../components/columnHeaders/GridColumnUnsortedIcon';
import { ErrorOverlay } from '../../components/ErrorOverlay';
import { GridNoResultsOverlay } from '../../components/GridNoResultsOverlay';

const DEFAULT_GRID_SLOTS_ICONS: GridIconSlotsComponent = {
BooleanCellTrueIcon: GridCheckIcon,
BooleanCellFalseIcon: GridCloseIcon,
OpenFilterButtonIcon: GridFilterListIcon,
ColumnFilteredIcon: GridFilterAltIcon,
ColumnSelectorIcon: GridColumnIcon,
ColumnMenuIcon: GridTripleDotsVerticalIcon,
ColumnUnsortedIcon: GridColumnUnsortedIcon,
ColumnSortedAscendingIcon: GridArrowUpwardIcon,
ColumnSortedDescendingIcon: GridArrowDownwardIcon,
ColumnResizeIcon: GridSeparatorIcon,
DensityCompactIcon: GridViewHeadlineIcon,
DensityStandardIcon: GridTableRowsIcon,
DensityComfortableIcon: GridViewStreamIcon,
ExportIcon: GridSaveAltIcon,
};

export const DEFAULT_GRID_SLOTS_COMPONENTS: GridApiRefComponentsProperty = {
Copy link
Member Author

Choose a reason for hiding this comment

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

...DEFAULT_GRID_SLOTS_ICONS,
Checkbox: MUICheckbox,
ColumnMenu: GridColumnMenu,
ColumnsPanel: GridColumnsPanel,
ErrorOverlay,
FilterPanel: GridFilterPanel,
Footer: GridFooter,
Header: GridHeader,
PreferencesPanel: GridPreferencesPanel,
LoadingOverlay: GridLoadingOverlay,
NoResultsOverlay: GridNoResultsOverlay,
NoRowsOverlay: GridNoRowsOverlay,
Pagination: GridPagination,
Panel: GridPanel,
};

export const useGridComponents = (
apiRef: GridApiRef,
Expand Down
65 changes: 1 addition & 64 deletions packages/grid/_modules_/grid/models/gridSlotsComponent.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
import * as React from 'react';
import MUICheckbox from '@material-ui/core/Checkbox';
import { ErrorOverlay } from '../components/ErrorOverlay';
import { GridFooter } from '../components/GridFooter';
import { GridHeader } from '../components/GridHeader';
import { GridPreferencesPanel } from '../components/panel/GridPreferencesPanel';
import {
GridArrowDownwardIcon,
GridArrowUpwardIcon,
GridColumnIcon,
GridFilterAltIcon,
GridFilterListIcon,
GridSeparatorIcon,
GridTableRowsIcon,
GridTripleDotsVerticalIcon,
GridViewHeadlineIcon,
GridViewStreamIcon,
GridSaveAltIcon,
GridCloseIcon,
GridCheckIcon,
} from '../components/icons/index';
import { GridLoadingOverlay } from '../components/GridLoadingOverlay';
import { GridColumnMenu } from '../components/menu/columnMenu/GridColumnMenu';
import { GridNoRowsOverlay } from '../components/GridNoRowsOverlay';
import { GridNoResultsOverlay } from '../components/GridNoResultsOverlay';
import { GridPagination } from '../components/GridPagination';
import { GridColumnsPanel } from '../components/panel/GridColumnsPanel';
import { GridFilterPanel } from '../components/panel/filterPanel/GridFilterPanel';
import { GridPanel } from '../components/panel/GridPanel';
import { GridColumnUnsortedIcon } from '../components/columnHeaders/GridColumnUnsortedIcon';
import { GridApiRefComponentsProperty } from './api/gridComponentsApi';

import { GridIconSlotsComponent } from './gridIconSlotsComponent';

/**
Expand Down Expand Up @@ -94,37 +65,3 @@ export interface GridSlotsComponent extends GridIconSlotsComponent {
*/
Panel?: React.JSXElementConstructor<any>;
}

export const DEFAULT_GRID_SLOTS_ICONS: GridIconSlotsComponent = {
BooleanCellTrueIcon: GridCheckIcon,
BooleanCellFalseIcon: GridCloseIcon,
OpenFilterButtonIcon: GridFilterListIcon,
ColumnFilteredIcon: GridFilterAltIcon,
ColumnSelectorIcon: GridColumnIcon,
ColumnMenuIcon: GridTripleDotsVerticalIcon,
ColumnUnsortedIcon: GridColumnUnsortedIcon,
ColumnSortedAscendingIcon: GridArrowUpwardIcon,
ColumnSortedDescendingIcon: GridArrowDownwardIcon,
ColumnResizeIcon: GridSeparatorIcon,
DensityCompactIcon: GridViewHeadlineIcon,
DensityStandardIcon: GridTableRowsIcon,
DensityComfortableIcon: GridViewStreamIcon,
ExportIcon: GridSaveAltIcon,
};

export const DEFAULT_GRID_SLOTS_COMPONENTS: GridApiRefComponentsProperty = {
...DEFAULT_GRID_SLOTS_ICONS,
Checkbox: MUICheckbox,
ColumnMenu: GridColumnMenu,
ColumnsPanel: GridColumnsPanel,
ErrorOverlay,
FilterPanel: GridFilterPanel,
Footer: GridFooter,
Header: GridHeader,
PreferencesPanel: GridPreferencesPanel,
LoadingOverlay: GridLoadingOverlay,
NoResultsOverlay: GridNoResultsOverlay,
NoRowsOverlay: GridNoRowsOverlay,
Pagination: GridPagination,
Panel: GridPanel,
};
Loading