Skip to content

Commit 22326db

Browse files
committed
fix: blur goto input should came back to 1
close ant-design/ant-design#35118
1 parent 3fd1744 commit 22326db

File tree

3 files changed

+54
-39
lines changed

3 files changed

+54
-39
lines changed

docs/examples/default.jsx

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
1-
/* eslint func-names: 0, no-console: 0 */
2-
import React from 'react';
1+
import React, { useState } from 'react';
32
import Pagination from 'rc-pagination';
43
import '../../assets/index.less';
54
import 'rc-select/assets/index.less';
65

7-
export default class App extends React.Component {
8-
state = {
9-
current: 3,
6+
const App = () => {
7+
const [current, setCurrent] = useState(1);
8+
const onChange = page => {
9+
setCurrent(page);
1010
};
11+
return (
12+
<Pagination
13+
onChange={onChange}
14+
current={current}
15+
total={25}
16+
/>
17+
);
18+
};
1119

12-
onChange = page => {
13-
console.log(page);
14-
this.setState({
15-
current: page,
16-
});
17-
};
18-
19-
render() {
20-
return (
21-
<Pagination
22-
onChange={this.onChange}
23-
current={this.state.current}
24-
total={25}
25-
/>
26-
);
27-
}
28-
}
20+
export default App;

src/Pagination.jsx

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -253,32 +253,31 @@ class Pagination extends React.Component {
253253
}
254254
};
255255

256-
handleChange = (p) => {
257-
const { disabled } = this.props;
258-
259-
let page = p;
256+
handleChange = (page) => {
257+
const { disabled, onChange } = this.props;
258+
const { pageSize, current, currentInputValue } = this.state;
260259
if (this.isValid(page) && !disabled) {
261260
const currentPage = calculatePage(undefined, this.state, this.props);
261+
let newPage;
262262
if (page > currentPage) {
263-
page = currentPage;
263+
newPage = currentPage;
264264
} else if (page < 1) {
265-
page = 1;
265+
newPage = 1;
266266
}
267-
268267
if (!('current' in this.props)) {
269268
this.setState({
270-
current: page,
271-
currentInputValue: page,
269+
current: newPage,
272270
});
273271
}
274-
275-
const { pageSize } = this.state;
276-
this.props.onChange(page, pageSize);
277-
278-
return page;
272+
if (newPage !== currentInputValue) {
273+
this.setState({
274+
currentInputValue: newPage,
275+
});
276+
}
277+
onChange(newPage, pageSize);
278+
return newPage;
279279
}
280-
281-
return this.state.current;
280+
return current;
282281
};
283282

284283
prev = () => {

tests/simple.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import { mount } from 'enzyme';
33
import Pagination from '../src';
44

@@ -34,6 +34,30 @@ describe('simple Pagination', () => {
3434
expect(onChange).toBeCalled();
3535
});
3636

37+
it('should return to 1 when blur goto input in uncontrol mode', () => {
38+
const component = mount(
39+
<Pagination simple defaultCurrent={1} total={25} />,
40+
);
41+
const input = component.find('.rc-pagination-simple').find('input');
42+
input.simulate('focus');
43+
input.simulate('change', { target: { value: '' } });
44+
input.simulate('blur');
45+
expect(input.getDOMNode().value).toBe('1');
46+
});
47+
48+
it('should return to 1 when blur goto input in control mode', () => {
49+
const App = () => {
50+
const [current, setCurrent] = useState(1);
51+
return <Pagination simple current={1} total={25} onChange={setCurrent} />;
52+
};
53+
const component = mount(<App />);
54+
const input = component.find('.rc-pagination-simple').find('input');
55+
input.simulate('focus');
56+
input.simulate('change', { target: { value: '' } });
57+
input.simulate('blur');
58+
expect(input.getDOMNode().value).toBe('1');
59+
});
60+
3761
it('default current page is 1', () => {
3862
expect(wrapper.state().current).toBe(1);
3963
});

0 commit comments

Comments
 (0)