Skip to content

Commit

Permalink
fix(Pagination): dont reset page to 1 for pageSizes (#4239)
Browse files Browse the repository at this point in the history
  • Loading branch information
stvcisco authored and abbeyhrt committed Oct 22, 2019
1 parent 4a7521e commit 5b42ce2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
18 changes: 18 additions & 0 deletions packages/react/src/components/Pagination/Pagination-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ describe('Pagination', () => {
expect(pager.state().page).toEqual(1);
});

it('should not return to first page on changes to pageSizes where current pageSize is in new list', () => {
const pager = mount(
<Pagination pageSizes={[5, 10]} pageSize={10} totalItems={50} />
);
pager.setState({ page: 2 });
pager.setProps({ pageSizes: [5, 10, 20] });
expect(pager.state().page).toEqual(2);
});

it('should return to first page on changes to pageSizes where current pageSize is not in new list', () => {
const pager = mount(
<Pagination pageSizes={[5, 10]} pageSize={10} totalItems={50} />
);
pager.setState({ page: 2 });
pager.setProps({ pageSizes: [25, 50, 100] });
expect(pager.state().page).toEqual(1);
});

it('should avoid returning to first page unless actual change in pageSizes is detected', () => {
const pager = mount(
<Pagination pageSizes={[5, 10]} totalItems={50} />
Expand Down
11 changes: 5 additions & 6 deletions packages/react/src/components/Pagination/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,16 @@ export default class Pagination extends Component {
pageSize: currentPageSize,
} = state;
const pageSizesChanged = !equals(pageSizes, prevPageSizes);
if (pageSizesChanged && !pageSizes.includes(pageSize)) {
pageSize = pageSizes[0];
}
const pageChanged = page !== prevPage;
const pageSizeChanged = pageSize !== prevPageSize;
return !pageSizesChanged && !pageChanged && !pageSizeChanged
? null
: {
page: pageSizesChanged ? 1 : pageChanged ? page : currentPage,
pageSize: pageSizesChanged
? pageSizes[0]
: pageSizeChanged
? pageSize
: currentPageSize,
page: pageSizeChanged && 1 || pageChanged && page || currentPage,
pageSize: pageSizeChanged ? pageSize : currentPageSize,
prevPageSizes: pageSizes,
prevPage: page,
prevPageSize: pageSize,
Expand Down

0 comments on commit 5b42ce2

Please sign in to comment.