Fix server-side pagination (#1894)

This commit is contained in:
Andrew Kang-G
2020-02-15 05:15:00 +09:00
committed by GitHub
parent 2479b568dc
commit f003ba3e94
2 changed files with 19 additions and 10 deletions

View File

@@ -20,4 +20,4 @@
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
}

View File

@@ -1,3 +1,4 @@
/* eslint-disable react/no-did-update-set-state */
import React, { Component } from 'react'
import classnames from 'classnames'
@@ -71,6 +72,14 @@ export default class ReactTablePagination extends Component {
page: this.props.page,
})
}
/* when the last page from new props is smaller
than the current page in the page box,
the current page needs to be the last page. */
if (this.props.pages !== prevProps.pages && this.props.pages <= this.state.page) {
this.setState({
page: this.props.pages - 1,
})
}
}
getSafePage (page) {
@@ -148,7 +157,7 @@ export default class ReactTablePagination extends Component {
if (!canPrevious) return
this.changePage(page - 1)
}}
disabled={!canPrevious}
disabled={!canPrevious || this.state.page < 1}
>
{this.props.previousText}
</PreviousComponent>
@@ -160,13 +169,13 @@ export default class ReactTablePagination extends Component {
{this.props.ofText} {renderTotalPagesCount(pages)}
</span>
{showPageSizeOptions &&
renderPageSizeOptions({
pageSize,
rowsSelectorText: this.props.rowsSelectorText,
pageSizeOptions,
onPageSizeChange,
rowsText: this.props.rowsText,
})}
renderPageSizeOptions({
pageSize,
rowsSelectorText: this.props.rowsSelectorText,
pageSizeOptions,
onPageSizeChange,
rowsText: this.props.rowsText,
})}
</div>
<div className="-next">
<NextComponent
@@ -174,7 +183,7 @@ export default class ReactTablePagination extends Component {
if (!canNext) return
this.changePage(page + 1)
}}
disabled={!canNext}
disabled={!canNext || this.state.page >= this.props.pages - 1}
>
{this.props.nextText}
</NextComponent>