Skip to content

Commit 4d1311b

Browse files
committed
Merge branch 'master' of github.com:react-component/pagination
2 parents 2f0d5e1 + 028acca commit 4d1311b

File tree

4 files changed

+72
-56
lines changed

4 files changed

+72
-56
lines changed

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ React Pagination Component.
66
[![NPM version][npm-image]][npm-url]
77
[![build status][travis-image]][travis-url]
88
[![Test coverage][coveralls-image]][coveralls-url]
9-
[![gemnasium deps][gemnasium-image]][gemnasium-url]
10-
[![node version][node-image]][node-url]
9+
[![Dependencies](https://img.shields.io/david/react-component/pagination.svg?style=flat-square)](https://david-dm.org/react-component/pagination)
10+
[![DevDependencies](https://img.shields.io/david/dev/react-component/pagination.svg?style=flat-square)](https://david-dm.org/react-component/pagination?type=dev)
1111
[![npm download][download-image]][download-url]
1212

1313
[npm-image]: http://img.shields.io/npm/v/rc-pagination.svg?style=flat-square
@@ -16,10 +16,6 @@ React Pagination Component.
1616
[travis-url]: https://travis-ci.org/react-component/pagination
1717
[coveralls-image]: https://img.shields.io/coveralls/react-component/pagination.svg?style=flat-square
1818
[coveralls-url]: https://coveralls.io/r/react-component/pagination?branch=master
19-
[gemnasium-image]: http://img.shields.io/gemnasium/react-component/pagination.svg?style=flat-square
20-
[gemnasium-url]: https://gemnasium.com/react-component/pagination
21-
[node-image]: https://img.shields.io/badge/node.js-%3E=_0.10-green.svg?style=flat-square
22-
[node-url]: http://nodejs.org/download/
2319
[download-image]: https://img.shields.io/npm/dm/rc-pagination.svg?style=flat-square
2420
[download-url]: https://npmjs.org/package/rc-pagination
2521

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@
5757
"rc-select": "6.x",
5858
"rc-test": "^6.0.1",
5959
"rc-tools": "6.x",
60-
"react": "^16.0.0",
61-
"react-dom": "^16.0.0"
60+
"react": "^16.5.2",
61+
"react-dom": "^16.5.2"
6262
},
6363
"pre-commit": [
6464
"lint"
6565
],
6666
"dependencies": {
6767
"babel-runtime": "6.x",
68-
"prop-types": "^15.5.7"
68+
"prop-types": "^15.5.7",
69+
"react-lifecycles-compat": "^3.0.4"
6970
}
7071
}

src/Pagination.jsx

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Pager from './Pager';
44
import Options from './Options';
55
import KEYCODE from './KeyCode';
66
import LOCALE from './locale/zh_CN';
7+
import { polyfill } from 'react-lifecycles-compat';
78

89
function noop() {
910
}
@@ -18,7 +19,15 @@ function defaultItemRender(page, type, element) {
1819
return element;
1920
}
2021

21-
export default class Pagination extends React.Component {
22+
function calculatePage(p, state, props) {
23+
let pageSize = p;
24+
if (typeof pageSize === 'undefined') {
25+
pageSize = state.pageSize;
26+
}
27+
return Math.floor((props.total - 1) / pageSize) + 1;
28+
}
29+
30+
class Pagination extends React.Component {
2231
static propTypes = {
2332
prefixCls: PropTypes.string,
2433
current: PropTypes.number,
@@ -93,28 +102,6 @@ export default class Pagination extends React.Component {
93102
};
94103
}
95104

96-
componentWillReceiveProps(nextProps) {
97-
if ('current' in nextProps) {
98-
this.setState({
99-
current: nextProps.current,
100-
currentInputValue: nextProps.current,
101-
});
102-
}
103-
104-
if ('pageSize' in nextProps) {
105-
const newState = {};
106-
let current = this.state.current;
107-
const newCurrent = this.calculatePage(nextProps.pageSize);
108-
current = current > newCurrent ? newCurrent : current;
109-
if (!('current' in nextProps)) {
110-
newState.current = current;
111-
newState.currentInputValue = current;
112-
}
113-
newState.pageSize = nextProps.pageSize;
114-
this.setState(newState);
115-
}
116-
}
117-
118105
componentDidUpdate(prevProps, prevState) {
119106
// When current page change, fix focused style of prev item
120107
// A hacky solution of https://github.com/ant-design/ant-design/issues/8948
@@ -129,12 +116,40 @@ export default class Pagination extends React.Component {
129116
}
130117
}
131118

132-
getJumpPrevPage() {
119+
static getDerivedStateFromProps(props, state) {
120+
let newState = {};
121+
122+
if ('current' in props) {
123+
newState = {
124+
current: props.current,
125+
currentInputValue: props.current,
126+
};
127+
}
128+
129+
if ('pageSize' in props) {
130+
let current = state.current;
131+
const newCurrent = calculatePage(props.pageSize, state, props);
132+
current = current > newCurrent ? newCurrent : current;
133+
134+
if (!('current' in props)) {
135+
newState.current = current;
136+
newState.currentInputValue = current;
137+
}
138+
newState.pageSize = props.pageSize;
139+
}
140+
141+
return newState;
142+
}
143+
144+
getJumpPrevPage = () => {
133145
return Math.max(1, this.state.current - (this.props.showLessItems ? 3 : 5));
134146
}
135147

136-
getJumpNextPage() {
137-
return Math.min(this.calculatePage(), this.state.current + (this.props.showLessItems ? 3 : 5));
148+
getJumpNextPage = () => {
149+
return Math.min(
150+
calculatePage(undefined, this.state, this.props),
151+
this.state.current + (this.props.showLessItems ? 3 : 5)
152+
);
138153
}
139154

140155
/**
@@ -156,14 +171,6 @@ export default class Pagination extends React.Component {
156171
this.paginationNode = node;
157172
}
158173

159-
calculatePage = (p) => {
160-
let pageSize = p;
161-
if (typeof pageSize === 'undefined') {
162-
pageSize = this.state.pageSize;
163-
}
164-
return Math.floor((this.props.total - 1) / pageSize) + 1;
165-
}
166-
167174
isValid = (page) => {
168175
return isInteger(page) && page >= 1 && page !== this.state.current;
169176
}
@@ -204,7 +211,7 @@ export default class Pagination extends React.Component {
204211

205212
changePageSize = (size) => {
206213
let current = this.state.current;
207-
const newCurrent = this.calculatePage(size);
214+
const newCurrent = calculatePage(size, this.state, this.props);
208215
current = current > newCurrent ? newCurrent : current;
209216
// fix the issue:
210217
// Once 'total' is 0, 'current' in 'onShowSizeChange' is 0, which is not correct.
@@ -231,8 +238,9 @@ export default class Pagination extends React.Component {
231238
handleChange = (p) => {
232239
let page = p;
233240
if (this.isValid(page)) {
234-
if (page > this.calculatePage()) {
235-
page = this.calculatePage();
241+
const currentPage = calculatePage(undefined, this.state, this.props);
242+
if (page > currentPage) {
243+
page = currentPage;
236244
}
237245

238246
if (!('current' in this.props)) {
@@ -263,14 +271,6 @@ export default class Pagination extends React.Component {
263271
}
264272
}
265273

266-
getJumpPrevPage() {
267-
return Math.max(1, this.state.current - (this.props.showLessItems ? 3 : 5));
268-
}
269-
270-
getJumpNextPage() {
271-
return Math.min(this.calculatePage(), this.state.current + (this.props.showLessItems ? 3 : 5));
272-
}
273-
274274
jumpPrev = () => {
275275
this.handleChange(this.getJumpPrevPage());
276276
}
@@ -284,7 +284,7 @@ export default class Pagination extends React.Component {
284284
}
285285

286286
hasNext = () => {
287-
return this.state.current < this.calculatePage();
287+
return this.state.current < calculatePage(undefined, this.state, this.props);
288288
}
289289

290290
runIfEnter = (event, callback, ...restParams) => {
@@ -325,7 +325,7 @@ export default class Pagination extends React.Component {
325325
const locale = props.locale;
326326

327327
const prefixCls = props.prefixCls;
328-
const allPages = this.calculatePage();
328+
const allPages = calculatePage(undefined, this.state, this.props);
329329
const pagerList = [];
330330
let jumpPrev = null;
331331
let jumpNext = null;
@@ -647,3 +647,7 @@ export default class Pagination extends React.Component {
647647
);
648648
}
649649
}
650+
651+
polyfill(Pagination);
652+
653+
export default Pagination;

src/locale/ro_RO.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default {
2+
// Options.jsx
3+
items_per_page: '/ pagină',
4+
jump_to: 'Mergi la',
5+
jump_to_confirm: 'confirm',
6+
page: '',
7+
8+
// Pagination.jsx
9+
prev_page: 'Pagina Anterioară',
10+
next_page: 'Pagina Următoare',
11+
prev_5: '5 Pagini Anterioare',
12+
next_5: '5 Pagini Următoare',
13+
prev_3: '3 Pagini Anterioare',
14+
next_3: '3 Pagini Următoare',
15+
};

0 commit comments

Comments
 (0)