Skip to content

Commit 297c5ad

Browse files
committed
fix pagination bug
1 parent 7016e55 commit 297c5ad

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

packages/react-bootstrap-table2-paginator/src/page-resolver.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ export default ExtendBase =>
1818
return { totalPages, lastPage, dropdownOpen: false };
1919
}
2020

21-
calculateTotalPage(sizePerPage = this.props.currSizePerPage) {
22-
const { dataSize } = this.props;
21+
calculateTotalPage(sizePerPage = this.props.currSizePerPage, dataSize = this.props.dataSize) {
2322
return Math.ceil(dataSize / sizePerPage);
2423
}
2524

packages/react-bootstrap-table2-paginator/src/page.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
export const getByCurrPage = store => (page, sizePerPage, pageStartIndex) => {
2+
const dataSize = store.data.length;
3+
if (!dataSize) return [];
24
const getNormalizedPage = () => {
35
const offset = Math.abs(1 - pageStartIndex);
46
return page + offset;
57
};
68
const end = (getNormalizedPage() * sizePerPage) - 1;
79
const start = end - (sizePerPage - 1);
8-
const dataSize = store.data.length;
910

1011
const result = [];
1112
for (let i = start; i <= end; i += 1) {

packages/react-bootstrap-table2-paginator/src/pagination.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ class Pagination extends pageResolver(Component) {
2020

2121
componentWillReceiveProps(nextProps) {
2222
const { dataSize, currSizePerPage } = nextProps;
23-
2423
if (currSizePerPage !== this.props.currSizePerPage || dataSize !== this.props.dataSize) {
25-
const totalPages = this.calculateTotalPage(currSizePerPage);
24+
const totalPages = this.calculateTotalPage(currSizePerPage, dataSize);
2625
const lastPage = this.calculateLastPage(totalPages);
2726
this.setState({ totalPages, lastPage });
2827
}

packages/react-bootstrap-table2-paginator/test/page.test.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ import { getByCurrPage } from '../src/page';
44
describe('Page Functions', () => {
55
let data;
66
let store;
7+
const params = [
8+
// [page, sizePerPage, pageStartIndex]
9+
[1, 10, 1],
10+
[1, 25, 1],
11+
[1, 30, 1],
12+
[3, 30, 1],
13+
[4, 30, 1],
14+
[10, 10, 1],
15+
[0, 10, 0],
16+
[1, 10, 0],
17+
[9, 10, 0]
18+
];
719

820
describe('getByCurrPage', () => {
921
beforeEach(() => {
@@ -16,23 +28,20 @@ describe('Page Functions', () => {
1628
});
1729

1830
it('should always return correct data', () => {
19-
[
20-
// [page, sizePerPage, pageStartIndex]
21-
[1, 10, 1],
22-
[1, 25, 1],
23-
[1, 30, 1],
24-
[3, 30, 1],
25-
[4, 30, 1],
26-
[10, 10, 1],
27-
[0, 10, 0],
28-
[1, 10, 0],
29-
[9, 10, 0]
30-
].forEach(([page, sizePerPage, pageStartIndex]) => {
31+
params.forEach(([page, sizePerPage, pageStartIndex]) => {
3132
const rows = getByCurrPage(store)(page, sizePerPage, pageStartIndex);
3233
expect(rows).toBeDefined();
3334
expect(Array.isArray(rows)).toBeTruthy();
3435
expect(rows.every(row => !!row)).toBeTruthy();
3536
});
3637
});
38+
39+
it('should return empty array when store.data is empty', () => {
40+
store.data = [];
41+
params.forEach(([page, sizePerPage, pageStartIndex]) => {
42+
const rows = getByCurrPage(store)(page, sizePerPage, pageStartIndex);
43+
expect(rows).toHaveLength(0);
44+
});
45+
});
3746
});
3847
});

packages/react-bootstrap-table2-paginator/test/pagination.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,13 @@ describe('Pagination', () => {
143143
it('should setting correct state.totalPages', () => {
144144
instance.componentWillReceiveProps(nextProps);
145145
expect(instance.state.totalPages).toEqual(
146-
instance.calculateTotalPage(nextProps.currSizePerPage));
146+
instance.calculateTotalPage(nextProps.currSizePerPage, nextProps.dataSize));
147147
});
148148

149149
it('should setting correct state.lastPage', () => {
150150
instance.componentWillReceiveProps(nextProps);
151-
const totalPages = instance.calculateTotalPage(nextProps.currSizePerPage);
151+
const totalPages = instance.calculateTotalPage(
152+
nextProps.currSizePerPage, nextProps.dataSize);
152153
expect(instance.state.lastPage).toEqual(
153154
instance.calculateLastPage(totalPages));
154155
});

0 commit comments

Comments
 (0)