Skip to content

Commit

Permalink
Rework using props
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Jul 27, 2021
1 parent 26f483b commit 76d0718
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 113 deletions.
41 changes: 23 additions & 18 deletions packages/grid/_modules_/grid/components/GridPagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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';
import { DEFAULT_PAGE_SIZE } from '../hooks/features/pagination/gridPaginationState';

const defaultTheme = createTheme();
// Used to hide the Rows per page selector on small devices
Expand Down Expand Up @@ -45,6 +47,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 @@ -82,23 +85,21 @@ export const GridPagination = React.forwardRef<
};
};

const hasPageSizeInRowsPerPageOptions = options.rowsPerPageOptions?.includes(
paginationState.pageSize,
);

if (
process.env.NODE_ENV !== 'production' &&
!warnedOnceMissingPageSizeInRowsPerPageOptions &&
options.rowsPerPageOptions &&
!hasPageSizeInRowsPerPageOptions
) {
console.warn(
[
`Material-UI: The current pageSize (${paginationState.pageSize}) is not preset in the rowsPerPageOptions.`,
`Add it to show the pagination select.`,
].join('\n'),
);
warnedOnceMissingPageSizeInRowsPerPageOptions = true;
if (process.env.NODE_ENV !== 'production') {
if (
!warnedOnceMissingPageSizeInRowsPerPageOptions &&
!rootProps.rowsPerPageOptions!.includes(rootProps.pageSize ?? DEFAULT_PAGE_SIZE)
) {
console.warn(
[
`Material-UI: The page size \`${
rootProps.pageSize ?? DEFAULT_PAGE_SIZE
}\` is not preset in the \`rowsPerPageOptions\``,
`Add it to show the pagination select.`,
].join('\n'),
);
warnedOnceMissingPageSizeInRowsPerPageOptions = true;
}
}

return (
Expand All @@ -114,7 +115,11 @@ export const GridPagination = React.forwardRef<
component="div"
count={paginationState.rowCount}
page={paginationState.page <= lastPage ? paginationState.page : lastPage}
rowsPerPageOptions={hasPageSizeInRowsPerPageOptions ? options.rowsPerPageOptions : []}
rowsPerPageOptions={
options.rowsPerPageOptions?.includes(paginationState.pageSize)
? options.rowsPerPageOptions
: []
}
rowsPerPage={paginationState.pageSize}
{...apiRef!.current.getLocaleText('MuiTablePagination')}
{...getPaginationChangeHandlers()}
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,12 @@ export interface GridPaginationState {
pageCount: number;
rowCount: number;
}

export const DEFAULT_PAGE_SIZE = 100;

export const getInitialPaginationState = (): GridPaginationState => ({
page: 0,
pageCount: 0,
rowCount: 0,
pageSize: DEFAULT_PAGE_SIZE,
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,73 @@
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 = {
...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

0 comments on commit 76d0718

Please sign in to comment.