Skip to content

Commit a889816

Browse files
orzyyyyafc163
authored andcommitted
pref: migrate to new lifecycle
1 parent eeb8732 commit a889816

File tree

2 files changed

+113
-106
lines changed

2 files changed

+113
-106
lines changed

package.json

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
1-
{
2-
"name": "rc-pagination",
3-
"version": "1.17.3",
4-
"description": "pagination ui component for react",
5-
"keywords": [
6-
"react",
7-
"react-component",
8-
"react-pagination",
9-
"pagination"
10-
],
11-
"homepage": "http://github.com/react-component/pagination",
12-
"author": "i@xiaoba.me",
13-
"repository": {
14-
"type": "git",
15-
"url": "git@github.com:react-component/pagination.git"
16-
},
17-
"bugs": {
18-
"url": "http://github.com/react-component/pagination/issues"
19-
},
20-
"license": "MIT",
21-
"files": [
22-
"dist",
23-
"lib",
24-
"es",
25-
"assets/*.css"
26-
],
27-
"main": "lib/index",
28-
"module": "es/index",
29-
"config": {
30-
"port": 3000,
31-
"entry": {
32-
"rc-pagination": [
33-
"./src/index.js",
34-
"./assets/index.less"
35-
]
36-
}
37-
},
38-
"scripts": {
39-
"dist": "rc-tools run dist",
40-
"build": "rc-tools run build",
41-
"compile": "rc-tools run compile --babel-runtime",
42-
"gh-pages": "rc-tools run gh-pages",
43-
"start": "rc-tools run server",
44-
"pub": "rc-tools run pub --babel-runtime",
45-
"lint": "rc-tools run lint",
46-
"lint:fix": "rc-tools run lint --fix",
47-
"karma": "rc-test run karma",
48-
"saucelabs": "rc-test run saucelabs",
49-
"test": "rc-test run test",
50-
"chrome-test": "rc-test run chrome-test",
51-
"coverage": "rc-test run coverage"
52-
},
53-
"devDependencies": {
54-
"core-js": "^2.5.1",
55-
"expect.js": "0.3.x",
56-
"pre-commit": "1.x",
57-
"rc-select": "6.x",
58-
"rc-test": "^6.0.1",
59-
"rc-tools": "6.x",
60-
"react": "^16.0.0",
61-
"react-dom": "^16.0.0"
62-
},
63-
"pre-commit": [
64-
"lint"
65-
],
66-
"dependencies": {
67-
"babel-runtime": "6.x",
68-
"prop-types": "^15.5.7"
69-
}
70-
}
1+
{
2+
"name": "rc-pagination",
3+
"version": "1.17.3",
4+
"description": "pagination ui component for react",
5+
"keywords": [
6+
"react",
7+
"react-component",
8+
"react-pagination",
9+
"pagination"
10+
],
11+
"homepage": "http://github.com/react-component/pagination",
12+
"author": "i@xiaoba.me",
13+
"repository": {
14+
"type": "git",
15+
"url": "git@github.com:react-component/pagination.git"
16+
},
17+
"bugs": {
18+
"url": "http://github.com/react-component/pagination/issues"
19+
},
20+
"license": "MIT",
21+
"files": [
22+
"dist",
23+
"lib",
24+
"es",
25+
"assets/*.css"
26+
],
27+
"main": "lib/index",
28+
"module": "es/index",
29+
"config": {
30+
"port": 3000,
31+
"entry": {
32+
"rc-pagination": [
33+
"./src/index.js",
34+
"./assets/index.less"
35+
]
36+
}
37+
},
38+
"scripts": {
39+
"dist": "rc-tools run dist",
40+
"build": "rc-tools run build",
41+
"compile": "rc-tools run compile --babel-runtime",
42+
"gh-pages": "rc-tools run gh-pages",
43+
"start": "rc-tools run server",
44+
"pub": "rc-tools run pub --babel-runtime",
45+
"lint": "rc-tools run lint",
46+
"lint:fix": "rc-tools run lint --fix",
47+
"karma": "rc-test run karma",
48+
"saucelabs": "rc-test run saucelabs",
49+
"test": "rc-test run test",
50+
"chrome-test": "rc-test run chrome-test",
51+
"coverage": "rc-test run coverage"
52+
},
53+
"devDependencies": {
54+
"core-js": "^2.5.1",
55+
"expect.js": "0.3.x",
56+
"pre-commit": "1.x",
57+
"rc-select": "6.x",
58+
"rc-test": "^6.0.1",
59+
"rc-tools": "6.x",
60+
"react": "^16.5.2",
61+
"react-dom": "^16.5.2"
62+
},
63+
"pre-commit": [
64+
"lint"
65+
],
66+
"dependencies": {
67+
"babel-runtime": "6.x",
68+
"prop-types": "^15.5.7"
69+
}
70+
}

src/Pagination.jsx

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ function defaultItemRender(page, type, element) {
1818
return element;
1919
}
2020

21+
function calculatePage(p, state, props) {
22+
let pageSize = p;
23+
if (typeof pageSize === 'undefined') {
24+
pageSize = state.pageSize;
25+
}
26+
return Math.floor((props.total - 1) / pageSize) + 1;
27+
}
28+
2129
export default class Pagination extends React.Component {
2230
static propTypes = {
2331
prefixCls: PropTypes.string,
@@ -93,28 +101,6 @@ export default class Pagination extends React.Component {
93101
};
94102
}
95103

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-
118104
componentDidUpdate(prevProps, prevState) {
119105
// When current page change, fix focused style of prev item
120106
// A hacky solution of https://github.com/ant-design/ant-design/issues/8948
@@ -129,12 +115,40 @@ export default class Pagination extends React.Component {
129115
}
130116
}
131117

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

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

140154
/**
@@ -156,14 +170,6 @@ export default class Pagination extends React.Component {
156170
this.paginationNode = node;
157171
}
158172

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-
167173
isValid = (page) => {
168174
return isInteger(page) && page >= 1 && page !== this.state.current;
169175
}
@@ -204,7 +210,7 @@ export default class Pagination extends React.Component {
204210

205211
changePageSize = (size) => {
206212
let current = this.state.current;
207-
const newCurrent = this.calculatePage(size);
213+
const newCurrent = calculatePage(size, this.state, this.props);
208214
current = current > newCurrent ? newCurrent : current;
209215
// fix the issue:
210216
// Once 'total' is 0, 'current' in 'onShowSizeChange' is 0, which is not correct.
@@ -231,8 +237,9 @@ export default class Pagination extends React.Component {
231237
handleChange = (p) => {
232238
let page = p;
233239
if (this.isValid(page)) {
234-
if (page > this.calculatePage()) {
235-
page = this.calculatePage();
240+
const currentPage = calculatePage(undefined, this.state, this.props);
241+
if (page > currentPage) {
242+
page = currentPage;
236243
}
237244

238245
if (!('current' in this.props)) {
@@ -276,7 +283,7 @@ export default class Pagination extends React.Component {
276283
}
277284

278285
hasNext = () => {
279-
return this.state.current < this.calculatePage();
286+
return this.state.current < calculatePage(undefined, this.state, this.props);
280287
}
281288

282289
runIfEnter = (event, callback, ...restParams) => {
@@ -317,7 +324,7 @@ export default class Pagination extends React.Component {
317324
const locale = props.locale;
318325

319326
const prefixCls = props.prefixCls;
320-
const allPages = this.calculatePage();
327+
const allPages = calculatePage(undefined, this.state, this.props);
321328
const pagerList = [];
322329
let jumpPrev = null;
323330
let jumpNext = null;

0 commit comments

Comments
 (0)