Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

Commit

Permalink
fix: Pagination component's range and page text for no items, resolves
Browse files Browse the repository at this point in the history
…#553
  • Loading branch information
Nathan Rosa authored and Nathan Rosa committed May 9, 2018
1 parent 7065843 commit 4a0f1f0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/components/Pagination/Pagination-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Pagination pageSizes={[5, 10]} totalItems={0} />);
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);
Expand Down
12 changes: 6 additions & 6 deletions src/components/Pagination/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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;

Expand All @@ -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
)}
Expand All @@ -163,7 +165,7 @@ export default class Pagination extends Component {
<span className="bx--pagination__text">
{pagesUnknown
? pageText(statePage)
: pageRangeText(statePage, Math.ceil(totalItems / statePageSize))}
: pageRangeText(statePage, totalPages)}
</span>
<button
className="bx--pagination__button bx--pagination__button--backward"
Expand Down Expand Up @@ -191,9 +193,7 @@ export default class Pagination extends Component {
className="bx--pagination__button bx--pagination__button--forward"
onClick={this.incrementPage}
disabled={
this.props.disabled ||
statePage === Math.ceil(totalItems / statePageSize) ||
isLastPage
this.props.disabled || statePage === totalPages || isLastPage
}>
<Icon
className="bx--pagination__button-icon"
Expand Down
7 changes: 7 additions & 0 deletions src/components/PaginationV2/PaginationV2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Pagination pageSizes={[5, 10]} totalItems={0} />);
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);
Expand Down
10 changes: 4 additions & 6 deletions src/components/PaginationV2/PaginationV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default class PaginationV2 extends Component {
}
);
const inputId = id || this.uniqueId;
const totalPages = Math.ceil(totalItems / statePageSize);
const totalPages = Math.max(Math.ceil(totalItems / statePageSize), 1);
const selectItems = this.renderSelectItems(totalPages);

return (
Expand Down Expand Up @@ -180,7 +180,7 @@ export default class PaginationV2 extends Component {
statePage * statePageSize
)
: itemRangeText(
statePageSize * (statePage - 1) + 1,
Math.min(statePageSize * (statePage - 1) + 1, totalItems),
Math.min(statePage * statePageSize, totalItems),
totalItems
)}
Expand All @@ -190,7 +190,7 @@ export default class PaginationV2 extends Component {
<span className="bx--pagination__text">
{pagesUnknown
? pageText(statePage)
: pageRangeText(statePage, Math.ceil(totalItems / statePageSize))}
: pageRangeText(statePage, totalPages)}
</span>
<button
className={backButtonClasses}
Expand All @@ -217,9 +217,7 @@ export default class PaginationV2 extends Component {
className="bx--pagination__button bx--pagination__button--forward"
onClick={this.incrementPage}
disabled={
this.props.disabled ||
statePage === Math.ceil(totalItems / statePageSize) ||
isLastPage
this.props.disabled || statePage === totalPages || isLastPage
}>
<Icon
className="bx--pagination__button-icon"
Expand Down

0 comments on commit 4a0f1f0

Please sign in to comment.