Skip to content

Commit b9f1011

Browse files
committed
- Fixed mobile caret issue and
- take element from target instead of ref and focus element
1 parent 60eea9c commit b9f1011

File tree

5 files changed

+76
-44
lines changed

5 files changed

+76
-44
lines changed

dist/react-number-format.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* react-number-format - 1.1.0-alpha
2+
* react-number-format - 1.1.0-alpha2
33
* Author : Sudhanshu Yadav
44
* Copyright (c) 2016,2017 to Sudhanshu Yadav - ignitersworld.com , released under the MIT license.
55
*/
@@ -98,7 +98,8 @@ return /******/ (function(modules) { // webpackBootstrap
9898
suffix: _react.PropTypes.string,
9999
format: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.func]),
100100
mask: _react.PropTypes.string,
101-
value: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string])
101+
value: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
102+
customInput: _react.PropTypes.func
102103
};
103104

104105
var defaultProps = {
@@ -167,8 +168,7 @@ return /******/ (function(modules) { // webpackBootstrap
167168
}
168169
}, {
169170
key: 'setCaretPosition',
170-
value: function setCaretPosition(caretPos) {
171-
var el = this.refs.input;
171+
value: function setCaretPosition(el, caretPos) {
172172
el.value = el.value;
173173
// ^ this is used to not only get "focus", but
174174
// to make sure we don't have it everything -selected-
@@ -302,18 +302,25 @@ return /******/ (function(modules) { // webpackBootstrap
302302
var _this2 = this;
303303

304304
e.persist();
305-
var inputValue = e.target.value;
305+
var el = e.target;
306+
var inputValue = el.value;
306307

307308
var _formatInput = this.formatInput(inputValue),
308309
formattedValue = _formatInput.formattedValue,
309310
value = _formatInput.value;
310311

311-
var cursorPos = this.refs.input.selectionStart;
312+
var cursorPos = el.selectionStart;
312313

313314
//change the state
314315
this.setState({ value: formattedValue }, function () {
315316
cursorPos = _this2.getCursorPosition(inputValue, formattedValue, cursorPos);
316-
_this2.setCaretPosition(cursorPos);
317+
/*
318+
setting caret position within timeout of 0ms is required for mobile chrome,
319+
otherwise browser resets the caret position after we set it
320+
*/
321+
setTimeout(function () {
322+
return _this2.setCaretPosition(el, cursorPos);
323+
}, 0);
317324
if (callback) callback(e, value);
318325
});
319326

@@ -332,26 +339,28 @@ return /******/ (function(modules) { // webpackBootstrap
332339
}, {
333340
key: 'onKeyDown',
334341
value: function onKeyDown(e) {
335-
var _refs$input = this.refs.input,
336-
selectionStart = _refs$input.selectionStart,
337-
selectionEnd = _refs$input.selectionEnd,
338-
value = _refs$input.value;
342+
var el = e.target;
343+
var selectionStart = el.selectionStart,
344+
selectionEnd = el.selectionEnd,
345+
value = el.value;
339346
var decimalPrecision = this.props.decimalPrecision;
340347
var key = e.key;
341348

342349
var numRegex = this.getNumberRegex(false, decimalPrecision !== false);
343350
//Handle backspace and delete against non numerical/decimal characters
344351
if (selectionEnd - selectionStart === 0) {
345352
if (key === 'Delete' && !numRegex.test(value[selectionStart])) {
353+
e.preventDefault();
346354
var nextCursorPosition = selectionStart;
347355
while (!numRegex.test(value[nextCursorPosition]) && nextCursorPosition < value.length) {
348356
nextCursorPosition++;
349-
}this.setCaretPosition(nextCursorPosition);
357+
}this.setCaretPosition(el, nextCursorPosition);
350358
} else if (key === 'Backspace' && !numRegex.test(value[selectionStart - 1])) {
359+
e.preventDefault();
351360
var prevCursorPosition = selectionStart;
352361
while (!numRegex.test(value[prevCursorPosition - 1]) && prevCursorPosition > 0) {
353362
prevCursorPosition--;
354-
}this.setCaretPosition(prevCursorPosition);
363+
}this.setCaretPosition(el, prevCursorPosition);
355364
}
356365
}
357366

@@ -366,21 +375,26 @@ return /******/ (function(modules) { // webpackBootstrap
366375
delete props[key];
367376
});
368377

378+
var inputProps = _extends({}, props, {
379+
type: 'tel',
380+
value: this.state.value,
381+
onInput: this.onChange,
382+
onChange: this.onChange,
383+
onKeyDown: this.onKeyDown
384+
});
385+
369386
if (this.props.displayType === 'text') {
370387
return _react2.default.createElement(
371388
'span',
372389
props,
373390
this.state.value
374391
);
392+
} else if (this.props.customInput) {
393+
var CustomInput = this.props.customInput;
394+
return _react2.default.createElement(CustomInput, inputProps);
375395
}
376-
return _react2.default.createElement('input', _extends({}, props, {
377-
type: 'tel',
378-
value: this.state.value,
379-
ref: 'input',
380-
onInput: this.onChange,
381-
onChange: this.onChange,
382-
onKeyDown: this.onKeyDown
383-
}));
396+
397+
return _react2.default.createElement('input', inputProps);
384398
}
385399
}]);
386400

dist/react-number-format.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)