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

[RFR] Convert PaginationActions to hooks #3598

Merged
merged 7 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
160 changes: 73 additions & 87 deletions packages/ra-ui-materialui/src/list/PaginationActions.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import React, { Component } from 'react';
import React from 'react';
import PropTypes from 'prop-types';
import pure from 'recompose/pure';
import Button from '@material-ui/core/Button';
import { withStyles, createStyles } from '@material-ui/core/styles';
import { makeStyles } from '@material-ui/core/styles';
import ChevronLeft from '@material-ui/icons/ChevronLeft';
import ChevronRight from '@material-ui/icons/ChevronRight';
import compose from 'recompose/compose';
import { translate } from 'ra-core';
import { useTranslate } from 'react-admin';
Copy link
Contributor

Choose a reason for hiding this comment

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

should be ra-core


const styles = theme =>
createStyles({
actions: {
flexShrink: 0,
color: theme.palette.text.secondary,
marginLeft: 20,
},
hellip: { padding: '1.2em' },
});
const useStyles = makeStyles(theme => ({
actions: {
flexShrink: 0,
color: theme.palette.text.secondary,
marginLeft: 20,
},
hellip: { padding: '1.2em' },
}));

export class PaginationActions extends Component {
function PaginationActions({
classes: classesOverride,
page,
rowsPerPage,
count,
onChangePage,
}) {
const classes = useStyles({ classes: classesOverride });
const translate = useTranslate();
/**
* Warning: material-ui's page is 0-based
*/
range() {
const { page, rowsPerPage, count } = this.props;
const range = () => {
const nbPages = Math.ceil(count / rowsPerPage) || 1;
if (isNaN(page) || nbPages === 1) {
return [];
Expand Down Expand Up @@ -57,101 +61,88 @@ export class PaginationActions extends Component {
}

return input;
}
};

getNbPages = () =>
Math.ceil(this.props.count / this.props.rowsPerPage) || 1;
const getNbPages = () => Math.ceil(count / rowsPerPage) || 1;

prevPage = event => {
if (this.props.page === 0) {
throw new Error(
this.props.translate('ra.navigation.page_out_from_begin')
);
const prevPage = event => {
if (page === 0) {
throw new Error(translate('ra.navigation.page_out_from_begin'));
}
this.props.onChangePage(event, this.props.page - 1);
onChangePage(event, page - 1);
};

nextPage = event => {
if (this.props.page > this.getNbPages() - 1) {
throw new Error(
this.props.translate('ra.navigation.page_out_from_end')
);
const nextPage = event => {
if (page > getNbPages() - 1) {
throw new Error(translate('ra.navigation.page_out_from_end'));
}
this.props.onChangePage(event, this.props.page + 1);
onChangePage(event, page + 1);
};

gotoPage = event => {
const gotoPage = event => {
const page = parseInt(event.currentTarget.dataset.page, 10);
if (page < 0 || page > this.getNbPages() - 1) {
if (page < 0 || page > getNbPages() - 1) {
throw new Error(
this.props.translate('ra.navigation.page_out_of_boundaries', {
translate('ra.navigation.page_out_of_boundaries', {
page: page + 1,
})
);
}
this.props.onChangePage(event, page);
onChangePage(event, page);
};

renderPageNums() {
const { classes = {} } = this.props;

return this.range().map((pageNum, index) =>
const renderPageNums = () => {
return range().map((pageNum, index) =>
pageNum === '.' ? (
<span key={`hyphen_${index}`} className={classes.hellip}>
&hellip;
</span>
) : (
<Button
className="page-number"
color={
pageNum === this.props.page + 1 ? 'default' : 'primary'
}
color={pageNum === page + 1 ? 'default' : 'primary'}
key={pageNum}
data-page={pageNum - 1}
onClick={this.gotoPage}
onClick={gotoPage}
size="small"
>
{pageNum}
</Button>
)
);
}

render() {
const { classes = {}, page, translate } = this.props;
};

const nbPages = this.getNbPages();
if (nbPages === 1) return <div className={classes.actions} />;
return (
<div className={classes.actions}>
{page > 0 && (
<Button
color="primary"
key="prev"
onClick={this.prevPage}
className="previous-page"
size="small"
>
<ChevronLeft />
{translate('ra.navigation.prev')}
</Button>
)}
{this.renderPageNums()}
{page !== nbPages - 1 && (
<Button
color="primary"
key="next"
onClick={this.nextPage}
className="next-page"
size="small"
>
{translate('ra.navigation.next')}
<ChevronRight />
</Button>
)}
</div>
);
}
const nbPages = getNbPages();
if (nbPages === 1) return <div className={classes.actions} />;
return (
<div className={classes.actions}>
{page > 0 && (
<Button
color="primary"
key="prev"
onClick={prevPage}
className="previous-page"
size="small"
>
<ChevronLeft />
{translate('ra.navigation.prev')}
</Button>
)}
{renderPageNums()}
{page !== nbPages - 1 && (
<Button
color="primary"
key="next"
onClick={nextPage}
className="next-page"
size="small"
>
{translate('ra.navigation.next')}
<ChevronRight />
</Button>
)}
</div>
);
}

/**
Expand All @@ -163,16 +154,11 @@ export class PaginationActions extends Component {
PaginationActions.propTypes = {
backIconButtonProps: PropTypes.object,
count: PropTypes.number.isRequired,
classes: PropTypes.object,
nextIconButtonProps: PropTypes.object,
onChangePage: PropTypes.func.isRequired,
page: PropTypes.number.isRequired,
rowsPerPage: PropTypes.number.isRequired,
};

const enhance = compose(
pure,
translate,
withStyles(styles)
);

export default enhance(PaginationActions);
export default React.memo(PaginationActions);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { shallow } from 'enzyme';

import { PaginationActions } from './PaginationActions';
import PaginationActions from './PaginationActions';

describe('<PaginationActions />', () => {
it('should not render any actions when no pagination is necessary', () => {
Expand Down