Skip to content

feat: blur quick jumper input to change current item #188

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 3 commits into from
Jul 22, 2019
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"now-build": "npm run build"
},
"devDependencies": {
"core-js": "^3.0.0",
"core-js": "^3.1.4",
"expect.js": "0.3.x",
"pre-commit": "1.x",
"rc-select": "9.x",
Expand Down
27 changes: 18 additions & 9 deletions src/Options.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ class Options extends React.Component {
pageSizeOptions: ['10', '20', '30', '40'],
};

constructor(props) {
super(props);
state = {
goInputText: '',
};

this.state = {
goInputText: '',
};
getValidValue() {
const { goInputText, current } = this.state;
return isNaN(goInputText) ? current : Number(goInputText);
}

buildOptionText = (value) => {
Expand All @@ -44,17 +45,24 @@ class Options extends React.Component {
});
}

handleBlur = () => {
const { goButton, quickGo } = this.props;
if (goButton) {
return;
}
quickGo(this.getValidValue());
}

go = (e) => {
let val = this.state.goInputText;
if (val === '') {
const { goInputText } = this.state;
if (goInputText === '') {
return;
}
val = isNaN(val) ? this.props.current : Number(val);
if (e.keyCode === KEYCODE.ENTER || e.type === 'click') {
this.setState({
goInputText: '',
});
this.props.quickGo(val);
this.props.quickGo(this.getValidValue());
}
}

Expand Down Expand Up @@ -128,6 +136,7 @@ class Options extends React.Component {
value={goInputText}
onChange={this.handleChange}
onKeyUp={this.go}
onBlur={this.handleBlur}
/>
{locale.page}
{gotoButton}
Expand Down
29 changes: 16 additions & 13 deletions src/Pagination.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ class Pagination extends React.Component {
return iconNode;
}

getValidValue(e) {
const inputValue = e.target.value;
const { currentInputValue } = this.state;
let value;
if (inputValue === '') {
value = inputValue;
} else if (isNaN(Number(inputValue))) {
value = currentInputValue;
} else {
value = Number(inputValue);
}
return value;
}

savePaginationNode = (node) => {
this.paginationNode = node;
}
Expand All @@ -194,24 +208,13 @@ class Pagination extends React.Component {
}

handleKeyUp = (e) => {
const inputValue = e.target.value;
const currentInputValue = this.state.currentInputValue;
let value;

if (inputValue === '') {
value = inputValue;
} else if (isNaN(Number(inputValue))) {
value = currentInputValue;
} else {
value = Number(inputValue);
}

const value = this.getValidValue(e);
const { currentInputValue } = this.state;
if (value !== currentInputValue) {
this.setState({
currentInputValue: value,
});
}

if (e.keyCode === KEYCODE.ENTER) {
this.handleChange(value);
} else if (e.keyCode === KEYCODE.ARROW_UP) {
Expand Down
44 changes: 42 additions & 2 deletions tests/Pagination.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('Uncontrolled Pagination', () => {

afterEach(() => {
ReactDOM.unmountComponentAtNode(container);
current = 1;
});

it('default current page is 1', () => {
Expand Down Expand Up @@ -135,8 +136,7 @@ describe('Uncontrolled Pagination', () => {
expect(TestUtils.isDOMComponent(quickJumper)).to.be(true);
expect(TestUtils.isDOMComponent(input)).to.be(true);
expect(TestUtils.isDOMComponent(goButton)).to.be(true);
input.value = '2';
Simulate.change(input);
Simulate.change(input, { target: { value: '2' } });
setTimeout(() => {
Simulate.click(goButton);
setTimeout(() => {
Expand All @@ -148,6 +148,46 @@ describe('Uncontrolled Pagination', () => {
}, 10);
});

// https://github.com/ant-design/ant-design/issues/17763
it('should not jump when blur input when there is goButton', (done) => {
const quickJumper = TestUtils.findRenderedDOMComponentWithClass(
pagination,
'rc-pagination-options-quick-jumper'
);
const input = quickJumper.querySelector('input');
Simulate.change(input, { target: { value: '2' } });
setTimeout(() => {
Simulate.blur(input);
setTimeout(() => {
expect(pagination.state.current).to.be(1);
expect(current).to.be(1);
done();
}, 10);
}, 10);
});

// https://github.com/ant-design/ant-design/issues/17763
it('should not jump when blur input when there is not goButton', (done) => {
ReactDOM.render(
<Pagination pageSize={10} total={20} showQuickJumper />,
container,
function () {
const quickJumper = TestUtils.findRenderedDOMComponentWithClass(
this, 'rc-pagination-options-quick-jumper',
);
const input = quickJumper.querySelector('input');
Simulate.change(input, { target: { value: '2' } });
setTimeout(() => {
Simulate.blur(input);
setTimeout(() => {
expect(this.state.current).to.be(2);
done();
}, 10);
}, 10);
},
);
});

// https://github.com/ant-design/ant-design/issues/15539
it('should hide quick jumper when only one page', (done) => {
ReactDOM.render(
Expand Down