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

Make pagination more extendable #4132

Merged
merged 4 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 12 additions & 4 deletions packages/ra-ui-materialui/src/list/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import PropTypes from 'prop-types';
import { TablePagination, Toolbar, useMediaQuery } from '@material-ui/core';
import { useTranslate, sanitizeListRestProps } from 'ra-core';

import PaginationActions from './PaginationActions';
import PaginationLimit from './PaginationLimit';
import DefaultPaginationActions from './PaginationActions';
import DefaultPaginationLimit from './PaginationLimit';

const emptyArray = [];

Expand All @@ -16,6 +16,8 @@ const Pagination = ({
total,
setPage,
setPerPage,
ActionsComponent,
Copy link
Member

Choose a reason for hiding this comment

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

not sure about this name. I think in other places (or in material-ui), component props start with lowercase and don't include the term "component"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right: list, create, show etc, are component (and not elements) yet their props are lowercase and without component at the end.
I submitted the change

limit,
...rest
}) => {
useEffect(() => {
Expand Down Expand Up @@ -64,8 +66,9 @@ const Pagination = ({
);

if (total === 0) {
return loading ? <Toolbar variant="dense" /> : <PaginationLimit />;
return loading ? <Toolbar variant="dense" /> : limit;
}

if (isSmall) {
return (
<TablePagination
Expand All @@ -80,14 +83,15 @@ const Pagination = ({
/>
);
}

return (
<TablePagination
count={total}
rowsPerPage={perPage}
page={page - 1}
onChangePage={handlePageChange}
onChangeRowsPerPage={handlePerPageChange}
ActionsComponent={PaginationActions}
ActionsComponent={ActionsComponent}
component="span"
labelRowsPerPage={translate('ra.navigation.page_rows_per_page')}
labelDisplayedRows={labelDisplayedRows}
Expand All @@ -106,10 +110,14 @@ Pagination.propTypes = {
setPage: PropTypes.func,
setPerPage: PropTypes.func,
total: PropTypes.number,
ActionsComponent: PropTypes.node,
limit: PropTypes.element,
};

Pagination.defaultProps = {
rowsPerPageOptions: [5, 10, 25],
ActionsComponent: DefaultPaginationActions,
limit: <DefaultPaginationLimit />,
};

export default React.memo(Pagination);
27 changes: 20 additions & 7 deletions packages/ra-ui-materialui/src/list/PaginationActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function PaginationActions({
rowsPerPage,
count,
onChangePage,
color,
size,
}) {
const classes = useStyles({ classes: classesOverride });
const translate = useTranslate();
Expand Down Expand Up @@ -102,12 +104,12 @@ function PaginationActions({
</span>
) : (
<Button
size={size}
className="page-number"
color={pageNum === page + 1 ? 'default' : 'primary'}
color={pageNum === page + 1 ? 'default' : color}
key={pageNum}
data-page={pageNum - 1}
onClick={gotoPage}
size="small"
>
{pageNum}
</Button>
Expand All @@ -116,16 +118,20 @@ function PaginationActions({
};

const nbPages = getNbPages();
if (nbPages === 1) return <div className={classes.actions} />;

if (nbPages === 1) {
return <div className={classes.actions} />;
}

return (
<div className={classes.actions}>
{page > 0 && (
<Button
color="primary"
color={color}
size={size}
key="prev"
onClick={prevPage}
className="previous-page"
size="small"
>
<ChevronLeft />
{translate('ra.navigation.prev')}
Expand All @@ -134,11 +140,11 @@ function PaginationActions({
{renderPageNums()}
{page !== nbPages - 1 && (
<Button
color="primary"
color={color}
size={size}
key="next"
onClick={nextPage}
className="next-page"
size="small"
>
{translate('ra.navigation.next')}
<ChevronRight />
Expand All @@ -162,6 +168,13 @@ PaginationActions.propTypes = {
onChangePage: PropTypes.func.isRequired,
page: PropTypes.number.isRequired,
rowsPerPage: PropTypes.number.isRequired,
color: PropTypes.oneOf(['primary', 'secondary']),
size: PropTypes.oneOf(['small', 'medium', 'large']),
};

PaginationActions.defaultProps = {
color: 'primary',
size: 'small',
};

export default React.memo(PaginationActions);