mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2025-10-16 11:55:39 +00:00
parent
258ea43225
commit
85a9ab72af
@ -1,12 +1,39 @@
|
|||||||
|
/* eslint no-param-reassign: 0 */
|
||||||
|
|
||||||
|
const getNormalizedPage = (
|
||||||
|
page,
|
||||||
|
pageStartIndex
|
||||||
|
) => {
|
||||||
|
const offset = Math.abs(1 - pageStartIndex);
|
||||||
|
return page + offset;
|
||||||
|
};
|
||||||
|
|
||||||
|
const endIndex = (
|
||||||
|
page,
|
||||||
|
sizePerPage,
|
||||||
|
pageStartIndex
|
||||||
|
) => (getNormalizedPage(page, pageStartIndex) * sizePerPage) - 1;
|
||||||
|
|
||||||
|
const startIndex = (
|
||||||
|
end,
|
||||||
|
sizePerPage,
|
||||||
|
) => end - (sizePerPage - 1);
|
||||||
|
|
||||||
|
export const alignPage = (store, pageStartIndex, sizePerPage) => {
|
||||||
|
const end = endIndex(store.page, sizePerPage, pageStartIndex);
|
||||||
|
const dataSize = store.data.length;
|
||||||
|
|
||||||
|
if (end - 1 > dataSize) {
|
||||||
|
return pageStartIndex;
|
||||||
|
}
|
||||||
|
return store.page;
|
||||||
|
};
|
||||||
|
|
||||||
export const getByCurrPage = (store, pageStartIndex) => {
|
export const getByCurrPage = (store, pageStartIndex) => {
|
||||||
const dataSize = store.data.length;
|
const dataSize = store.data.length;
|
||||||
if (!dataSize) return [];
|
if (!dataSize) return [];
|
||||||
const getNormalizedPage = () => {
|
const end = endIndex(store.page, store.sizePerPage, pageStartIndex);
|
||||||
const offset = Math.abs(1 - pageStartIndex);
|
const start = startIndex(end, store.sizePerPage);
|
||||||
return store.page + offset;
|
|
||||||
};
|
|
||||||
const end = (getNormalizedPage() * store.sizePerPage) - 1;
|
|
||||||
const start = end - (store.sizePerPage - 1);
|
|
||||||
|
|
||||||
const result = [];
|
const result = [];
|
||||||
for (let i = start; i <= end; i += 1) {
|
for (let i = start; i <= end; i += 1) {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import PropTypes from 'prop-types';
|
|||||||
|
|
||||||
import Const from './const';
|
import Const from './const';
|
||||||
import Pagination from './pagination';
|
import Pagination from './pagination';
|
||||||
import { getByCurrPage } from './page';
|
import { getByCurrPage, alignPage } from './page';
|
||||||
|
|
||||||
export default (Base, {
|
export default (Base, {
|
||||||
remoteResolver
|
remoteResolver
|
||||||
@ -49,17 +49,21 @@ export default (Base, {
|
|||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps) {
|
||||||
let needNewState = false;
|
let needNewState = false;
|
||||||
let { currPage, currSizePerPage } = this.state;
|
let { currPage, currSizePerPage } = this.state;
|
||||||
const { page, sizePerPage, pageStartIndex, onPageChange } = nextProps.pagination.options;
|
const { page, sizePerPage, onPageChange } = nextProps.pagination.options;
|
||||||
|
|
||||||
|
const pageStartIndex = typeof nextProps.pagination.options.pageStartIndex !== 'undefined' ?
|
||||||
|
nextProps.pagination.options.pageStartIndex : Const.PAGE_START_INDEX;
|
||||||
|
|
||||||
if (typeof page !== 'undefined' && currPage !== page) { // user defined page
|
if (typeof page !== 'undefined' && currPage !== page) { // user defined page
|
||||||
currPage = page;
|
currPage = page;
|
||||||
needNewState = true;
|
needNewState = true;
|
||||||
} else if (nextProps.isDataChanged) {
|
} else if (nextProps.isDataChanged) {
|
||||||
|
currPage = alignPage(this.props.store, pageStartIndex, currSizePerPage);
|
||||||
needNewState = true;
|
needNewState = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof currPage === 'undefined') {
|
if (typeof currPage === 'undefined') {
|
||||||
currPage = typeof pageStartIndex !== 'undefined' ? pageStartIndex : Const.PAGE_START_INDEX;
|
currPage = pageStartIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof sizePerPage !== 'undefined') {
|
if (typeof sizePerPage !== 'undefined') {
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import Store from 'react-bootstrap-table-next/src/store';
|
import Store from 'react-bootstrap-table-next/src/store';
|
||||||
import { getByCurrPage } from '../src/page';
|
import { getByCurrPage, alignPage } from '../src/page';
|
||||||
|
|
||||||
describe('Page Functions', () => {
|
describe('Page Functions', () => {
|
||||||
let data;
|
let data;
|
||||||
@ -48,4 +48,40 @@ describe('Page Functions', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('alignPage', () => {
|
||||||
|
const pageStartIndex = 1;
|
||||||
|
const sizePerPage = 10;
|
||||||
|
describe('if the length of store.data is less than the end page index', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
data = [];
|
||||||
|
for (let i = 0; i < 15; i += 1) {
|
||||||
|
data.push({ id: i, name: `test_name${i}` });
|
||||||
|
}
|
||||||
|
store = new Store('id');
|
||||||
|
store.data = data;
|
||||||
|
store.page = 2;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return pageStartIndex argument', () => {
|
||||||
|
expect(alignPage(store, pageStartIndex, sizePerPage)).toEqual(pageStartIndex);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('if the length of store.data is large than the end page index', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
data = [];
|
||||||
|
for (let i = 0; i < 30; i += 1) {
|
||||||
|
data.push({ id: i, name: `test_name${i}` });
|
||||||
|
}
|
||||||
|
store = new Store('id');
|
||||||
|
store.data = data;
|
||||||
|
store.page = 2;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return current page', () => {
|
||||||
|
expect(alignPage(store, pageStartIndex, sizePerPage)).toEqual(store.page);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -176,23 +176,6 @@ describe('Wrapper', () => {
|
|||||||
expect(props.store.page).toEqual(instance.state.currPage);
|
expect(props.store.page).toEqual(instance.state.currPage);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when nextProps.isDataChanged is true, currPage is undefined and options.pageStartIndex exists', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
nextProps.isDataChanged = true;
|
|
||||||
nextProps.pagination.options.pageStartIndex = 0;
|
|
||||||
instance.state.currPage = undefined;
|
|
||||||
instance.componentWillReceiveProps(nextProps);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should setting currPage state correctly', () => {
|
|
||||||
expect(instance.state.currPage).toBe(nextProps.pagination.options.pageStartIndex);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should saving store.page correctly', () => {
|
|
||||||
expect(props.store.page).toEqual(instance.state.currPage);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user