Skip to content

Fix behavior of the cursor after increment/decrement the value #78

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion __tests__/misc.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,35 @@ describe ('NumericInput/misc', function() {
widget.refsInput.selectionStart = 1
widget.refsInput.selectionEnd = 1
widget.saveSelection()
widget.increase()
widget.refsInput.value = 11
TestUtils.Simulate.change(widget.refsInput)
expect(widget.refsInput.value).toEqual('11%')
expect(widget.refsInput.selectionStart).toEqual(1)
expect(widget.refsInput.selectionEnd).toEqual(1)
done()
})
}

/**
* Selection have to be moved to the right after increment/decrement
* the value according to common behavior of html <input type=number /> element
*/
it ('persists selection', done => {
let widget = TestUtils.renderIntoDocument(
<NumericInput value={9}/>
)
expect(widget.refsInput.value).toEqual('9')
TestUtils.Simulate.focus(widget.refsInput)
widget.refsInput.selectionStart = 1
widget.refsInput.selectionEnd = 1
widget.saveSelection()
widget.increase()
expect(widget.refsInput.value).toEqual('10')
expect(widget.refsInput.selectionStart).toEqual(2)
expect(widget.refsInput.selectionEnd).toEqual(2)
done()
})

/**
* onChange gets called when input content is deleted
* @see https://github.com/vlad-ignatov/react-numeric-input/issues/27
Expand Down
10 changes: 8 additions & 2 deletions dist/react-numeric-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,13 @@ return /******/ (function(modules) { // webpackBootstrap
this._isStrict = _isStrict;

if (_n !== this.state.value) {
this.setState({ value: _n, stringValue: _n + "" }, callback);
var _nString = _n + "";
this.setState({
value: _n,
stringValue: _nString,
selectionStart: _nString.length,
selectionEnd: _nString.length
}, callback);
return true;
}

Expand Down Expand Up @@ -1082,7 +1088,7 @@ return /******/ (function(modules) { // webpackBootstrap
value: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]),
defaultValue: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]),
strict: _propTypes2.default.bool,
componentClass: _propTypes2.default.string,
componentClass: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string]),
mobile: function mobile(props, propName) {
var prop = props[propName];
if (prop !== true && prop !== false && prop !== 'auto' && typeof prop != 'function') {
Expand Down
2 changes: 1 addition & 1 deletion dist/react-numeric-input.min.js

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions docs/react-numeric-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,13 @@ return /******/ (function(modules) { // webpackBootstrap
this._isStrict = _isStrict;

if (_n !== this.state.value) {
this.setState({ value: _n, stringValue: _n + "" }, callback);
var _nString = _n + "";
this.setState({
value: _n,
stringValue: _nString,
selectionStart: _nString.length,
selectionEnd: _nString.length
}, callback);
return true;
}

Expand Down Expand Up @@ -1082,7 +1088,7 @@ return /******/ (function(modules) { // webpackBootstrap
value: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]),
defaultValue: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]),
strict: _propTypes2.default.bool,
componentClass: _propTypes2.default.string,
componentClass: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string]),
mobile: function mobile(props, propName) {
var prop = props[propName];
if (prop !== true && prop !== false && prop !== 'auto' && typeof prop != 'function') {
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,13 @@ module.exports =
this._isStrict = _isStrict;

if (_n !== this.state.value) {
this.setState({ value: _n, stringValue: _n + "" }, callback);
var _nString = _n + "";
this.setState({
value: _n,
stringValue: _nString,
selectionStart: _nString.length,
selectionEnd: _nString.length
}, callback);
return true;
}

Expand Down Expand Up @@ -804,7 +810,7 @@ module.exports =
value: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]),
defaultValue: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]),
strict: _propTypes2.default.bool,
componentClass: _propTypes2.default.string,
componentClass: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string]),
mobile: function mobile(props, propName) {
var prop = props[propName];
if (prop !== true && prop !== false && prop !== 'auto' && typeof prop != 'function') {
Expand Down
8 changes: 7 additions & 1 deletion src/NumericInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,13 @@ class NumericInput extends Component
this._isStrict = _isStrict;

if (_n !== this.state.value) {
this.setState({ value: _n, stringValue: _n + "" }, callback);
var _nString = _n + "";
this.setState({
value: _n,
stringValue: _nString,
selectionStart: _nString.length,
selectionEnd: _nString.length
}, callback);
return true;
}

Expand Down