fix pagination bug

This commit is contained in:
AllenFang 2017-12-13 21:47:29 +08:00
parent 7016e55472
commit 297c5ad438
5 changed files with 28 additions and 19 deletions

View File

@ -18,8 +18,7 @@ export default ExtendBase =>
return { totalPages, lastPage, dropdownOpen: false };
}
calculateTotalPage(sizePerPage = this.props.currSizePerPage) {
const { dataSize } = this.props;
calculateTotalPage(sizePerPage = this.props.currSizePerPage, dataSize = this.props.dataSize) {
return Math.ceil(dataSize / sizePerPage);
}

View File

@ -1,11 +1,12 @@
export const getByCurrPage = store => (page, sizePerPage, pageStartIndex) => {
const dataSize = store.data.length;
if (!dataSize) return [];
const getNormalizedPage = () => {
const offset = Math.abs(1 - pageStartIndex);
return page + offset;
};
const end = (getNormalizedPage() * sizePerPage) - 1;
const start = end - (sizePerPage - 1);
const dataSize = store.data.length;
const result = [];
for (let i = start; i <= end; i += 1) {

View File

@ -20,9 +20,8 @@ class Pagination extends pageResolver(Component) {
componentWillReceiveProps(nextProps) {
const { dataSize, currSizePerPage } = nextProps;
if (currSizePerPage !== this.props.currSizePerPage || dataSize !== this.props.dataSize) {
const totalPages = this.calculateTotalPage(currSizePerPage);
const totalPages = this.calculateTotalPage(currSizePerPage, dataSize);
const lastPage = this.calculateLastPage(totalPages);
this.setState({ totalPages, lastPage });
}

View File

@ -4,6 +4,18 @@ import { getByCurrPage } from '../src/page';
describe('Page Functions', () => {
let data;
let store;
const params = [
// [page, sizePerPage, pageStartIndex]
[1, 10, 1],
[1, 25, 1],
[1, 30, 1],
[3, 30, 1],
[4, 30, 1],
[10, 10, 1],
[0, 10, 0],
[1, 10, 0],
[9, 10, 0]
];
describe('getByCurrPage', () => {
beforeEach(() => {
@ -16,23 +28,20 @@ describe('Page Functions', () => {
});
it('should always return correct data', () => {
[
// [page, sizePerPage, pageStartIndex]
[1, 10, 1],
[1, 25, 1],
[1, 30, 1],
[3, 30, 1],
[4, 30, 1],
[10, 10, 1],
[0, 10, 0],
[1, 10, 0],
[9, 10, 0]
].forEach(([page, sizePerPage, pageStartIndex]) => {
params.forEach(([page, sizePerPage, pageStartIndex]) => {
const rows = getByCurrPage(store)(page, sizePerPage, pageStartIndex);
expect(rows).toBeDefined();
expect(Array.isArray(rows)).toBeTruthy();
expect(rows.every(row => !!row)).toBeTruthy();
});
});
it('should return empty array when store.data is empty', () => {
store.data = [];
params.forEach(([page, sizePerPage, pageStartIndex]) => {
const rows = getByCurrPage(store)(page, sizePerPage, pageStartIndex);
expect(rows).toHaveLength(0);
});
});
});
});

View File

@ -143,12 +143,13 @@ describe('Pagination', () => {
it('should setting correct state.totalPages', () => {
instance.componentWillReceiveProps(nextProps);
expect(instance.state.totalPages).toEqual(
instance.calculateTotalPage(nextProps.currSizePerPage));
instance.calculateTotalPage(nextProps.currSizePerPage, nextProps.dataSize));
});
it('should setting correct state.lastPage', () => {
instance.componentWillReceiveProps(nextProps);
const totalPages = instance.calculateTotalPage(nextProps.currSizePerPage);
const totalPages = instance.calculateTotalPage(
nextProps.currSizePerPage, nextProps.dataSize);
expect(instance.state.lastPage).toEqual(
instance.calculateLastPage(totalPages));
});