Skip to content

Commit d426196

Browse files
authored
fix: change pageSize not call onChange (#272)
* fix: change pageSize not call onChange * feat: adjust onShowSizeChange * adjust perttier * feat: add test case
1 parent 9257d8d commit d426196

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

src/Pagination.jsx

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class Pagination extends React.Component {
133133
* @param {React.ReactNode | React.ComponentType<PaginationProps>} icon received icon.
134134
* @returns {React.ReactNode}
135135
*/
136-
getItemIcon = icon => {
136+
getItemIcon = (icon) => {
137137
const { prefixCls } = this.props;
138138
// eslint-disable-next-line jsx-a11y/anchor-has-content
139139
let iconNode = icon || <a className={`${prefixCls}-item-link`} />;
@@ -161,11 +161,11 @@ class Pagination extends React.Component {
161161
return value;
162162
}
163163

164-
savePaginationNode = node => {
164+
savePaginationNode = (node) => {
165165
this.paginationNode = node;
166166
};
167167

168-
isValid = page => isInteger(page) && page !== this.state.current;
168+
isValid = (page) => isInteger(page) && page !== this.state.current;
169169

170170
shouldDisplayQuickJumper = () => {
171171
const { showQuickJumper, pageSize, total } = this.props;
@@ -175,13 +175,13 @@ class Pagination extends React.Component {
175175
return showQuickJumper;
176176
};
177177

178-
handleKeyDown = e => {
178+
handleKeyDown = (e) => {
179179
if (e.keyCode === KEYCODE.ARROW_UP || e.keyCode === KEYCODE.ARROW_DOWN) {
180180
e.preventDefault();
181181
}
182182
};
183183

184-
handleKeyUp = e => {
184+
handleKeyUp = (e) => {
185185
const value = this.getValidValue(e);
186186
const { currentInputValue } = this.state;
187187
if (value !== currentInputValue) {
@@ -198,7 +198,7 @@ class Pagination extends React.Component {
198198
}
199199
};
200200

201-
changePageSize = size => {
201+
changePageSize = (size) => {
202202
let { current } = this.state;
203203
const newCurrent = calculatePage(size, this.state, this.props);
204204
current = current > newCurrent ? newCurrent : current;
@@ -222,10 +222,15 @@ class Pagination extends React.Component {
222222
});
223223
}
224224
}
225+
226+
if ('onChange' in this.props && this.props.onChange) {
227+
this.props.onChange(current, size);
228+
}
229+
225230
this.props.onShowSizeChange(current, size);
226231
};
227232

228-
handleChange = p => {
233+
handleChange = (p) => {
229234
const { disabled } = this.props;
230235

231236
let page = p;
@@ -278,37 +283,37 @@ class Pagination extends React.Component {
278283
hasNext = () =>
279284
this.state.current < calculatePage(undefined, this.state, this.props);
280285

281-
getShowSizeChanger() {
282-
const { showSizeChanger, total, totalBoundaryShowSizeChanger } = this.props;
283-
if (typeof showSizeChanger !== 'undefined') {
284-
return showSizeChanger;
285-
}
286-
return total > totalBoundaryShowSizeChanger;
287-
}
286+
getShowSizeChanger() {
287+
const { showSizeChanger, total, totalBoundaryShowSizeChanger } = this.props;
288+
if (typeof showSizeChanger !== 'undefined') {
289+
return showSizeChanger;
290+
}
291+
return total > totalBoundaryShowSizeChanger;
292+
}
288293

289294
runIfEnter = (event, callback, ...restParams) => {
290295
if (event.key === 'Enter' || event.charCode === 13) {
291296
callback(...restParams);
292297
}
293298
};
294299

295-
runIfEnterPrev = e => {
300+
runIfEnterPrev = (e) => {
296301
this.runIfEnter(e, this.prev);
297302
};
298303

299-
runIfEnterNext = e => {
304+
runIfEnterNext = (e) => {
300305
this.runIfEnter(e, this.next);
301306
};
302307

303-
runIfEnterJumpPrev = e => {
308+
runIfEnterJumpPrev = (e) => {
304309
this.runIfEnter(e, this.jumpPrev);
305310
};
306311

307-
runIfEnterJumpNext = e => {
312+
runIfEnterJumpNext = (e) => {
308313
this.runIfEnter(e, this.jumpNext);
309314
};
310315

311-
handleGoTO = e => {
316+
handleGoTO = (e) => {
312317
if (e.keyCode === KEYCODE.ENTER || e.type === 'click') {
313318
this.handleChange(this.state.currentInputValue);
314319
}

tests/sizer.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ describe('Pagination with sizer', () => {
4040
/>,
4141
);
4242
wrapper.find(Select).find('input').simulate('mousedown');
43-
expect(wrapper.find(Select).find('.rc-select-item').at(2).text()).toBe('45 条/页✓');
43+
expect(wrapper.find(Select).find('.rc-select-item').at(2).text()).toBe(
44+
'45 条/页✓',
45+
);
46+
});
47+
48+
it('should onChange called when pageSize change', () => {
49+
const onChange = jest.fn();
50+
const wrapper = mount(
51+
<Pagination
52+
selectComponentClass={Select}
53+
onChange={onChange}
54+
total={500}
55+
defaultPageSize={20}
56+
/>,
57+
);
58+
wrapper.find(Select).find('input').simulate('mousedown');
59+
expect(wrapper.find(Select).find('.rc-select-item').at(2).text()).toBe(
60+
'50 条/页',
61+
);
62+
const pageSize1 = wrapper.find(Select).find('.rc-select-item').at(0);
63+
pageSize1.simulate('click');
64+
expect(onChange).toBeCalled();
65+
expect(onChange).toHaveBeenLastCalledWith(1, 10);
4466
});
4567
});

0 commit comments

Comments
 (0)