From 4a0f1f05c34c026c26d869ab0b7626d0907cf95e Mon Sep 17 00:00:00 2001 From: Nathan Rosa Date: Wed, 9 May 2018 17:11:13 -0500 Subject: [PATCH] fix: Pagination component's range and page text for no items, resolves #553 --- src/components/Pagination/Pagination-test.js | 7 +++++++ src/components/Pagination/Pagination.js | 12 ++++++------ src/components/PaginationV2/PaginationV2-test.js | 7 +++++++ src/components/PaginationV2/PaginationV2.js | 10 ++++------ 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/components/Pagination/Pagination-test.js b/src/components/Pagination/Pagination-test.js index 74f82fbe5c..de6cdedaff 100644 --- a/src/components/Pagination/Pagination-test.js +++ b/src/components/Pagination/Pagination-test.js @@ -171,6 +171,13 @@ describe('Pagination', () => { expect(label.text()).toBe('1 of 10 pages'); }); + it('should render ranges and pages for no items', () => { + const pager = mount(); + const labels = pager.find('.bx--pagination__text'); + expect(labels.at(1).text()).toBe('0-0 of 0 items'); + expect(labels.at(2).text()).toBe('1 of 1 pages'); + }); + it('should have two buttons for navigation', () => { const buttons = right.find('.bx--pagination__button'); expect(buttons.length).toBe(2); diff --git a/src/components/Pagination/Pagination.js b/src/components/Pagination/Pagination.js index aae7b74c65..3215c36e52 100644 --- a/src/components/Pagination/Pagination.js +++ b/src/components/Pagination/Pagination.js @@ -83,7 +83,8 @@ export default class Pagination extends Component { const page = Number(evt.target.value); if ( page > 0 && - page <= Math.ceil(this.props.totalItems / this.state.pageSize) + page <= + Math.max(Math.ceil(this.props.totalItems / this.state.pageSize), 1) ) { this.setState({ page }); this.props.onChange({ page, pageSize: this.state.pageSize }); @@ -127,6 +128,7 @@ export default class Pagination extends Component { const statePage = this.state.page; const statePageSize = this.state.pageSize; + const totalPages = Math.max(Math.ceil(totalItems / statePageSize), 1); const classNames = classnames('bx--pagination', className); const inputId = id || this.uniqueId; @@ -153,7 +155,7 @@ export default class Pagination extends Component { statePage * statePageSize ) : itemRangeText( - statePageSize * (statePage - 1) + 1, + Math.min(statePageSize * (statePage - 1) + 1, totalItems), Math.min(statePage * statePageSize, totalItems), totalItems )} @@ -163,7 +165,7 @@ export default class Pagination extends Component { {pagesUnknown ? pageText(statePage) - : pageRangeText(statePage, Math.ceil(totalItems / statePageSize))} + : pageRangeText(statePage, totalPages)}