Skip to content

fix: blur goto input should reset to 1 #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/demo/basic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## simple

<code src="../examples/simple.jsx">
3 changes: 3 additions & 0 deletions docs/demo/controlled.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## default

<code src="../examples/controlled.jsx">
3 changes: 0 additions & 3 deletions docs/demo/default.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/demo/simple.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## simple
## stupid

<code src="../examples/simple.jsx">
3 changes: 0 additions & 3 deletions docs/demo/stupid.md

This file was deleted.

19 changes: 19 additions & 0 deletions docs/examples/basic.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import '../../assets/index.less';
import React from 'react';
import Pagination from 'rc-pagination';

const App = () => (
<>
<Pagination total={25} />
<Pagination total={50} />
<Pagination total={60} />
<Pagination total={70} />
<Pagination total={80} />
<Pagination total={90} />
<Pagination total={100} />
<Pagination total={120} />
<Pagination total={500} />
</>
);

export default App;
20 changes: 20 additions & 0 deletions docs/examples/controlled.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, { useState } from 'react';
import Pagination from 'rc-pagination';
import '../../assets/index.less';
import 'rc-select/assets/index.less';

const App = () => {
const [current, setCurrent] = useState(1);
const onChange = page => {
setCurrent(page);
};
return (
<Pagination
onChange={onChange}
current={current}
total={25}
/>
);
};

export default App;
28 changes: 0 additions & 28 deletions docs/examples/default.jsx

This file was deleted.

18 changes: 2 additions & 16 deletions docs/examples/simple.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
import '../../assets/index.less';
import React from 'react';
import Pagination from 'rc-pagination';
import '../../assets/index.less';

const App = () => (
<>
<Pagination total={25} />
<Pagination total={50} />
<Pagination total={60} />
<Pagination total={70} />
<Pagination total={80} />
<Pagination total={90} />
<Pagination total={100} />
<Pagination total={120} />
<Pagination total={500} />
</>
);

export default App;
export default () => <Pagination simple defaultCurrent={1} total={50} />;
5 changes: 0 additions & 5 deletions docs/examples/stupid.jsx

This file was deleted.

31 changes: 15 additions & 16 deletions src/Pagination.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,32 +253,31 @@ class Pagination extends React.Component {
}
};

handleChange = (p) => {
const { disabled } = this.props;

let page = p;
handleChange = (page) => {
const { disabled, onChange } = this.props;
const { pageSize, current, currentInputValue } = this.state;
if (this.isValid(page) && !disabled) {
const currentPage = calculatePage(undefined, this.state, this.props);
let newPage = page;
if (page > currentPage) {
page = currentPage;
newPage = currentPage;
} else if (page < 1) {
page = 1;
newPage = 1;
}

if (!('current' in this.props)) {
this.setState({
current: page,
currentInputValue: page,
current: newPage,
});
}

const { pageSize } = this.state;
this.props.onChange(page, pageSize);

return page;
if (newPage !== currentInputValue) {
this.setState({
currentInputValue: newPage,
});
}
onChange(newPage, pageSize);
return newPage;
}

return this.state.current;
return current;
};

prev = () => {
Expand Down
26 changes: 25 additions & 1 deletion tests/simple.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import { mount } from 'enzyme';
import Pagination from '../src';

Expand Down Expand Up @@ -34,6 +34,30 @@ describe('simple Pagination', () => {
expect(onChange).toBeCalled();
});

it('should return to 1 when blur goto input in uncontrol mode', () => {
const component = mount(
<Pagination simple defaultCurrent={1} total={25} />,
);
const input = component.find('.rc-pagination-simple').find('input');
input.simulate('focus');
input.simulate('change', { target: { value: '' } });
input.simulate('blur');
expect(input.getDOMNode().value).toBe('1');
});

it('should return to 1 when blur goto input in control mode', () => {
const App = () => {
const [current, setCurrent] = useState(1);
return <Pagination simple current={1} total={25} onChange={setCurrent} />;
};
const component = mount(<App />);
const input = component.find('.rc-pagination-simple').find('input');
input.simulate('focus');
input.simulate('change', { target: { value: '' } });
input.simulate('blur');
expect(input.getDOMNode().value).toBe('1');
});

it('default current page is 1', () => {
expect(wrapper.state().current).toBe(1);
});
Expand Down