react-input-mask
Advanced tools
Comparing version 1.0.0-beta.1 to 1.0.0
@@ -302,2 +302,10 @@ (function (global, factory) { | ||
var defer = function (fn) { | ||
var defer = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (fn) { | ||
return setTimeout(fn, 0); | ||
}; | ||
return defer(fn); | ||
}; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
@@ -307,4 +315,2 @@ | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
@@ -353,13 +359,6 @@ | ||
_this.state = { value: value }; | ||
_this.value = value; | ||
return _this; | ||
} | ||
_createClass(InputElement, [{ | ||
key: 'componentWillUnmount', | ||
value: function componentWillUnmount() { | ||
this.unmounted = true; | ||
} | ||
}]); | ||
return InputElement; | ||
@@ -378,14 +377,5 @@ }(React.Component); | ||
var input = _this2.getInputDOMNode(); | ||
// workaround for Jest | ||
// it doesn't mount a real node so input will be null | ||
if (input && Object.getOwnPropertyDescriptor && Object.getPrototypeOf && Object.defineProperty) { | ||
var valueDescriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(input), 'value'); | ||
_this2.canUseAccessors = !!(valueDescriptor && valueDescriptor.get && valueDescriptor.set); | ||
if (_this2.getInputValue() !== _this2.value) { | ||
_this2.setInputValue(_this2.value); | ||
} | ||
if (_this2.maskOptions.mask && _this2.props.value == null) { | ||
_this2.updateUncontrolledInput(); | ||
} | ||
}; | ||
@@ -406,3 +396,3 @@ | ||
var showEmpty = nextProps.alwaysShowMask || _this2.isFocused(); | ||
var newValue = _this2.hasValue ? _this2.getStringValue(nextProps.value) : _this2.state.value; | ||
var newValue = _this2.hasValue ? _this2.getStringValue(nextProps.value) : _this2.value; | ||
@@ -435,16 +425,8 @@ if (!oldMaskOptions.mask && !_this2.hasValue) { | ||
_this2.value = newValue; | ||
if (_this2.state.value !== newValue) { | ||
_this2.setState({ value: newValue }); | ||
} | ||
}; | ||
this.componentDidUpdate = function (prevProps) { | ||
if ((_this2.maskOptions.mask || prevProps.mask) && _this2.props.value == null) { | ||
_this2.updateUncontrolledInput(); | ||
this.componentDidUpdate = function () { | ||
if (_this2.getInputValue() !== _this2.value) { | ||
_this2.setInputValue(_this2.value); | ||
} | ||
if (_this2.valueDescriptor && _this2.getInputValue() !== _this2.state.value) { | ||
_this2.setInputValue(_this2.state.value); | ||
} | ||
}; | ||
@@ -459,3 +441,2 @@ | ||
var input = _this2.input; | ||
if (!input) { | ||
@@ -473,45 +454,9 @@ return null; | ||
this.enableValueAccessors = function () { | ||
if (_this2.canUseAccessors) { | ||
var input = _this2.getInputDOMNode(); | ||
_this2.valueDescriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(input), 'value'); | ||
Object.defineProperty(input, 'value', { | ||
configurable: true, | ||
enumerable: true, | ||
get: function get() { | ||
return _this2.value; | ||
}, | ||
set: function set(value) { | ||
_this2.value = value; | ||
_this2.valueDescriptor.set.call(input, value); | ||
} | ||
}); | ||
} | ||
}; | ||
this.disableValueAccessors = function () { | ||
var valueDescriptor = _this2.valueDescriptor; | ||
var input = _this2.getInputDOMNode(); | ||
if (!valueDescriptor || !input) { | ||
return; | ||
} | ||
_this2.valueDescriptor = null; | ||
Object.defineProperty(input, 'value', valueDescriptor); | ||
}; | ||
this.getInputValue = function () { | ||
var input = _this2.getInputDOMNode(); | ||
var valueDescriptor = _this2.valueDescriptor; | ||
var value; | ||
if (valueDescriptor) { | ||
value = valueDescriptor.get.call(input); | ||
} else { | ||
value = input.value; | ||
if (!input) { | ||
return null; | ||
} | ||
return value; | ||
return input.value; | ||
}; | ||
@@ -550,3 +495,3 @@ | ||
this.setCursorToEnd = function () { | ||
var filledLen = getFilledLength(_this2.maskOptions, _this2.state.value); | ||
var filledLen = getFilledLength(_this2.maskOptions, _this2.value); | ||
var pos = _this2.getRightEditablePos(filledLen); | ||
@@ -607,8 +552,5 @@ if (pos !== null) { | ||
this.setCursorPos = function (pos) { | ||
var raf = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (fn) { | ||
return setTimeout(fn, 0); | ||
}; | ||
_this2.setSelection(pos, 0); | ||
_this2.setSelection(pos, 0); | ||
raf(function () { | ||
defer(function () { | ||
_this2.setSelection(pos, 0); | ||
@@ -628,94 +570,35 @@ }); | ||
this.updateUncontrolledInput = function () { | ||
if (_this2.getInputValue() !== _this2.state.value) { | ||
_this2.setInputValue(_this2.state.value); | ||
this.onKeyDown = function (event) { | ||
_this2.backspaceOrDeleteRemoval = null; | ||
if (typeof _this2.props.onKeyDown === 'function') { | ||
_this2.props.onKeyDown(event); | ||
} | ||
}; | ||
this.onKeyDown = function (event) { | ||
var key = event.key, | ||
ctrlKey = event.ctrlKey, | ||
metaKey = event.metaKey; | ||
metaKey = event.metaKey, | ||
defaultPrevented = event.defaultPrevented; | ||
var hasHandler = typeof _this2.props.onKeyDown === 'function'; | ||
if (ctrlKey || metaKey) { | ||
if (hasHandler) { | ||
_this2.props.onKeyDown(event); | ||
} | ||
if (ctrlKey || metaKey || defaultPrevented) { | ||
return; | ||
} | ||
var cursorPos = _this2.getCursorPos(); | ||
var value = _this2.state.value; | ||
var prefix = _this2.maskOptions.prefix; | ||
if (key === 'Backspace' || key === 'Delete') { | ||
_this2.backspaceOrDeleteRemoval = { | ||
key: key, | ||
selection: _this2.getSelection() | ||
}; | ||
var preventDefault = false; | ||
switch (key) { | ||
case 'Backspace': | ||
case 'Delete': | ||
var deleteFromRight = key === 'Delete'; | ||
var selectionRange = _this2.getSelection(); | ||
if (selectionRange.length) { | ||
value = clearRange(_this2.maskOptions, value, selectionRange.start, selectionRange.length); | ||
} else if (cursorPos < prefix.length || !deleteFromRight && cursorPos === prefix.length) { | ||
cursorPos = prefix.length; | ||
} else { | ||
var editablePos = deleteFromRight ? _this2.getRightEditablePos(cursorPos) : _this2.getLeftEditablePos(cursorPos - 1); | ||
if (editablePos !== null) { | ||
value = clearRange(_this2.maskOptions, value, editablePos, 1); | ||
cursorPos = editablePos; | ||
} | ||
} | ||
preventDefault = true; | ||
break; | ||
default: | ||
break; | ||
} | ||
if (hasHandler) { | ||
_this2.props.onKeyDown(event); | ||
} | ||
if (value !== _this2.state.value) { | ||
preventDefault = true; | ||
_this2.setInputValue(value); | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : value | ||
defer(function () { | ||
_this2.backspaceOrDeleteRemoval = null; | ||
}); | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
} | ||
if (preventDefault) { | ||
event.preventDefault(); | ||
_this2.setCursorPos(cursorPos); | ||
} | ||
}; | ||
this.onKeyPress = function (event) { | ||
var key = event.key, | ||
ctrlKey = event.ctrlKey, | ||
metaKey = event.metaKey; | ||
var hasHandler = typeof _this2.props.onKeyPress === 'function'; | ||
if (key === 'Enter' || ctrlKey || metaKey) { | ||
if (hasHandler) { | ||
_this2.props.onKeyPress(event); | ||
} | ||
return; | ||
} | ||
if (_this2.isWindowsPhoneBrowser) { | ||
return; | ||
} | ||
var cursorPos = _this2.getCursorPos(); | ||
var selection = _this2.getSelection(); | ||
var value = _this2.state.value; | ||
this.onChange = function (event) { | ||
var paste = _this2.paste; | ||
var _maskOptions = _this2.maskOptions, | ||
mask = _maskOptions.mask, | ||
maskChar = _maskOptions.maskChar, | ||
lastEditablePos = _maskOptions.lastEditablePos, | ||
@@ -725,53 +608,11 @@ prefix = _maskOptions.prefix; | ||
if (isPermanentChar(_this2.maskOptions, cursorPos) && mask[cursorPos] === key) { | ||
value = insertString(_this2.maskOptions, value, key, cursorPos); | ||
++cursorPos; | ||
} else { | ||
var editablePos = _this2.getRightEditablePos(cursorPos); | ||
if (editablePos !== null && isAllowedChar(_this2.maskOptions, editablePos, key)) { | ||
value = clearRange(_this2.maskOptions, value, selection.start, selection.length); | ||
value = insertString(_this2.maskOptions, value, key, editablePos); | ||
cursorPos = editablePos + 1; | ||
} | ||
} | ||
var value = _this2.getInputValue(); | ||
var oldValue = _this2.value; | ||
if (value !== _this2.state.value) { | ||
_this2.setInputValue(value); | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : value | ||
}); | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
if (paste) { | ||
_this2.paste = null; | ||
_this2.pasteText(paste.value, value, paste.selection, event); | ||
return; | ||
} | ||
event.preventDefault(); | ||
if (cursorPos < lastEditablePos && cursorPos > prefix.length) { | ||
cursorPos = _this2.getRightEditablePos(cursorPos); | ||
} | ||
_this2.setCursorPos(cursorPos); | ||
}; | ||
this.onChange = function (event) { | ||
var pasteSelection = _this2.pasteSelection; | ||
var _maskOptions2 = _this2.maskOptions, | ||
mask = _maskOptions2.mask, | ||
maskChar = _maskOptions2.maskChar, | ||
lastEditablePos = _maskOptions2.lastEditablePos, | ||
prefix = _maskOptions2.prefix; | ||
var value = _this2.getInputValue(); | ||
if (!value && _this2.preventEmptyChange) { | ||
_this2.disableValueAccessors(); | ||
_this2.preventEmptyChange = false; | ||
_this2.setInputValue(_this2.state.value); | ||
return; | ||
} | ||
var oldValue = _this2.state.value; | ||
if (pasteSelection) { | ||
_this2.pasteSelection = null; | ||
_this2.pasteText(oldValue, value, pasteSelection, event); | ||
return; | ||
} | ||
var selection = _this2.getSelection(); | ||
@@ -786,3 +627,23 @@ var cursorPos = selection.end; | ||
if (valueLen > oldValueLen) { | ||
if (_this2.backspaceOrDeleteRemoval) { | ||
var deleteFromRight = _this2.backspaceOrDeleteRemoval.key === 'Delete'; | ||
value = _this2.value; | ||
selection = _this2.backspaceOrDeleteRemoval.selection; | ||
cursorPos = selection.start; | ||
_this2.backspaceOrDeleteRemoval = null; | ||
if (selection.length) { | ||
value = clearRange(_this2.maskOptions, value, selection.start, selection.length); | ||
} else if (selection.start < prefix.length || !deleteFromRight && selection.start === prefix.length) { | ||
cursorPos = prefix.length; | ||
} else { | ||
var editablePos = deleteFromRight ? _this2.getRightEditablePos(cursorPos) : _this2.getLeftEditablePos(cursorPos - 1); | ||
if (editablePos !== null) { | ||
value = clearRange(_this2.maskOptions, value, editablePos, 1); | ||
cursorPos = editablePos; | ||
} | ||
} | ||
} else if (valueLen > oldValueLen) { | ||
var enteredStringLen = valueLen - oldValueLen; | ||
@@ -806,3 +667,6 @@ var startPos = selection.end - enteredStringLen; | ||
if (enteredStringLen !== 1 || cursorPos >= prefix.length && cursorPos < lastEditablePos) { | ||
cursorPos = getFilledLength(_this2.maskOptions, clearedValue); | ||
cursorPos = Math.max(getFilledLength(_this2.maskOptions, clearedValue), cursorPos); | ||
if (cursorPos < lastEditablePos) { | ||
cursorPos = _this2.getRightEditablePos(cursorPos); | ||
} | ||
} else if (cursorPos < lastEditablePos) { | ||
@@ -826,3 +690,6 @@ cursorPos++; | ||
if (!clearOnly) { | ||
cursorPos = getFilledLength(_this2.maskOptions, clearedValue); | ||
cursorPos = Math.max(getFilledLength(_this2.maskOptions, clearedValue), cursorPos); | ||
if (cursorPos < lastEditablePos) { | ||
cursorPos = _this2.getRightEditablePos(cursorPos); | ||
} | ||
} else if (cursorPos < prefix.length) { | ||
@@ -834,49 +701,13 @@ cursorPos = prefix.length; | ||
if (_this2.isWindowsPhoneBrowser) { | ||
event.persist(); | ||
setTimeout(function () { | ||
if (_this2.unmounted) { | ||
return; | ||
} | ||
_this2.setInputValue(value); | ||
_this2.setInputValue(value); | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
if (!_this2.hasValue) { | ||
_this2.setState({ | ||
value: value | ||
}); | ||
} | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
_this2.setCursorPos(cursorPos); | ||
}, 0); | ||
if (_this2.isWindowsPhoneBrowser) { | ||
defer(function () { | ||
_this2.setSelection(cursorPos, 0); | ||
}); | ||
} else { | ||
// prevent android autocomplete insertion on backspace | ||
if (!_this2.canUseAccessors || !_this2.isAndroidBrowser) { | ||
_this2.setInputValue(value); | ||
} | ||
if (_this2.canUseAccessors && (_this2.isAndroidFirefox && value && !_this2.getInputValue() || _this2.isAndroidBrowser)) { | ||
_this2.value = value; | ||
_this2.enableValueAccessors(); | ||
if (_this2.isAndroidFirefox) { | ||
_this2.preventEmptyChange = true; | ||
} | ||
setTimeout(function () { | ||
_this2.preventEmptyChange = false; | ||
_this2.disableValueAccessors(); | ||
}, 0); | ||
} | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : value | ||
}); | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
_this2.setCursorPos(cursorPos); | ||
@@ -887,3 +718,3 @@ } | ||
this.onFocus = function (event) { | ||
if (!_this2.state.value) { | ||
if (!_this2.value) { | ||
var prefix = _this2.maskOptions.prefix; | ||
@@ -901,5 +732,3 @@ var value = formatValue(_this2.maskOptions, prefix); | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : inputValue | ||
}, _this2.setCursorToEnd); | ||
_this2.value = inputValue; | ||
@@ -909,4 +738,6 @@ if (isInputValueChanged && typeof _this2.props.onChange === 'function') { | ||
} | ||
} else if (getFilledLength(_this2.maskOptions, _this2.state.value) < _this2.maskOptions.mask.length) { | ||
_this2.setCursorToEnd(); | ||
} else if (getFilledLength(_this2.maskOptions, _this2.value) < _this2.maskOptions.mask.length) { | ||
_this2.setCursorToEnd(); | ||
} | ||
@@ -920,3 +751,3 @@ | ||
this.onBlur = function (event) { | ||
if (!_this2.props.alwaysShowMask && isEmpty(_this2.maskOptions, _this2.state.value)) { | ||
if (!_this2.props.alwaysShowMask && isEmpty(_this2.maskOptions, _this2.value)) { | ||
var inputValue = ''; | ||
@@ -929,6 +760,2 @@ var isInputValueChanged = inputValue !== _this2.getInputValue(); | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : '' | ||
}); | ||
if (isInputValueChanged && typeof _this2.props.onChange === 'function') { | ||
@@ -945,23 +772,13 @@ _this2.props.onChange(event); | ||
this.onPaste = function (event) { | ||
if (_this2.isAndroidBrowser) { | ||
_this2.pasteSelection = _this2.getSelection(); | ||
_this2.setInputValue(''); | ||
return; | ||
if (typeof _this2.props.onPaste === 'function') { | ||
_this2.props.onPaste(event); | ||
} | ||
var text; | ||
if (window.clipboardData && window.clipboardData.getData) { | ||
// IE | ||
text = window.clipboardData.getData('Text'); | ||
} else if (event.clipboardData && event.clipboardData.getData) { | ||
text = event.clipboardData.getData('text/plain'); | ||
if (_this2.isAndroidBrowser && !event.defaultPrevented) { | ||
_this2.paste = { | ||
value: _this2.getInputValue(), | ||
selection: _this2.getSelection() | ||
}; | ||
_this2.setInputValue(''); | ||
} | ||
if (text) { | ||
var value = _this2.state.value; | ||
var selection = _this2.getSelection(); | ||
_this2.pasteText(value, text, selection, event); | ||
} | ||
event.preventDefault(); | ||
}; | ||
@@ -980,8 +797,3 @@ | ||
if (value !== _this2.getInputValue()) { | ||
if (event) { | ||
_this2.setInputValue(value); | ||
} | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : value | ||
}); | ||
_this2.setInputValue(value); | ||
if (event && typeof _this2.props.onChange === 'function') { | ||
@@ -1005,3 +817,3 @@ _this2.props.onChange(event); | ||
if (!props.disabled && !props.readOnly) { | ||
var handlersKeys = ['onFocus', 'onBlur', 'onChange', 'onKeyDown', 'onKeyPress', 'onPaste']; | ||
var handlersKeys = ['onFocus', 'onBlur', 'onChange', 'onKeyDown', 'onPaste']; | ||
handlersKeys.forEach(function (key) { | ||
@@ -1011,6 +823,2 @@ props[key] = _this2[key]; | ||
} | ||
if (props.value != null) { | ||
props.value = _this2.state.value; | ||
} | ||
} | ||
@@ -1017,0 +825,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):e.ReactInputMask=t(e.React)}(this,function(e){"use strict";function t(){var e=new RegExp("windows","i"),t=new RegExp("firefox","i"),n=new RegExp("android","i"),s=navigator.userAgent;return!e.test(s)&&!t.test(s)&&n.test(s)}function n(){var e=new RegExp("windows","i"),t=new RegExp("phone","i"),n=navigator.userAgent;return e.test(n)&&t.test(n)}function s(){var e=new RegExp("windows","i"),t=new RegExp("firefox","i"),n=new RegExp("android","i"),s=navigator.userAgent;return!e.test(s)&&t.test(s)&&n.test(s)}function a(e,t){return-1!==e.permanents.indexOf(t)}function o(e,t,n){var s=e.mask,o=e.charsRules;if(!n)return!1;if(a(e,t))return s[t]===n;var r=o[s[t]];return new RegExp(r).test(n)}function r(e,t){return t.split("").every(function(t,n){return a(e,n)||!o(e,n,t)})}function i(e,t){var n=e.maskChar,s=e.prefix;if(!n)return t.length;for(var r=s.length,i=t.length;i>=s.length;i--){var u=t[i];if(!a(e,i)&&o(e,i,u)){r=i+1;break}}return r}function u(e,t){return i(e,t)===e.mask.length}function l(e,t){var n=e.maskChar,s=e.mask,o=e.prefix;if(!n)return t=c(e,"",t,0),(t=t.slice(0,i(e,t))).length<o.length&&(t=o),t;if(t)return c(e,l(e,""),t,0);for(var r=0;r<s.length;r++)a(e,r)?t+=s[r]:t+=n;return t}function p(e,t,n,s){var o=n+s,r=e.maskChar,i=e.mask,u=e.prefix,p=t.split("");return r?p.map(function(t,s){return s<n||s>=o?t:a(e,s)?i[s]:r}).join(""):(n=Math.max(u.length,n),p.splice(n,o-n),t=p.join(""),l(e,t))}function c(e,t,n,s){var r=e.mask,i=e.maskChar,p=e.prefix,c=n.split(""),f=u(e,t),h=function(t,n){return!a(e,t)||n===r[t]},g=function(t,n){return!i||!a(e,n)||t!==i};return!i&&s>t.length&&(t+=r.slice(t.length,s)),c.every(function(n){for(;!h(s,n);){if(s>=t.length&&(t+=r[s]),!g(n,s))return!0;if(++s>=r.length)return!1}return!o(e,s,n)&&n!==i||(s<t.length?i||f||s<p.length?t=t.slice(0,s)+n+t.slice(s+1):(t=t.slice(0,s)+n+t.slice(s),t=l(e,t)):i||(t+=n),++s<r.length)}),t}function f(e,t,n,s){var r=e.mask,i=e.maskChar,u=n.split(""),l=s,p=function(t,n){return!a(e,t)||n===r[t]};return u.every(function(t){for(;!p(s,t);)if(++s>=r.length)return!1;return(o(e,s,t)||t===i)&&s++,s<r.length}),s-l}function h(e,t){var n={};for(var s in e)t.indexOf(s)>=0||Object.prototype.hasOwnProperty.call(e,s)&&(n[s]=e[s]);return n}function g(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function m(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function v(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}e="default"in e?e["default"]:e;var d={9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},k=function(e,t,n){if(t===undefined&&(t="_"),null==n&&(n=d),!e||"string"!=typeof e)return{maskChar:t,charsRules:n,mask:null,prefix:null,lastEditablePos:null,permanents:[]};var s="",a="",o=[],r=!1,i=null;return e.split("").forEach(function(e,t){r||"\\"!==e?(r||!n[e]?(o.push(s.length),a.length===t&&(a+=e)):i=s.length+1,s+=e,r=!1):r=!0}),{maskChar:t,charsRules:n,prefix:a,mask:s,lastEditablePos:i,permanents:o}},O=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var s in n)Object.prototype.hasOwnProperty.call(n,s)&&(e[s]=n[s])}return e},y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},b=function(){function e(e,t){for(var n=0;n<t.length;n++){var s=t[n];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(e,s.key,s)}}return function(t,n,s){return n&&e(t.prototype,n),s&&e(t,s),t}}(),w=function(e){function t(e){g(this,t);var n=m(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));C.call(n);var s=e.mask,a=e.maskChar,o=e.formatChars,r=e.defaultValue,i=e.value,u=e.alwaysShowMask;return n.hasValue=null!=i,n.maskOptions=k(s,a,o),null==r&&(r=""),null==i&&(i=r),i=n.getStringValue(i),n.maskOptions.mask&&(u||i)&&(i=l(n.maskOptions,i)),n.state={value:i},n}return v(t,e),b(t,[{key:"componentWillUnmount",value:function(){this.unmounted=!0}}]),t}(e.Component),C=function(){var g=this;this.lastCursorPos=null,this.componentDidMount=function(){g.isAndroidBrowser=t(),g.isWindowsPhoneBrowser=n(),g.isAndroidFirefox=s();var e=g.getInputDOMNode();if(e&&Object.getOwnPropertyDescriptor&&Object.getPrototypeOf&&Object.defineProperty){var a=Object.getOwnPropertyDescriptor(Object.getPrototypeOf(e),"value");g.canUseAccessors=!!(a&&a.get&&a.set)}g.maskOptions.mask&&null==g.props.value&&g.updateUncontrolledInput()},this.componentWillReceiveProps=function(e){var t=g.maskOptions;if(g.hasValue=null!=e.value,g.maskOptions=k(e.mask,e.maskChar,e.formatChars),g.maskOptions.mask){var n=g.maskOptions.mask&&g.maskOptions.mask!==t.mask,s=e.alwaysShowMask||g.isFocused(),a=g.hasValue?g.getStringValue(e.value):g.state.value;if(t.mask||g.hasValue||(a=g.getInputDOMNode().value),(n||g.maskOptions.mask&&(a||s))&&(a=l(g.maskOptions,a),n)){var o=g.lastCursorPos,p=i(g.maskOptions,a);(null===o||p<o)&&(o=u(g.maskOptions,a)?p:g.getRightEditablePos(p),g.setCursorPos(o))}!g.maskOptions.mask||!r(g.maskOptions,a)||s||g.hasValue&&e.value||(a=""),g.value=a,g.state.value!==a&&g.setState({value:a})}else g.lastCursorPos=null},this.componentDidUpdate=function(e){(g.maskOptions.mask||e.mask)&&null==g.props.value&&g.updateUncontrolledInput(),g.valueDescriptor&&g.getInputValue()!==g.state.value&&g.setInputValue(g.state.value)},this.isDOMElement=function(e){return"object"===("undefined"==typeof HTMLElement?"undefined":y(HTMLElement))?e instanceof HTMLElement:1===e.nodeType&&"string"==typeof e.nodeName},this.getInputDOMNode=function(){var t=g.input;return t?g.isDOMElement(t)?t:e.findDOMNode(t):null},this.enableValueAccessors=function(){if(g.canUseAccessors){var e=g.getInputDOMNode();g.valueDescriptor=Object.getOwnPropertyDescriptor(Object.getPrototypeOf(e),"value"),Object.defineProperty(e,"value",{configurable:!0,enumerable:!0,get:function(){return g.value},set:function(t){g.value=t,g.valueDescriptor.set.call(e,t)}})}},this.disableValueAccessors=function(){var e=g.valueDescriptor,t=g.getInputDOMNode();e&&t&&(g.valueDescriptor=null,Object.defineProperty(t,"value",e))},this.getInputValue=function(){var e=g.getInputDOMNode(),t=g.valueDescriptor;return t?t.get.call(e):e.value},this.setInputValue=function(e){var t=g.getInputDOMNode();t&&(g.value=e,t.value=e)},this.getLeftEditablePos=function(e){for(var t=e;t>=0;--t)if(!a(g.maskOptions,t))return t;return null},this.getRightEditablePos=function(e){for(var t=g.maskOptions.mask,n=e;n<t.length;++n)if(!a(g.maskOptions,n))return n;return null},this.setCursorToEnd=function(){var e=i(g.maskOptions,g.state.value),t=g.getRightEditablePos(e);null!==t&&g.setCursorPos(t)},this.setSelection=function(e){var t=arguments.length>1&&arguments[1]!==undefined?arguments[1]:0,n=g.getInputDOMNode();if(n){var s=e+t;if("selectionStart"in n&&"selectionEnd"in n)n.selectionStart=e,n.selectionEnd=s;else{var a=n.createTextRange();a.collapse(!0),a.moveStart("character",e),a.moveEnd("character",s-e),a.select()}}},this.getSelection=function(){var e=g.getInputDOMNode(),t=0,n=0;if("selectionStart"in e&&"selectionEnd"in e)t=e.selectionStart,n=e.selectionEnd;else{var s=document.selection.createRange();s.parentElement()===e&&(t=-s.moveStart("character",-e.value.length),n=-s.moveEnd("character",-e.value.length))}return{start:t,end:n,length:n-t}},this.getCursorPos=function(){return g.getSelection().start},this.setCursorPos=function(e){var t=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){return setTimeout(e,0)};g.setSelection(e,0),t(function(){g.setSelection(e,0)}),g.lastCursorPos=e},this.isFocused=function(){return document.activeElement===g.getInputDOMNode()},this.getStringValue=function(e){return e||0===e?e+"":""},this.updateUncontrolledInput=function(){g.getInputValue()!==g.state.value&&g.setInputValue(g.state.value)},this.onKeyDown=function(e){var t=e.key,n=e.ctrlKey,s=e.metaKey,a="function"==typeof g.props.onKeyDown;if(n||s)a&&g.props.onKeyDown(e);else{var o=g.getCursorPos(),r=g.state.value,i=g.maskOptions.prefix,u=!1;switch(t){case"Backspace":case"Delete":var l="Delete"===t,c=g.getSelection();if(c.length)r=p(g.maskOptions,r,c.start,c.length);else if(o<i.length||!l&&o===i.length)o=i.length;else{var f=l?g.getRightEditablePos(o):g.getLeftEditablePos(o-1);null!==f&&(r=p(g.maskOptions,r,f,1),o=f)}u=!0}a&&g.props.onKeyDown(e),r!==g.state.value&&(u=!0,g.setInputValue(r),g.setState({value:g.hasValue?g.state.value:r}),"function"==typeof g.props.onChange&&g.props.onChange(e)),u&&(e.preventDefault(),g.setCursorPos(o))}},this.onKeyPress=function(e){var t=e.key,n=e.ctrlKey,s=e.metaKey,r="function"==typeof g.props.onKeyPress;if("Enter"===t||n||s)r&&g.props.onKeyPress(e);else if(!g.isWindowsPhoneBrowser){var i=g.getCursorPos(),u=g.getSelection(),l=g.state.value,f=g.maskOptions,h=f.mask,m=f.lastEditablePos,v=f.prefix;if(a(g.maskOptions,i)&&h[i]===t)l=c(g.maskOptions,l,t,i),++i;else{var d=g.getRightEditablePos(i);null!==d&&o(g.maskOptions,d,t)&&(l=p(g.maskOptions,l,u.start,u.length),l=c(g.maskOptions,l,t,d),i=d+1)}l!==g.state.value&&(g.setInputValue(l),g.setState({value:g.hasValue?g.state.value:l}),"function"==typeof g.props.onChange&&g.props.onChange(e)),e.preventDefault(),i<m&&i>v.length&&(i=g.getRightEditablePos(i)),g.setCursorPos(i)}},this.onChange=function(e){var t=g.pasteSelection,n=g.maskOptions,s=n.mask,a=n.maskChar,o=n.lastEditablePos,r=n.prefix,u=g.getInputValue();if(!u&&g.preventEmptyChange)return g.disableValueAccessors(),g.preventEmptyChange=!1,void g.setInputValue(g.state.value);var f=g.state.value;if(t)return g.pasteSelection=null,void g.pasteText(f,u,t,e);var h,m,v=g.getSelection(),d=v.end,k=s.length,O=u.length,y=f.length;if(O>y){var b=O-y,w=v.end-b;m=u.substr(w,b),d=w<o&&(1!==b||m!==s[w])?g.getRightEditablePos(w):w,u=u.substr(0,w)+u.substr(w+b),h=p(g.maskOptions,u,w,k-w),h=c(g.maskOptions,h,m,d),u=c(g.maskOptions,f,m,d),1!==b||d>=r.length&&d<o?d=i(g.maskOptions,h):d<o&&d++}else if(O<y){var C=k-O,P=(m=u.substr(0,v.end))===f.substr(0,v.end);h=p(g.maskOptions,f,v.end,C),a&&(u=c(g.maskOptions,h,m,0)),h=p(g.maskOptions,h,v.end,k-v.end),h=c(g.maskOptions,h,m,0),P?d<r.length&&(d=r.length):d=i(g.maskOptions,h)}u=l(g.maskOptions,u),g.isWindowsPhoneBrowser?(e.persist(),setTimeout(function(){g.unmounted||(g.setInputValue(u),g.hasValue||g.setState({value:u}),"function"==typeof g.props.onChange&&g.props.onChange(e),g.setCursorPos(d))},0)):(g.canUseAccessors&&g.isAndroidBrowser||g.setInputValue(u),g.canUseAccessors&&(g.isAndroidFirefox&&u&&!g.getInputValue()||g.isAndroidBrowser)&&(g.value=u,g.enableValueAccessors(),g.isAndroidFirefox&&(g.preventEmptyChange=!0),setTimeout(function(){g.preventEmptyChange=!1,g.disableValueAccessors()},0)),g.setState({value:g.hasValue?g.state.value:u}),"function"==typeof g.props.onChange&&g.props.onChange(e),g.setCursorPos(d))},this.onFocus=function(e){if(g.state.value)i(g.maskOptions,g.state.value)<g.maskOptions.mask.length&&g.setCursorToEnd();else{var t=g.maskOptions.prefix,n=l(g.maskOptions,t),s=l(g.maskOptions,n),a=s!==e.target.value;a&&(e.target.value=s),g.setState({value:g.hasValue?g.state.value:s},g.setCursorToEnd),a&&"function"==typeof g.props.onChange&&g.props.onChange(e)}"function"==typeof g.props.onFocus&&g.props.onFocus(e)},this.onBlur=function(e){if(!g.props.alwaysShowMask&&r(g.maskOptions,g.state.value)){var t=""!==g.getInputValue();t&&g.setInputValue(""),g.setState({value:g.hasValue?g.state.value:""}),t&&"function"==typeof g.props.onChange&&g.props.onChange(e)}"function"==typeof g.props.onBlur&&g.props.onBlur(e)},this.onPaste=function(e){if(g.isAndroidBrowser)return g.pasteSelection=g.getSelection(),void g.setInputValue("");var t;if(window.clipboardData&&window.clipboardData.getData?t=window.clipboardData.getData("Text"):e.clipboardData&&e.clipboardData.getData&&(t=e.clipboardData.getData("text/plain")),t){var n=g.state.value,s=g.getSelection();g.pasteText(n,t,s,e)}e.preventDefault()},this.pasteText=function(e,t,n,s){var a=n.start;n.length&&(e=p(g.maskOptions,e,a,n.length));var o=f(g.maskOptions,e,t,a);e=c(g.maskOptions,e,t,a),a+=o,a=g.getRightEditablePos(a)||a,e!==g.getInputValue()&&(s&&g.setInputValue(e),g.setState({value:g.hasValue?g.state.value:e}),s&&"function"==typeof g.props.onChange&&g.props.onChange(s)),g.setCursorPos(a)},this.render=function(){var t=g.props,n=(t.mask,t.alwaysShowMask,t.maskChar,t.formatChars,h(t,["mask","alwaysShowMask","maskChar","formatChars"]));return g.maskOptions.mask&&(n.disabled||n.readOnly||["onFocus","onBlur","onChange","onKeyDown","onKeyPress","onPaste"].forEach(function(e){n[e]=g[e]}),null!=n.value&&(n.value=g.state.value)),e.createElement("input",O({ref:function(e){return g.input=e}},n))}};return w}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):e.ReactInputMask=t(e.React)}(this,function(e){"use strict";function t(){var e=new RegExp("windows","i"),t=new RegExp("firefox","i"),n=new RegExp("android","i"),o=navigator.userAgent;return!e.test(o)&&!t.test(o)&&n.test(o)}function n(){var e=new RegExp("windows","i"),t=new RegExp("phone","i"),n=navigator.userAgent;return e.test(n)&&t.test(n)}function o(){var e=new RegExp("windows","i"),t=new RegExp("firefox","i"),n=new RegExp("android","i"),o=navigator.userAgent;return!e.test(o)&&t.test(o)&&n.test(o)}function s(e,t){return-1!==e.permanents.indexOf(t)}function a(e,t,n){var o=e.mask,a=e.charsRules;if(!n)return!1;if(s(e,t))return o[t]===n;var r=a[o[t]];return new RegExp(r).test(n)}function r(e,t){return t.split("").every(function(t,n){return s(e,n)||!a(e,n,t)})}function i(e,t){var n=e.maskChar,o=e.prefix;if(!n)return t.length;for(var r=o.length,i=t.length;i>=o.length;i--){var u=t[i];if(!s(e,i)&&a(e,i,u)){r=i+1;break}}return r}function u(e,t){return i(e,t)===e.mask.length}function l(e,t){var n=e.maskChar,o=e.mask,a=e.prefix;if(!n)return t=c(e,"",t,0),(t=t.slice(0,i(e,t))).length<a.length&&(t=a),t;if(t)return c(e,l(e,""),t,0);for(var r=0;r<o.length;r++)s(e,r)?t+=o[r]:t+=n;return t}function p(e,t,n,o){var a=n+o,r=e.maskChar,i=e.mask,u=e.prefix,p=t.split("");return r?p.map(function(t,o){return o<n||o>=a?t:s(e,o)?i[o]:r}).join(""):(n=Math.max(u.length,n),p.splice(n,a-n),t=p.join(""),l(e,t))}function c(e,t,n,o){var r=e.mask,i=e.maskChar,p=e.prefix,c=n.split(""),f=u(e,t),h=function(t,n){return!s(e,t)||n===r[t]},m=function(t,n){return!i||!s(e,n)||t!==i};return!i&&o>t.length&&(t+=r.slice(t.length,o)),c.every(function(n){for(;!h(o,n);){if(o>=t.length&&(t+=r[o]),!m(n,o))return!0;if(++o>=r.length)return!1}return!a(e,o,n)&&n!==i||(o<t.length?i||f||o<p.length?t=t.slice(0,o)+n+t.slice(o+1):(t=t.slice(0,o)+n+t.slice(o),t=l(e,t)):i||(t+=n),++o<r.length)}),t}function f(e,t,n,o){var r=e.mask,i=e.maskChar,u=n.split(""),l=o,p=function(t,n){return!s(e,t)||n===r[t]};return u.every(function(t){for(;!p(o,t);)if(++o>=r.length)return!1;return(a(e,o,t)||t===i)&&o++,o<r.length}),o-l}function h(e,t){var n={};for(var o in e)t.indexOf(o)>=0||Object.prototype.hasOwnProperty.call(e,o)&&(n[o]=e[o]);return n}function m(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function g(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function v(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}e="default"in e?e["default"]:e;var d={9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},k=function(e,t,n){if(t===undefined&&(t="_"),null==n&&(n=d),!e||"string"!=typeof e)return{maskChar:t,charsRules:n,mask:null,prefix:null,lastEditablePos:null,permanents:[]};var o="",s="",a=[],r=!1,i=null;return e.split("").forEach(function(e,t){r||"\\"!==e?(r||!n[e]?(a.push(o.length),s.length===t&&(s+=e)):i=o.length+1,o+=e,r=!1):r=!0}),{maskChar:t,charsRules:n,prefix:s,mask:o,lastEditablePos:i,permanents:a}},O=function(e){return(window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){return setTimeout(e,0)})(e)},y=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},b="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},w=function(e){function t(e){m(this,t);var n=g(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));E.call(n);var o=e.mask,s=e.maskChar,a=e.formatChars,r=e.defaultValue,i=e.value,u=e.alwaysShowMask;return n.hasValue=null!=i,n.maskOptions=k(o,s,a),null==r&&(r=""),null==i&&(i=r),i=n.getStringValue(i),n.maskOptions.mask&&(u||i)&&(i=l(n.maskOptions,i)),n.value=i,n}return v(t,e),t}(e.Component),E=function(){var a=this;this.lastCursorPos=null,this.componentDidMount=function(){a.isAndroidBrowser=t(),a.isWindowsPhoneBrowser=n(),a.isAndroidFirefox=o(),a.getInputValue()!==a.value&&a.setInputValue(a.value)},this.componentWillReceiveProps=function(e){var t=a.maskOptions;if(a.hasValue=null!=e.value,a.maskOptions=k(e.mask,e.maskChar,e.formatChars),a.maskOptions.mask){var n=a.maskOptions.mask&&a.maskOptions.mask!==t.mask,o=e.alwaysShowMask||a.isFocused(),s=a.hasValue?a.getStringValue(e.value):a.value;if(t.mask||a.hasValue||(s=a.getInputDOMNode().value),(n||a.maskOptions.mask&&(s||o))&&(s=l(a.maskOptions,s),n)){var p=a.lastCursorPos,c=i(a.maskOptions,s);(null===p||c<p)&&(p=u(a.maskOptions,s)?c:a.getRightEditablePos(c),a.setCursorPos(p))}!a.maskOptions.mask||!r(a.maskOptions,s)||o||a.hasValue&&e.value||(s=""),a.value=s}else a.lastCursorPos=null},this.componentDidUpdate=function(){a.getInputValue()!==a.value&&a.setInputValue(a.value)},this.isDOMElement=function(e){return"object"===("undefined"==typeof HTMLElement?"undefined":b(HTMLElement))?e instanceof HTMLElement:1===e.nodeType&&"string"==typeof e.nodeName},this.getInputDOMNode=function(){var t=a.input;return t?a.isDOMElement(t)?t:e.findDOMNode(t):null},this.getInputValue=function(){var e=a.getInputDOMNode();return e?e.value:null},this.setInputValue=function(e){var t=a.getInputDOMNode();t&&(a.value=e,t.value=e)},this.getLeftEditablePos=function(e){for(var t=e;t>=0;--t)if(!s(a.maskOptions,t))return t;return null},this.getRightEditablePos=function(e){for(var t=a.maskOptions.mask,n=e;n<t.length;++n)if(!s(a.maskOptions,n))return n;return null},this.setCursorToEnd=function(){var e=i(a.maskOptions,a.value),t=a.getRightEditablePos(e);null!==t&&a.setCursorPos(t)},this.setSelection=function(e){var t=arguments.length>1&&arguments[1]!==undefined?arguments[1]:0,n=a.getInputDOMNode();if(n){var o=e+t;if("selectionStart"in n&&"selectionEnd"in n)n.selectionStart=e,n.selectionEnd=o;else{var s=n.createTextRange();s.collapse(!0),s.moveStart("character",e),s.moveEnd("character",o-e),s.select()}}},this.getSelection=function(){var e=a.getInputDOMNode(),t=0,n=0;if("selectionStart"in e&&"selectionEnd"in e)t=e.selectionStart,n=e.selectionEnd;else{var o=document.selection.createRange();o.parentElement()===e&&(t=-o.moveStart("character",-e.value.length),n=-o.moveEnd("character",-e.value.length))}return{start:t,end:n,length:n-t}},this.getCursorPos=function(){return a.getSelection().start},this.setCursorPos=function(e){a.setSelection(e,0),O(function(){a.setSelection(e,0)}),a.lastCursorPos=e},this.isFocused=function(){return document.activeElement===a.getInputDOMNode()},this.getStringValue=function(e){return e||0===e?e+"":""},this.onKeyDown=function(e){a.backspaceOrDeleteRemoval=null,"function"==typeof a.props.onKeyDown&&a.props.onKeyDown(e);var t=e.key,n=e.ctrlKey,o=e.metaKey,s=e.defaultPrevented;n||o||s||"Backspace"!==t&&"Delete"!==t||(a.backspaceOrDeleteRemoval={key:t,selection:a.getSelection()},O(function(){a.backspaceOrDeleteRemoval=null}))},this.onChange=function(e){var t=a.paste,n=a.maskOptions,o=n.mask,s=n.maskChar,r=n.lastEditablePos,u=n.prefix,f=a.getInputValue(),h=a.value;if(t)return a.paste=null,void a.pasteText(t.value,f,t.selection,e);var m,g,v=a.getSelection(),d=v.end,k=o.length,y=f.length,b=h.length;if(a.backspaceOrDeleteRemoval){var w="Delete"===a.backspaceOrDeleteRemoval.key;if(f=a.value,v=a.backspaceOrDeleteRemoval.selection,d=v.start,a.backspaceOrDeleteRemoval=null,v.length)f=p(a.maskOptions,f,v.start,v.length);else if(v.start<u.length||!w&&v.start===u.length)d=u.length;else{var E=w?a.getRightEditablePos(d):a.getLeftEditablePos(d-1);null!==E&&(f=p(a.maskOptions,f,E,1),d=E)}}else if(y>b){var C=y-b,P=v.end-C;g=f.substr(P,C),d=P<r&&(1!==C||g!==o[P])?a.getRightEditablePos(P):P,f=f.substr(0,P)+f.substr(P+C),m=p(a.maskOptions,f,P,k-P),m=c(a.maskOptions,m,g,d),f=c(a.maskOptions,h,g,d),1!==C||d>=u.length&&d<r?(d=Math.max(i(a.maskOptions,m),d))<r&&(d=a.getRightEditablePos(d)):d<r&&d++}else if(y<b){var R=k-y,x=(g=f.substr(0,v.end))===h.substr(0,v.end);m=p(a.maskOptions,h,v.end,R),s&&(f=c(a.maskOptions,m,g,0)),m=p(a.maskOptions,m,v.end,k-v.end),m=c(a.maskOptions,m,g,0),x?d<u.length&&(d=u.length):(d=Math.max(i(a.maskOptions,m),d))<r&&(d=a.getRightEditablePos(d))}f=l(a.maskOptions,f),a.setInputValue(f),"function"==typeof a.props.onChange&&a.props.onChange(e),a.isWindowsPhoneBrowser?O(function(){a.setSelection(d,0)}):a.setCursorPos(d)},this.onFocus=function(e){if(a.value)i(a.maskOptions,a.value)<a.maskOptions.mask.length&&a.setCursorToEnd();else{var t=a.maskOptions.prefix,n=l(a.maskOptions,t),o=l(a.maskOptions,n),s=o!==e.target.value;s&&(e.target.value=o),a.value=o,s&&"function"==typeof a.props.onChange&&a.props.onChange(e),a.setCursorToEnd()}"function"==typeof a.props.onFocus&&a.props.onFocus(e)},this.onBlur=function(e){if(!a.props.alwaysShowMask&&r(a.maskOptions,a.value)){var t=""!==a.getInputValue();t&&a.setInputValue(""),t&&"function"==typeof a.props.onChange&&a.props.onChange(e)}"function"==typeof a.props.onBlur&&a.props.onBlur(e)},this.onPaste=function(e){"function"==typeof a.props.onPaste&&a.props.onPaste(e),a.isAndroidBrowser&&!e.defaultPrevented&&(a.paste={value:a.getInputValue(),selection:a.getSelection()},a.setInputValue(""))},this.pasteText=function(e,t,n,o){var s=n.start;n.length&&(e=p(a.maskOptions,e,s,n.length));var r=f(a.maskOptions,e,t,s);e=c(a.maskOptions,e,t,s),s+=r,s=a.getRightEditablePos(s)||s,e!==a.getInputValue()&&(a.setInputValue(e),o&&"function"==typeof a.props.onChange&&a.props.onChange(o)),a.setCursorPos(s)},this.render=function(){var t=a.props,n=(t.mask,t.alwaysShowMask,t.maskChar,t.formatChars,h(t,["mask","alwaysShowMask","maskChar","formatChars"]));return a.maskOptions.mask&&(n.disabled||n.readOnly||["onFocus","onBlur","onChange","onKeyDown","onPaste"].forEach(function(e){n[e]=a[e]})),e.createElement("input",y({ref:function(e){return a.input=e}},n))}};return w}); |
383
es/index.js
@@ -5,4 +5,2 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
@@ -21,3 +19,4 @@ | ||
import { isAndroidBrowser, isWindowsPhoneBrowser, isAndroidFirefox } from './utils/environment'; | ||
import { clearRange, formatValue, getFilledLength, isFilled, isEmpty, isPermanentChar, isAllowedChar, getInsertStringLength, insertString } from './utils/string'; | ||
import { clearRange, formatValue, getFilledLength, isFilled, isEmpty, isPermanentChar, getInsertStringLength, insertString } from './utils/string'; | ||
import defer from './utils/defer'; | ||
@@ -58,13 +57,6 @@ var InputElement = function (_React$Component) { | ||
_this.state = { value: value }; | ||
_this.value = value; | ||
return _this; | ||
} | ||
_createClass(InputElement, [{ | ||
key: 'componentWillUnmount', | ||
value: function componentWillUnmount() { | ||
this.unmounted = true; | ||
} | ||
}]); | ||
return InputElement; | ||
@@ -83,14 +75,5 @@ }(React.Component); | ||
var input = _this2.getInputDOMNode(); | ||
// workaround for Jest | ||
// it doesn't mount a real node so input will be null | ||
if (input && Object.getOwnPropertyDescriptor && Object.getPrototypeOf && Object.defineProperty) { | ||
var valueDescriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(input), 'value'); | ||
_this2.canUseAccessors = !!(valueDescriptor && valueDescriptor.get && valueDescriptor.set); | ||
if (_this2.getInputValue() !== _this2.value) { | ||
_this2.setInputValue(_this2.value); | ||
} | ||
if (_this2.maskOptions.mask && _this2.props.value == null) { | ||
_this2.updateUncontrolledInput(); | ||
} | ||
}; | ||
@@ -111,3 +94,3 @@ | ||
var showEmpty = nextProps.alwaysShowMask || _this2.isFocused(); | ||
var newValue = _this2.hasValue ? _this2.getStringValue(nextProps.value) : _this2.state.value; | ||
var newValue = _this2.hasValue ? _this2.getStringValue(nextProps.value) : _this2.value; | ||
@@ -140,16 +123,8 @@ if (!oldMaskOptions.mask && !_this2.hasValue) { | ||
_this2.value = newValue; | ||
if (_this2.state.value !== newValue) { | ||
_this2.setState({ value: newValue }); | ||
} | ||
}; | ||
this.componentDidUpdate = function (prevProps) { | ||
if ((_this2.maskOptions.mask || prevProps.mask) && _this2.props.value == null) { | ||
_this2.updateUncontrolledInput(); | ||
this.componentDidUpdate = function () { | ||
if (_this2.getInputValue() !== _this2.value) { | ||
_this2.setInputValue(_this2.value); | ||
} | ||
if (_this2.valueDescriptor && _this2.getInputValue() !== _this2.state.value) { | ||
_this2.setInputValue(_this2.state.value); | ||
} | ||
}; | ||
@@ -164,3 +139,2 @@ | ||
var input = _this2.input; | ||
if (!input) { | ||
@@ -178,45 +152,9 @@ return null; | ||
this.enableValueAccessors = function () { | ||
if (_this2.canUseAccessors) { | ||
var input = _this2.getInputDOMNode(); | ||
_this2.valueDescriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(input), 'value'); | ||
Object.defineProperty(input, 'value', { | ||
configurable: true, | ||
enumerable: true, | ||
get: function get() { | ||
return _this2.value; | ||
}, | ||
set: function set(value) { | ||
_this2.value = value; | ||
_this2.valueDescriptor.set.call(input, value); | ||
} | ||
}); | ||
} | ||
}; | ||
this.disableValueAccessors = function () { | ||
var valueDescriptor = _this2.valueDescriptor; | ||
var input = _this2.getInputDOMNode(); | ||
if (!valueDescriptor || !input) { | ||
return; | ||
} | ||
_this2.valueDescriptor = null; | ||
Object.defineProperty(input, 'value', valueDescriptor); | ||
}; | ||
this.getInputValue = function () { | ||
var input = _this2.getInputDOMNode(); | ||
var valueDescriptor = _this2.valueDescriptor; | ||
var value; | ||
if (valueDescriptor) { | ||
value = valueDescriptor.get.call(input); | ||
} else { | ||
value = input.value; | ||
if (!input) { | ||
return null; | ||
} | ||
return value; | ||
return input.value; | ||
}; | ||
@@ -255,3 +193,3 @@ | ||
this.setCursorToEnd = function () { | ||
var filledLen = getFilledLength(_this2.maskOptions, _this2.state.value); | ||
var filledLen = getFilledLength(_this2.maskOptions, _this2.value); | ||
var pos = _this2.getRightEditablePos(filledLen); | ||
@@ -312,8 +250,5 @@ if (pos !== null) { | ||
this.setCursorPos = function (pos) { | ||
var raf = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (fn) { | ||
return setTimeout(fn, 0); | ||
}; | ||
_this2.setSelection(pos, 0); | ||
_this2.setSelection(pos, 0); | ||
raf(function () { | ||
defer(function () { | ||
_this2.setSelection(pos, 0); | ||
@@ -333,94 +268,35 @@ }); | ||
this.updateUncontrolledInput = function () { | ||
if (_this2.getInputValue() !== _this2.state.value) { | ||
_this2.setInputValue(_this2.state.value); | ||
this.onKeyDown = function (event) { | ||
_this2.backspaceOrDeleteRemoval = null; | ||
if (typeof _this2.props.onKeyDown === 'function') { | ||
_this2.props.onKeyDown(event); | ||
} | ||
}; | ||
this.onKeyDown = function (event) { | ||
var key = event.key, | ||
ctrlKey = event.ctrlKey, | ||
metaKey = event.metaKey; | ||
metaKey = event.metaKey, | ||
defaultPrevented = event.defaultPrevented; | ||
var hasHandler = typeof _this2.props.onKeyDown === 'function'; | ||
if (ctrlKey || metaKey) { | ||
if (hasHandler) { | ||
_this2.props.onKeyDown(event); | ||
} | ||
if (ctrlKey || metaKey || defaultPrevented) { | ||
return; | ||
} | ||
var cursorPos = _this2.getCursorPos(); | ||
var value = _this2.state.value; | ||
var prefix = _this2.maskOptions.prefix; | ||
if (key === 'Backspace' || key === 'Delete') { | ||
_this2.backspaceOrDeleteRemoval = { | ||
key: key, | ||
selection: _this2.getSelection() | ||
}; | ||
var preventDefault = false; | ||
switch (key) { | ||
case 'Backspace': | ||
case 'Delete': | ||
var deleteFromRight = key === 'Delete'; | ||
var selectionRange = _this2.getSelection(); | ||
if (selectionRange.length) { | ||
value = clearRange(_this2.maskOptions, value, selectionRange.start, selectionRange.length); | ||
} else if (cursorPos < prefix.length || !deleteFromRight && cursorPos === prefix.length) { | ||
cursorPos = prefix.length; | ||
} else { | ||
var editablePos = deleteFromRight ? _this2.getRightEditablePos(cursorPos) : _this2.getLeftEditablePos(cursorPos - 1); | ||
if (editablePos !== null) { | ||
value = clearRange(_this2.maskOptions, value, editablePos, 1); | ||
cursorPos = editablePos; | ||
} | ||
} | ||
preventDefault = true; | ||
break; | ||
default: | ||
break; | ||
} | ||
if (hasHandler) { | ||
_this2.props.onKeyDown(event); | ||
} | ||
if (value !== _this2.state.value) { | ||
preventDefault = true; | ||
_this2.setInputValue(value); | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : value | ||
defer(function () { | ||
_this2.backspaceOrDeleteRemoval = null; | ||
}); | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
} | ||
if (preventDefault) { | ||
event.preventDefault(); | ||
_this2.setCursorPos(cursorPos); | ||
} | ||
}; | ||
this.onKeyPress = function (event) { | ||
var key = event.key, | ||
ctrlKey = event.ctrlKey, | ||
metaKey = event.metaKey; | ||
var hasHandler = typeof _this2.props.onKeyPress === 'function'; | ||
if (key === 'Enter' || ctrlKey || metaKey) { | ||
if (hasHandler) { | ||
_this2.props.onKeyPress(event); | ||
} | ||
return; | ||
} | ||
if (_this2.isWindowsPhoneBrowser) { | ||
return; | ||
} | ||
var cursorPos = _this2.getCursorPos(); | ||
var selection = _this2.getSelection(); | ||
var value = _this2.state.value; | ||
this.onChange = function (event) { | ||
var paste = _this2.paste; | ||
var _maskOptions = _this2.maskOptions, | ||
mask = _maskOptions.mask, | ||
maskChar = _maskOptions.maskChar, | ||
lastEditablePos = _maskOptions.lastEditablePos, | ||
@@ -430,53 +306,11 @@ prefix = _maskOptions.prefix; | ||
if (isPermanentChar(_this2.maskOptions, cursorPos) && mask[cursorPos] === key) { | ||
value = insertString(_this2.maskOptions, value, key, cursorPos); | ||
++cursorPos; | ||
} else { | ||
var editablePos = _this2.getRightEditablePos(cursorPos); | ||
if (editablePos !== null && isAllowedChar(_this2.maskOptions, editablePos, key)) { | ||
value = clearRange(_this2.maskOptions, value, selection.start, selection.length); | ||
value = insertString(_this2.maskOptions, value, key, editablePos); | ||
cursorPos = editablePos + 1; | ||
} | ||
} | ||
var value = _this2.getInputValue(); | ||
var oldValue = _this2.value; | ||
if (value !== _this2.state.value) { | ||
_this2.setInputValue(value); | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : value | ||
}); | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
if (paste) { | ||
_this2.paste = null; | ||
_this2.pasteText(paste.value, value, paste.selection, event); | ||
return; | ||
} | ||
event.preventDefault(); | ||
if (cursorPos < lastEditablePos && cursorPos > prefix.length) { | ||
cursorPos = _this2.getRightEditablePos(cursorPos); | ||
} | ||
_this2.setCursorPos(cursorPos); | ||
}; | ||
this.onChange = function (event) { | ||
var pasteSelection = _this2.pasteSelection; | ||
var _maskOptions2 = _this2.maskOptions, | ||
mask = _maskOptions2.mask, | ||
maskChar = _maskOptions2.maskChar, | ||
lastEditablePos = _maskOptions2.lastEditablePos, | ||
prefix = _maskOptions2.prefix; | ||
var value = _this2.getInputValue(); | ||
if (!value && _this2.preventEmptyChange) { | ||
_this2.disableValueAccessors(); | ||
_this2.preventEmptyChange = false; | ||
_this2.setInputValue(_this2.state.value); | ||
return; | ||
} | ||
var oldValue = _this2.state.value; | ||
if (pasteSelection) { | ||
_this2.pasteSelection = null; | ||
_this2.pasteText(oldValue, value, pasteSelection, event); | ||
return; | ||
} | ||
var selection = _this2.getSelection(); | ||
@@ -491,3 +325,23 @@ var cursorPos = selection.end; | ||
if (valueLen > oldValueLen) { | ||
if (_this2.backspaceOrDeleteRemoval) { | ||
var deleteFromRight = _this2.backspaceOrDeleteRemoval.key === 'Delete'; | ||
value = _this2.value; | ||
selection = _this2.backspaceOrDeleteRemoval.selection; | ||
cursorPos = selection.start; | ||
_this2.backspaceOrDeleteRemoval = null; | ||
if (selection.length) { | ||
value = clearRange(_this2.maskOptions, value, selection.start, selection.length); | ||
} else if (selection.start < prefix.length || !deleteFromRight && selection.start === prefix.length) { | ||
cursorPos = prefix.length; | ||
} else { | ||
var editablePos = deleteFromRight ? _this2.getRightEditablePos(cursorPos) : _this2.getLeftEditablePos(cursorPos - 1); | ||
if (editablePos !== null) { | ||
value = clearRange(_this2.maskOptions, value, editablePos, 1); | ||
cursorPos = editablePos; | ||
} | ||
} | ||
} else if (valueLen > oldValueLen) { | ||
var enteredStringLen = valueLen - oldValueLen; | ||
@@ -511,3 +365,6 @@ var startPos = selection.end - enteredStringLen; | ||
if (enteredStringLen !== 1 || cursorPos >= prefix.length && cursorPos < lastEditablePos) { | ||
cursorPos = getFilledLength(_this2.maskOptions, clearedValue); | ||
cursorPos = Math.max(getFilledLength(_this2.maskOptions, clearedValue), cursorPos); | ||
if (cursorPos < lastEditablePos) { | ||
cursorPos = _this2.getRightEditablePos(cursorPos); | ||
} | ||
} else if (cursorPos < lastEditablePos) { | ||
@@ -531,3 +388,6 @@ cursorPos++; | ||
if (!clearOnly) { | ||
cursorPos = getFilledLength(_this2.maskOptions, clearedValue); | ||
cursorPos = Math.max(getFilledLength(_this2.maskOptions, clearedValue), cursorPos); | ||
if (cursorPos < lastEditablePos) { | ||
cursorPos = _this2.getRightEditablePos(cursorPos); | ||
} | ||
} else if (cursorPos < prefix.length) { | ||
@@ -539,49 +399,13 @@ cursorPos = prefix.length; | ||
if (_this2.isWindowsPhoneBrowser) { | ||
event.persist(); | ||
setTimeout(function () { | ||
if (_this2.unmounted) { | ||
return; | ||
} | ||
_this2.setInputValue(value); | ||
_this2.setInputValue(value); | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
if (!_this2.hasValue) { | ||
_this2.setState({ | ||
value: value | ||
}); | ||
} | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
_this2.setCursorPos(cursorPos); | ||
}, 0); | ||
if (_this2.isWindowsPhoneBrowser) { | ||
defer(function () { | ||
_this2.setSelection(cursorPos, 0); | ||
}); | ||
} else { | ||
// prevent android autocomplete insertion on backspace | ||
if (!_this2.canUseAccessors || !_this2.isAndroidBrowser) { | ||
_this2.setInputValue(value); | ||
} | ||
if (_this2.canUseAccessors && (_this2.isAndroidFirefox && value && !_this2.getInputValue() || _this2.isAndroidBrowser)) { | ||
_this2.value = value; | ||
_this2.enableValueAccessors(); | ||
if (_this2.isAndroidFirefox) { | ||
_this2.preventEmptyChange = true; | ||
} | ||
setTimeout(function () { | ||
_this2.preventEmptyChange = false; | ||
_this2.disableValueAccessors(); | ||
}, 0); | ||
} | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : value | ||
}); | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
_this2.setCursorPos(cursorPos); | ||
@@ -592,3 +416,3 @@ } | ||
this.onFocus = function (event) { | ||
if (!_this2.state.value) { | ||
if (!_this2.value) { | ||
var prefix = _this2.maskOptions.prefix; | ||
@@ -606,5 +430,3 @@ var value = formatValue(_this2.maskOptions, prefix); | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : inputValue | ||
}, _this2.setCursorToEnd); | ||
_this2.value = inputValue; | ||
@@ -614,4 +436,6 @@ if (isInputValueChanged && typeof _this2.props.onChange === 'function') { | ||
} | ||
} else if (getFilledLength(_this2.maskOptions, _this2.state.value) < _this2.maskOptions.mask.length) { | ||
_this2.setCursorToEnd(); | ||
} else if (getFilledLength(_this2.maskOptions, _this2.value) < _this2.maskOptions.mask.length) { | ||
_this2.setCursorToEnd(); | ||
} | ||
@@ -625,3 +449,3 @@ | ||
this.onBlur = function (event) { | ||
if (!_this2.props.alwaysShowMask && isEmpty(_this2.maskOptions, _this2.state.value)) { | ||
if (!_this2.props.alwaysShowMask && isEmpty(_this2.maskOptions, _this2.value)) { | ||
var inputValue = ''; | ||
@@ -634,6 +458,2 @@ var isInputValueChanged = inputValue !== _this2.getInputValue(); | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : '' | ||
}); | ||
if (isInputValueChanged && typeof _this2.props.onChange === 'function') { | ||
@@ -650,23 +470,13 @@ _this2.props.onChange(event); | ||
this.onPaste = function (event) { | ||
if (_this2.isAndroidBrowser) { | ||
_this2.pasteSelection = _this2.getSelection(); | ||
_this2.setInputValue(''); | ||
return; | ||
if (typeof _this2.props.onPaste === 'function') { | ||
_this2.props.onPaste(event); | ||
} | ||
var text; | ||
if (window.clipboardData && window.clipboardData.getData) { | ||
// IE | ||
text = window.clipboardData.getData('Text'); | ||
} else if (event.clipboardData && event.clipboardData.getData) { | ||
text = event.clipboardData.getData('text/plain'); | ||
if (_this2.isAndroidBrowser && !event.defaultPrevented) { | ||
_this2.paste = { | ||
value: _this2.getInputValue(), | ||
selection: _this2.getSelection() | ||
}; | ||
_this2.setInputValue(''); | ||
} | ||
if (text) { | ||
var value = _this2.state.value; | ||
var selection = _this2.getSelection(); | ||
_this2.pasteText(value, text, selection, event); | ||
} | ||
event.preventDefault(); | ||
}; | ||
@@ -685,8 +495,3 @@ | ||
if (value !== _this2.getInputValue()) { | ||
if (event) { | ||
_this2.setInputValue(value); | ||
} | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : value | ||
}); | ||
_this2.setInputValue(value); | ||
if (event && typeof _this2.props.onChange === 'function') { | ||
@@ -710,3 +515,3 @@ _this2.props.onChange(event); | ||
if (!props.disabled && !props.readOnly) { | ||
var handlersKeys = ['onFocus', 'onBlur', 'onChange', 'onKeyDown', 'onKeyPress', 'onPaste']; | ||
var handlersKeys = ['onFocus', 'onBlur', 'onChange', 'onKeyDown', 'onPaste']; | ||
handlersKeys.forEach(function (key) { | ||
@@ -716,6 +521,2 @@ props[key] = _this2[key]; | ||
} | ||
if (props.value != null) { | ||
props.value = _this2.state.value; | ||
} | ||
} | ||
@@ -722,0 +523,0 @@ |
384
lib/index.js
@@ -11,4 +11,2 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _react = require('react'); | ||
@@ -26,2 +24,6 @@ | ||
var _defer = require('./utils/defer'); | ||
var _defer2 = _interopRequireDefault(_defer); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -72,13 +74,6 @@ | ||
_this.state = { value: value }; | ||
_this.value = value; | ||
return _this; | ||
} | ||
_createClass(InputElement, [{ | ||
key: 'componentWillUnmount', | ||
value: function componentWillUnmount() { | ||
this.unmounted = true; | ||
} | ||
}]); | ||
return InputElement; | ||
@@ -97,14 +92,5 @@ }(_react2.default.Component); | ||
var input = _this2.getInputDOMNode(); | ||
// workaround for Jest | ||
// it doesn't mount a real node so input will be null | ||
if (input && Object.getOwnPropertyDescriptor && Object.getPrototypeOf && Object.defineProperty) { | ||
var valueDescriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(input), 'value'); | ||
_this2.canUseAccessors = !!(valueDescriptor && valueDescriptor.get && valueDescriptor.set); | ||
if (_this2.getInputValue() !== _this2.value) { | ||
_this2.setInputValue(_this2.value); | ||
} | ||
if (_this2.maskOptions.mask && _this2.props.value == null) { | ||
_this2.updateUncontrolledInput(); | ||
} | ||
}; | ||
@@ -125,3 +111,3 @@ | ||
var showEmpty = nextProps.alwaysShowMask || _this2.isFocused(); | ||
var newValue = _this2.hasValue ? _this2.getStringValue(nextProps.value) : _this2.state.value; | ||
var newValue = _this2.hasValue ? _this2.getStringValue(nextProps.value) : _this2.value; | ||
@@ -154,16 +140,8 @@ if (!oldMaskOptions.mask && !_this2.hasValue) { | ||
_this2.value = newValue; | ||
if (_this2.state.value !== newValue) { | ||
_this2.setState({ value: newValue }); | ||
} | ||
}; | ||
this.componentDidUpdate = function (prevProps) { | ||
if ((_this2.maskOptions.mask || prevProps.mask) && _this2.props.value == null) { | ||
_this2.updateUncontrolledInput(); | ||
this.componentDidUpdate = function () { | ||
if (_this2.getInputValue() !== _this2.value) { | ||
_this2.setInputValue(_this2.value); | ||
} | ||
if (_this2.valueDescriptor && _this2.getInputValue() !== _this2.state.value) { | ||
_this2.setInputValue(_this2.state.value); | ||
} | ||
}; | ||
@@ -178,3 +156,2 @@ | ||
var input = _this2.input; | ||
if (!input) { | ||
@@ -192,45 +169,9 @@ return null; | ||
this.enableValueAccessors = function () { | ||
if (_this2.canUseAccessors) { | ||
var input = _this2.getInputDOMNode(); | ||
_this2.valueDescriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(input), 'value'); | ||
Object.defineProperty(input, 'value', { | ||
configurable: true, | ||
enumerable: true, | ||
get: function get() { | ||
return _this2.value; | ||
}, | ||
set: function set(value) { | ||
_this2.value = value; | ||
_this2.valueDescriptor.set.call(input, value); | ||
} | ||
}); | ||
} | ||
}; | ||
this.disableValueAccessors = function () { | ||
var valueDescriptor = _this2.valueDescriptor; | ||
var input = _this2.getInputDOMNode(); | ||
if (!valueDescriptor || !input) { | ||
return; | ||
} | ||
_this2.valueDescriptor = null; | ||
Object.defineProperty(input, 'value', valueDescriptor); | ||
}; | ||
this.getInputValue = function () { | ||
var input = _this2.getInputDOMNode(); | ||
var valueDescriptor = _this2.valueDescriptor; | ||
var value; | ||
if (valueDescriptor) { | ||
value = valueDescriptor.get.call(input); | ||
} else { | ||
value = input.value; | ||
if (!input) { | ||
return null; | ||
} | ||
return value; | ||
return input.value; | ||
}; | ||
@@ -269,3 +210,3 @@ | ||
this.setCursorToEnd = function () { | ||
var filledLen = (0, _string.getFilledLength)(_this2.maskOptions, _this2.state.value); | ||
var filledLen = (0, _string.getFilledLength)(_this2.maskOptions, _this2.value); | ||
var pos = _this2.getRightEditablePos(filledLen); | ||
@@ -326,8 +267,5 @@ if (pos !== null) { | ||
this.setCursorPos = function (pos) { | ||
var raf = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (fn) { | ||
return setTimeout(fn, 0); | ||
}; | ||
_this2.setSelection(pos, 0); | ||
_this2.setSelection(pos, 0); | ||
raf(function () { | ||
(0, _defer2.default)(function () { | ||
_this2.setSelection(pos, 0); | ||
@@ -347,94 +285,35 @@ }); | ||
this.updateUncontrolledInput = function () { | ||
if (_this2.getInputValue() !== _this2.state.value) { | ||
_this2.setInputValue(_this2.state.value); | ||
this.onKeyDown = function (event) { | ||
_this2.backspaceOrDeleteRemoval = null; | ||
if (typeof _this2.props.onKeyDown === 'function') { | ||
_this2.props.onKeyDown(event); | ||
} | ||
}; | ||
this.onKeyDown = function (event) { | ||
var key = event.key, | ||
ctrlKey = event.ctrlKey, | ||
metaKey = event.metaKey; | ||
metaKey = event.metaKey, | ||
defaultPrevented = event.defaultPrevented; | ||
var hasHandler = typeof _this2.props.onKeyDown === 'function'; | ||
if (ctrlKey || metaKey) { | ||
if (hasHandler) { | ||
_this2.props.onKeyDown(event); | ||
} | ||
if (ctrlKey || metaKey || defaultPrevented) { | ||
return; | ||
} | ||
var cursorPos = _this2.getCursorPos(); | ||
var value = _this2.state.value; | ||
var prefix = _this2.maskOptions.prefix; | ||
if (key === 'Backspace' || key === 'Delete') { | ||
_this2.backspaceOrDeleteRemoval = { | ||
key: key, | ||
selection: _this2.getSelection() | ||
}; | ||
var preventDefault = false; | ||
switch (key) { | ||
case 'Backspace': | ||
case 'Delete': | ||
var deleteFromRight = key === 'Delete'; | ||
var selectionRange = _this2.getSelection(); | ||
if (selectionRange.length) { | ||
value = (0, _string.clearRange)(_this2.maskOptions, value, selectionRange.start, selectionRange.length); | ||
} else if (cursorPos < prefix.length || !deleteFromRight && cursorPos === prefix.length) { | ||
cursorPos = prefix.length; | ||
} else { | ||
var editablePos = deleteFromRight ? _this2.getRightEditablePos(cursorPos) : _this2.getLeftEditablePos(cursorPos - 1); | ||
if (editablePos !== null) { | ||
value = (0, _string.clearRange)(_this2.maskOptions, value, editablePos, 1); | ||
cursorPos = editablePos; | ||
} | ||
} | ||
preventDefault = true; | ||
break; | ||
default: | ||
break; | ||
} | ||
if (hasHandler) { | ||
_this2.props.onKeyDown(event); | ||
} | ||
if (value !== _this2.state.value) { | ||
preventDefault = true; | ||
_this2.setInputValue(value); | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : value | ||
(0, _defer2.default)(function () { | ||
_this2.backspaceOrDeleteRemoval = null; | ||
}); | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
} | ||
if (preventDefault) { | ||
event.preventDefault(); | ||
_this2.setCursorPos(cursorPos); | ||
} | ||
}; | ||
this.onKeyPress = function (event) { | ||
var key = event.key, | ||
ctrlKey = event.ctrlKey, | ||
metaKey = event.metaKey; | ||
var hasHandler = typeof _this2.props.onKeyPress === 'function'; | ||
if (key === 'Enter' || ctrlKey || metaKey) { | ||
if (hasHandler) { | ||
_this2.props.onKeyPress(event); | ||
} | ||
return; | ||
} | ||
if (_this2.isWindowsPhoneBrowser) { | ||
return; | ||
} | ||
var cursorPos = _this2.getCursorPos(); | ||
var selection = _this2.getSelection(); | ||
var value = _this2.state.value; | ||
this.onChange = function (event) { | ||
var paste = _this2.paste; | ||
var _maskOptions = _this2.maskOptions, | ||
mask = _maskOptions.mask, | ||
maskChar = _maskOptions.maskChar, | ||
lastEditablePos = _maskOptions.lastEditablePos, | ||
@@ -444,53 +323,11 @@ prefix = _maskOptions.prefix; | ||
if ((0, _string.isPermanentChar)(_this2.maskOptions, cursorPos) && mask[cursorPos] === key) { | ||
value = (0, _string.insertString)(_this2.maskOptions, value, key, cursorPos); | ||
++cursorPos; | ||
} else { | ||
var editablePos = _this2.getRightEditablePos(cursorPos); | ||
if (editablePos !== null && (0, _string.isAllowedChar)(_this2.maskOptions, editablePos, key)) { | ||
value = (0, _string.clearRange)(_this2.maskOptions, value, selection.start, selection.length); | ||
value = (0, _string.insertString)(_this2.maskOptions, value, key, editablePos); | ||
cursorPos = editablePos + 1; | ||
} | ||
} | ||
var value = _this2.getInputValue(); | ||
var oldValue = _this2.value; | ||
if (value !== _this2.state.value) { | ||
_this2.setInputValue(value); | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : value | ||
}); | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
if (paste) { | ||
_this2.paste = null; | ||
_this2.pasteText(paste.value, value, paste.selection, event); | ||
return; | ||
} | ||
event.preventDefault(); | ||
if (cursorPos < lastEditablePos && cursorPos > prefix.length) { | ||
cursorPos = _this2.getRightEditablePos(cursorPos); | ||
} | ||
_this2.setCursorPos(cursorPos); | ||
}; | ||
this.onChange = function (event) { | ||
var pasteSelection = _this2.pasteSelection; | ||
var _maskOptions2 = _this2.maskOptions, | ||
mask = _maskOptions2.mask, | ||
maskChar = _maskOptions2.maskChar, | ||
lastEditablePos = _maskOptions2.lastEditablePos, | ||
prefix = _maskOptions2.prefix; | ||
var value = _this2.getInputValue(); | ||
if (!value && _this2.preventEmptyChange) { | ||
_this2.disableValueAccessors(); | ||
_this2.preventEmptyChange = false; | ||
_this2.setInputValue(_this2.state.value); | ||
return; | ||
} | ||
var oldValue = _this2.state.value; | ||
if (pasteSelection) { | ||
_this2.pasteSelection = null; | ||
_this2.pasteText(oldValue, value, pasteSelection, event); | ||
return; | ||
} | ||
var selection = _this2.getSelection(); | ||
@@ -505,3 +342,23 @@ var cursorPos = selection.end; | ||
if (valueLen > oldValueLen) { | ||
if (_this2.backspaceOrDeleteRemoval) { | ||
var deleteFromRight = _this2.backspaceOrDeleteRemoval.key === 'Delete'; | ||
value = _this2.value; | ||
selection = _this2.backspaceOrDeleteRemoval.selection; | ||
cursorPos = selection.start; | ||
_this2.backspaceOrDeleteRemoval = null; | ||
if (selection.length) { | ||
value = (0, _string.clearRange)(_this2.maskOptions, value, selection.start, selection.length); | ||
} else if (selection.start < prefix.length || !deleteFromRight && selection.start === prefix.length) { | ||
cursorPos = prefix.length; | ||
} else { | ||
var editablePos = deleteFromRight ? _this2.getRightEditablePos(cursorPos) : _this2.getLeftEditablePos(cursorPos - 1); | ||
if (editablePos !== null) { | ||
value = (0, _string.clearRange)(_this2.maskOptions, value, editablePos, 1); | ||
cursorPos = editablePos; | ||
} | ||
} | ||
} else if (valueLen > oldValueLen) { | ||
var enteredStringLen = valueLen - oldValueLen; | ||
@@ -525,3 +382,6 @@ var startPos = selection.end - enteredStringLen; | ||
if (enteredStringLen !== 1 || cursorPos >= prefix.length && cursorPos < lastEditablePos) { | ||
cursorPos = (0, _string.getFilledLength)(_this2.maskOptions, clearedValue); | ||
cursorPos = Math.max((0, _string.getFilledLength)(_this2.maskOptions, clearedValue), cursorPos); | ||
if (cursorPos < lastEditablePos) { | ||
cursorPos = _this2.getRightEditablePos(cursorPos); | ||
} | ||
} else if (cursorPos < lastEditablePos) { | ||
@@ -545,3 +405,6 @@ cursorPos++; | ||
if (!clearOnly) { | ||
cursorPos = (0, _string.getFilledLength)(_this2.maskOptions, clearedValue); | ||
cursorPos = Math.max((0, _string.getFilledLength)(_this2.maskOptions, clearedValue), cursorPos); | ||
if (cursorPos < lastEditablePos) { | ||
cursorPos = _this2.getRightEditablePos(cursorPos); | ||
} | ||
} else if (cursorPos < prefix.length) { | ||
@@ -553,49 +416,13 @@ cursorPos = prefix.length; | ||
if (_this2.isWindowsPhoneBrowser) { | ||
event.persist(); | ||
setTimeout(function () { | ||
if (_this2.unmounted) { | ||
return; | ||
} | ||
_this2.setInputValue(value); | ||
_this2.setInputValue(value); | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
if (!_this2.hasValue) { | ||
_this2.setState({ | ||
value: value | ||
}); | ||
} | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
_this2.setCursorPos(cursorPos); | ||
}, 0); | ||
if (_this2.isWindowsPhoneBrowser) { | ||
(0, _defer2.default)(function () { | ||
_this2.setSelection(cursorPos, 0); | ||
}); | ||
} else { | ||
// prevent android autocomplete insertion on backspace | ||
if (!_this2.canUseAccessors || !_this2.isAndroidBrowser) { | ||
_this2.setInputValue(value); | ||
} | ||
if (_this2.canUseAccessors && (_this2.isAndroidFirefox && value && !_this2.getInputValue() || _this2.isAndroidBrowser)) { | ||
_this2.value = value; | ||
_this2.enableValueAccessors(); | ||
if (_this2.isAndroidFirefox) { | ||
_this2.preventEmptyChange = true; | ||
} | ||
setTimeout(function () { | ||
_this2.preventEmptyChange = false; | ||
_this2.disableValueAccessors(); | ||
}, 0); | ||
} | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : value | ||
}); | ||
if (typeof _this2.props.onChange === 'function') { | ||
_this2.props.onChange(event); | ||
} | ||
_this2.setCursorPos(cursorPos); | ||
@@ -606,3 +433,3 @@ } | ||
this.onFocus = function (event) { | ||
if (!_this2.state.value) { | ||
if (!_this2.value) { | ||
var prefix = _this2.maskOptions.prefix; | ||
@@ -620,5 +447,3 @@ var value = (0, _string.formatValue)(_this2.maskOptions, prefix); | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : inputValue | ||
}, _this2.setCursorToEnd); | ||
_this2.value = inputValue; | ||
@@ -628,4 +453,6 @@ if (isInputValueChanged && typeof _this2.props.onChange === 'function') { | ||
} | ||
} else if ((0, _string.getFilledLength)(_this2.maskOptions, _this2.state.value) < _this2.maskOptions.mask.length) { | ||
_this2.setCursorToEnd(); | ||
} else if ((0, _string.getFilledLength)(_this2.maskOptions, _this2.value) < _this2.maskOptions.mask.length) { | ||
_this2.setCursorToEnd(); | ||
} | ||
@@ -639,3 +466,3 @@ | ||
this.onBlur = function (event) { | ||
if (!_this2.props.alwaysShowMask && (0, _string.isEmpty)(_this2.maskOptions, _this2.state.value)) { | ||
if (!_this2.props.alwaysShowMask && (0, _string.isEmpty)(_this2.maskOptions, _this2.value)) { | ||
var inputValue = ''; | ||
@@ -648,6 +475,2 @@ var isInputValueChanged = inputValue !== _this2.getInputValue(); | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : '' | ||
}); | ||
if (isInputValueChanged && typeof _this2.props.onChange === 'function') { | ||
@@ -664,23 +487,13 @@ _this2.props.onChange(event); | ||
this.onPaste = function (event) { | ||
if (_this2.isAndroidBrowser) { | ||
_this2.pasteSelection = _this2.getSelection(); | ||
_this2.setInputValue(''); | ||
return; | ||
if (typeof _this2.props.onPaste === 'function') { | ||
_this2.props.onPaste(event); | ||
} | ||
var text; | ||
if (window.clipboardData && window.clipboardData.getData) { | ||
// IE | ||
text = window.clipboardData.getData('Text'); | ||
} else if (event.clipboardData && event.clipboardData.getData) { | ||
text = event.clipboardData.getData('text/plain'); | ||
if (_this2.isAndroidBrowser && !event.defaultPrevented) { | ||
_this2.paste = { | ||
value: _this2.getInputValue(), | ||
selection: _this2.getSelection() | ||
}; | ||
_this2.setInputValue(''); | ||
} | ||
if (text) { | ||
var value = _this2.state.value; | ||
var selection = _this2.getSelection(); | ||
_this2.pasteText(value, text, selection, event); | ||
} | ||
event.preventDefault(); | ||
}; | ||
@@ -699,8 +512,3 @@ | ||
if (value !== _this2.getInputValue()) { | ||
if (event) { | ||
_this2.setInputValue(value); | ||
} | ||
_this2.setState({ | ||
value: _this2.hasValue ? _this2.state.value : value | ||
}); | ||
_this2.setInputValue(value); | ||
if (event && typeof _this2.props.onChange === 'function') { | ||
@@ -724,3 +532,3 @@ _this2.props.onChange(event); | ||
if (!props.disabled && !props.readOnly) { | ||
var handlersKeys = ['onFocus', 'onBlur', 'onChange', 'onKeyDown', 'onKeyPress', 'onPaste']; | ||
var handlersKeys = ['onFocus', 'onBlur', 'onChange', 'onKeyDown', 'onPaste']; | ||
handlersKeys.forEach(function (key) { | ||
@@ -730,6 +538,2 @@ props[key] = _this2[key]; | ||
} | ||
if (props.value != null) { | ||
props.value = _this2.state.value; | ||
} | ||
} | ||
@@ -736,0 +540,0 @@ |
{ | ||
"name": "react-input-mask", | ||
"description": "Masked input component for React", | ||
"version": "1.0.0-beta.1", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/sanniassin/react-input-mask", | ||
@@ -20,2 +20,6 @@ "license": "MIT", | ||
"babel-cli": "^6.24.1", | ||
"babel-eslint": "^7.2.3", | ||
"babel-loader": "^7.1.0", | ||
"babel-plugin-transform-es3-member-expression-literals": "^6.22.0", | ||
"babel-plugin-transform-es3-property-literals": "^6.22.0", | ||
"babel-polyfill": "^6.23.0", | ||
@@ -30,2 +34,8 @@ "babel-preset-es2015": "^6.24.1", | ||
"cross-env": "^5.0.1", | ||
"eslint": "^3.19.0", | ||
"eslint-config-airbnb": "^15.0.1", | ||
"eslint-plugin-import": "^2.6.0", | ||
"eslint-plugin-jsx-a11y": "^5.1.0", | ||
"eslint-plugin-react": "^7.1.0", | ||
"html-webpack-plugin": "^2.29.0", | ||
"karma": "^1.6.0", | ||
@@ -38,4 +48,4 @@ "karma-browserify": "^5.0.5", | ||
"phantomjs-prebuilt": "^2.1.7", | ||
"react": "^15.5.4", | ||
"react-dom": "^15.5.4", | ||
"react": "^15.6.1", | ||
"react-dom": "^15.6.1", | ||
"rollup": "^0.42.0", | ||
@@ -46,3 +56,5 @@ "rollup-plugin-babel": "^2.7.1", | ||
"rollup-plugin-uglify": "^2.0.1", | ||
"watchify": "^3.7.0" | ||
"watchify": "^3.9.0", | ||
"webpack": "^2.6.1", | ||
"webpack-dev-server": "^2.5.0" | ||
}, | ||
@@ -64,2 +76,4 @@ "main": "lib/index.js", | ||
"build:umd:min": "cross-env BABEL_ENV=es NODE_ENV=production rollup -c -i src/index.js -o dist/react-input-mask.min.js", | ||
"dev": "cross-env NODE_ENV=development webpack-dev-server", | ||
"dev-ie8": "cross-env NODE_ENV=development webpack-dev-server --inline=false", | ||
"prepare": "npm test && npm run clean && npm run build", | ||
@@ -66,0 +80,0 @@ "test": "npm run build && npm run test:input && npm run test:server-render && npm run test:build", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
17
1
90392
37
1997