react-input-mask
Advanced tools
Comparing version 1.2.2 to 2.0.0-beta.0
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('react')) : | ||
typeof define === 'function' && define.amd ? define(['react'], factory) : | ||
(global.ReactInputMask = factory(global.React)); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('react')) : | ||
typeof define === 'function' && define.amd ? define(['react'], factory) : | ||
(global.ReactInputMask = factory(global.React)); | ||
}(this, (function (React) { 'use strict'; | ||
React = React && React.hasOwnProperty('default') ? React['default'] : React; | ||
React = React && React.hasOwnProperty('default') ? React['default'] : React; | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
return _typeof(obj); | ||
} | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
function _extends() { | ||
_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]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
return _extends.apply(this, arguments); | ||
} | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype = Object.create(superClass.prototype); | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
} | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
function _instanceof(left, right) { | ||
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { | ||
return right[Symbol.hasInstance](left); | ||
} else { | ||
return left instanceof right; | ||
} | ||
} | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
function _objectWithoutProperties(source, excluded) { | ||
if (source == null) return {}; | ||
var target = {}; | ||
var sourceKeys = Object.keys(source); | ||
var key, i; | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
for (i = 0; i < sourceKeys.length; i++) { | ||
key = sourceKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
target[key] = source[key]; | ||
return target; | ||
} | ||
if (Object.getOwnPropertySymbols) { | ||
var sourceSymbolKeys = Object.getOwnPropertySymbols(source); | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype.__proto__ = superClass && superClass.prototype; | ||
subClass.__proto__ = superClass; | ||
} | ||
for (i = 0; i < sourceSymbolKeys.length; i++) { | ||
key = sourceSymbolKeys[i]; | ||
function _objectWithoutProperties(source, excluded) { | ||
if (source == null) return {}; | ||
var target = {}; | ||
var sourceKeys = Object.keys(source); | ||
var key, i; | ||
for (i = 0; i < sourceKeys.length; i++) { | ||
key = sourceKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
} | ||
if (Object.getOwnPropertySymbols) { | ||
var sourceSymbolKeys = Object.getOwnPropertySymbols(source); | ||
function _assertThisInitialized(self) { | ||
if (self === void 0) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
for (i = 0; i < sourceSymbolKeys.length; i++) { | ||
key = sourceSymbolKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
} | ||
return self; | ||
} | ||
function _assertThisInitialized(self) { | ||
if (self === void 0) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
var defaultCharsRules = { | ||
'9': '[0-9]', | ||
'a': '[A-Za-z]', | ||
'*': '[A-Za-z0-9]' | ||
}; | ||
var defaultMaskChar = '_'; | ||
function parseMask (mask, maskChar, charsRules) { | ||
if (maskChar === undefined) { | ||
maskChar = defaultMaskChar; | ||
return self; | ||
} | ||
if (charsRules == null) { | ||
charsRules = defaultCharsRules; | ||
function setInputSelection(input, start, end) { | ||
if ('selectionStart' in input && 'selectionEnd' in input) { | ||
input.selectionStart = start; | ||
input.selectionEnd = end; | ||
} else { | ||
var range = input.createTextRange(); | ||
range.collapse(true); | ||
range.moveStart('character', start); | ||
range.moveEnd('character', end - start); | ||
range.select(); | ||
} | ||
} | ||
function getInputSelection(input) { | ||
var start = 0; | ||
var end = 0; | ||
if (!mask || typeof mask !== 'string') { | ||
if ('selectionStart' in input && 'selectionEnd' in input) { | ||
start = input.selectionStart; | ||
end = input.selectionEnd; | ||
} else { | ||
var range = document.selection.createRange(); | ||
if (range.parentElement() === input) { | ||
start = -range.moveStart('character', -input.value.length); | ||
end = -range.moveEnd('character', -input.value.length); | ||
} | ||
} | ||
return { | ||
maskChar: maskChar, | ||
charsRules: charsRules, | ||
mask: null, | ||
prefix: null, | ||
lastEditablePos: null, | ||
permanents: [] | ||
start: start, | ||
end: end, | ||
length: end - start | ||
}; | ||
} | ||
var str = ''; | ||
var prefix = ''; | ||
var permanents = []; | ||
var isPermanent = false; | ||
var lastEditablePos = null; | ||
mask.split('').forEach(function (character) { | ||
if (!isPermanent && character === '\\') { | ||
isPermanent = true; | ||
} else { | ||
if (isPermanent || !charsRules[character]) { | ||
permanents.push(str.length); | ||
var defaultFormatChars = { | ||
'9': '[0-9]', | ||
'a': '[A-Za-z]', | ||
'*': '[A-Za-z0-9]' | ||
}; | ||
var defaultMaskChar = '_'; | ||
if (str.length === permanents.length - 1) { | ||
prefix += character; | ||
} | ||
} else { | ||
lastEditablePos = str.length + 1; | ||
} | ||
function parseMask (mask, maskChar, formatChars) { | ||
if (maskChar === undefined) { | ||
maskChar = defaultMaskChar; | ||
} | ||
str += character; | ||
isPermanent = false; | ||
if (formatChars == null) { | ||
formatChars = defaultFormatChars; | ||
} | ||
}); | ||
return { | ||
maskChar: maskChar, | ||
charsRules: charsRules, | ||
prefix: prefix, | ||
mask: str, | ||
lastEditablePos: lastEditablePos, | ||
permanents: permanents | ||
}; | ||
} | ||
function isAndroidBrowser() { | ||
var windows = new RegExp('windows', 'i'); | ||
var firefox = new RegExp('firefox', 'i'); | ||
var android = new RegExp('android', 'i'); | ||
var ua = navigator.userAgent; | ||
return !windows.test(ua) && !firefox.test(ua) && android.test(ua); | ||
} | ||
function isWindowsPhoneBrowser() { | ||
var windows = new RegExp('windows', 'i'); | ||
var phone = new RegExp('phone', 'i'); | ||
var ua = navigator.userAgent; | ||
return windows.test(ua) && phone.test(ua); | ||
} | ||
function isAndroidFirefox() { | ||
var windows = new RegExp('windows', 'i'); | ||
var firefox = new RegExp('firefox', 'i'); | ||
var android = new RegExp('android', 'i'); | ||
var ua = navigator.userAgent; | ||
return !windows.test(ua) && firefox.test(ua) && android.test(ua); | ||
} | ||
if (!mask || typeof mask !== 'string') { | ||
return { | ||
maskChar: maskChar, | ||
formatChars: formatChars, | ||
mask: null, | ||
prefix: null, | ||
lastEditablePos: null, | ||
permanents: [] | ||
}; | ||
} | ||
function isPermanentChar(maskOptions, pos) { | ||
return maskOptions.permanents.indexOf(pos) !== -1; | ||
} | ||
function isAllowedChar(maskOptions, pos, character) { | ||
var mask = maskOptions.mask, | ||
charsRules = maskOptions.charsRules; | ||
var str = ''; | ||
var prefix = ''; | ||
var permanents = []; | ||
var isPermanent = false; | ||
var lastEditablePos = null; | ||
mask.split('').forEach(function (character) { | ||
if (!isPermanent && character === '\\') { | ||
isPermanent = true; | ||
} else { | ||
if (isPermanent || !formatChars[character]) { | ||
permanents.push(str.length); | ||
if (!character) { | ||
return false; | ||
if (str.length === permanents.length - 1) { | ||
prefix += character; | ||
} | ||
} else { | ||
lastEditablePos = str.length + 1; | ||
} | ||
str += character; | ||
isPermanent = false; | ||
} | ||
}); | ||
return { | ||
maskChar: maskChar, | ||
formatChars: formatChars, | ||
prefix: prefix, | ||
mask: str, | ||
lastEditablePos: lastEditablePos, | ||
permanents: permanents | ||
}; | ||
} | ||
if (isPermanentChar(maskOptions, pos)) { | ||
return mask[pos] === character; | ||
function isWindowsPhoneBrowser() { | ||
var windows = new RegExp('windows', 'i'); | ||
var phone = new RegExp('phone', 'i'); | ||
var ua = navigator.userAgent; | ||
return windows.test(ua) && phone.test(ua); | ||
} | ||
var ruleChar = mask[pos]; | ||
var charRule = charsRules[ruleChar]; | ||
return new RegExp(charRule).test(character); | ||
} | ||
function isEmpty(maskOptions, value) { | ||
return value.split('').every(function (character, i) { | ||
return isPermanentChar(maskOptions, i) || !isAllowedChar(maskOptions, i, character); | ||
}); | ||
} | ||
function getFilledLength(maskOptions, value) { | ||
var maskChar = maskOptions.maskChar, | ||
prefix = maskOptions.prefix; | ||
function isPermanentChar(maskOptions, pos) { | ||
return maskOptions.permanents.indexOf(pos) !== -1; | ||
} | ||
function isAllowedChar(maskOptions, pos, character) { | ||
var mask = maskOptions.mask, | ||
formatChars = maskOptions.formatChars; | ||
if (!maskChar) { | ||
while (value.length > prefix.length && isPermanentChar(maskOptions, value.length - 1)) { | ||
value = value.slice(0, value.length - 1); | ||
if (!character) { | ||
return false; | ||
} | ||
return value.length; | ||
if (isPermanentChar(maskOptions, pos)) { | ||
return mask[pos] === character; | ||
} | ||
var ruleChar = mask[pos]; | ||
var charRule = formatChars[ruleChar]; | ||
return new RegExp(charRule).test(character); | ||
} | ||
function isEmpty(maskOptions, value) { | ||
return value.split('').every(function (character, i) { | ||
return isPermanentChar(maskOptions, i) || !isAllowedChar(maskOptions, i, character); | ||
}); | ||
} | ||
function getFilledLength(maskOptions, value) { | ||
var maskChar = maskOptions.maskChar, | ||
prefix = maskOptions.prefix; | ||
var filledLength = prefix.length; | ||
if (!maskChar) { | ||
while (value.length > prefix.length && isPermanentChar(maskOptions, value.length - 1)) { | ||
value = value.slice(0, value.length - 1); | ||
} | ||
for (var i = value.length; i >= prefix.length; i--) { | ||
var character = value[i]; | ||
var isEnteredCharacter = !isPermanentChar(maskOptions, i) && isAllowedChar(maskOptions, i, character); | ||
return value.length; | ||
} | ||
if (isEnteredCharacter) { | ||
filledLength = i + 1; | ||
break; | ||
var filledLength = prefix.length; | ||
for (var i = value.length; i >= prefix.length; i--) { | ||
var character = value[i]; | ||
var isEnteredCharacter = !isPermanentChar(maskOptions, i) && isAllowedChar(maskOptions, i, character); | ||
if (isEnteredCharacter) { | ||
filledLength = i + 1; | ||
break; | ||
} | ||
} | ||
return filledLength; | ||
} | ||
function isFilled(maskOptions, value) { | ||
return getFilledLength(maskOptions, value) === maskOptions.mask.length; | ||
} | ||
function formatValue(maskOptions, value) { | ||
var maskChar = maskOptions.maskChar, | ||
mask = maskOptions.mask, | ||
prefix = maskOptions.prefix; | ||
return filledLength; | ||
} | ||
function isFilled(maskOptions, value) { | ||
return getFilledLength(maskOptions, value) === maskOptions.mask.length; | ||
} | ||
function formatValue(maskOptions, value) { | ||
var maskChar = maskOptions.maskChar, | ||
mask = maskOptions.mask, | ||
prefix = maskOptions.prefix; | ||
if (!maskChar) { | ||
value = insertString(maskOptions, '', value, 0); | ||
if (!maskChar) { | ||
value = insertString(maskOptions, '', value, 0); | ||
if (value.length < prefix.length) { | ||
value = prefix; | ||
} | ||
if (value.length < prefix.length) { | ||
value = prefix; | ||
while (value.length < mask.length && isPermanentChar(maskOptions, value.length)) { | ||
value += mask[value.length]; | ||
} | ||
return value; | ||
} | ||
while (value.length < mask.length && isPermanentChar(maskOptions, value.length)) { | ||
value += mask[value.length]; | ||
if (value) { | ||
var emptyValue = formatValue(maskOptions, ''); | ||
return insertString(maskOptions, emptyValue, value, 0); | ||
} | ||
for (var i = 0; i < mask.length; i++) { | ||
if (isPermanentChar(maskOptions, i)) { | ||
value += mask[i]; | ||
} else { | ||
value += maskChar; | ||
} | ||
} | ||
return value; | ||
} | ||
function clearRange(maskOptions, value, start, len) { | ||
var end = start + len; | ||
var maskChar = maskOptions.maskChar, | ||
mask = maskOptions.mask, | ||
prefix = maskOptions.prefix; | ||
var arrayValue = value.split(''); | ||
if (value) { | ||
var emptyValue = formatValue(maskOptions, ''); | ||
return insertString(maskOptions, emptyValue, value, 0); | ||
} | ||
if (!maskChar) { | ||
// remove any permanent chars after clear range, they will be added back by formatValue | ||
for (var i = end; i < arrayValue.length; i++) { | ||
if (isPermanentChar(maskOptions, i)) { | ||
arrayValue[i] = ''; | ||
} | ||
} | ||
for (var i = 0; i < mask.length; i++) { | ||
if (isPermanentChar(maskOptions, i)) { | ||
value += mask[i]; | ||
} else { | ||
value += maskChar; | ||
start = Math.max(prefix.length, start); | ||
arrayValue.splice(start, end - start); | ||
value = arrayValue.join(''); | ||
return formatValue(maskOptions, value); | ||
} | ||
} | ||
return value; | ||
} | ||
function clearRange(maskOptions, value, start, len) { | ||
var end = start + len; | ||
var maskChar = maskOptions.maskChar, | ||
mask = maskOptions.mask, | ||
prefix = maskOptions.prefix; | ||
var arrayValue = value.split(''); | ||
return arrayValue.map(function (character, i) { | ||
if (i < start || i >= end) { | ||
return character; | ||
} | ||
if (!maskChar) { | ||
// remove any permanent chars after clear range, they will be added back by foramtValue | ||
for (var i = end; i < arrayValue.length; i++) { | ||
if (isPermanentChar(maskOptions, i)) { | ||
arrayValue[i] = ''; | ||
return mask[i]; | ||
} | ||
} | ||
start = Math.max(prefix.length, start); | ||
arrayValue.splice(start, end - start); | ||
value = arrayValue.join(''); | ||
return formatValue(maskOptions, value); | ||
return maskChar; | ||
}).join(''); | ||
} | ||
function insertString(maskOptions, value, insertStr, insertPos) { | ||
var mask = maskOptions.mask, | ||
maskChar = maskOptions.maskChar, | ||
prefix = maskOptions.prefix; | ||
var arrayInsertStr = insertStr.split(''); | ||
var isInputFilled = isFilled(maskOptions, value); | ||
return arrayValue.map(function (character, i) { | ||
if (i < start || i >= end) { | ||
return character; | ||
} | ||
var isUsablePosition = function isUsablePosition(pos, character) { | ||
return !isPermanentChar(maskOptions, pos) || character === mask[pos]; | ||
}; | ||
if (isPermanentChar(maskOptions, i)) { | ||
return mask[i]; | ||
var isUsableCharacter = function isUsableCharacter(character, pos) { | ||
return !maskChar || !isPermanentChar(maskOptions, pos) || character !== maskChar; | ||
}; | ||
if (!maskChar && insertPos > value.length) { | ||
value += mask.slice(value.length, insertPos); | ||
} | ||
return maskChar; | ||
}).join(''); | ||
} | ||
function insertString(maskOptions, value, insertStr, insertPos) { | ||
var mask = maskOptions.mask, | ||
maskChar = maskOptions.maskChar, | ||
prefix = maskOptions.prefix; | ||
var arrayInsertStr = insertStr.split(''); | ||
var isInputFilled = isFilled(maskOptions, value); | ||
arrayInsertStr.every(function (insertCharacter) { | ||
while (!isUsablePosition(insertPos, insertCharacter)) { | ||
if (insertPos >= value.length) { | ||
value += mask[insertPos]; | ||
} | ||
var isUsablePosition = function isUsablePosition(pos, character) { | ||
return !isPermanentChar(maskOptions, pos) || character === mask[pos]; | ||
}; | ||
if (!isUsableCharacter(insertCharacter, insertPos)) { | ||
return true; | ||
} | ||
var isUsableCharacter = function isUsableCharacter(character, pos) { | ||
return !maskChar || !isPermanentChar(maskOptions, pos) || character !== maskChar; | ||
}; | ||
insertPos++; // stop iteration if maximum value length reached | ||
if (!maskChar && insertPos > value.length) { | ||
value += mask.slice(value.length, insertPos); | ||
} | ||
arrayInsertStr.every(function (insertCharacter) { | ||
while (!isUsablePosition(insertPos, insertCharacter)) { | ||
if (insertPos >= value.length) { | ||
value += mask[insertPos]; | ||
if (insertPos >= mask.length) { | ||
return false; | ||
} | ||
} | ||
if (!isUsableCharacter(insertCharacter, insertPos)) { | ||
var isAllowed = isAllowedChar(maskOptions, insertPos, insertCharacter) || insertCharacter === maskChar; | ||
if (!isAllowed) { | ||
return true; | ||
} | ||
if (insertPos < value.length) { | ||
if (maskChar || isInputFilled || insertPos < prefix.length) { | ||
value = value.slice(0, insertPos) + insertCharacter + value.slice(insertPos + 1); | ||
} else { | ||
value = value.slice(0, insertPos) + insertCharacter + value.slice(insertPos); | ||
value = formatValue(maskOptions, value); | ||
} | ||
} else if (!maskChar) { | ||
value += insertCharacter; | ||
} | ||
insertPos++; // stop iteration if maximum value length reached | ||
if (insertPos >= mask.length) { | ||
return false; | ||
} | ||
} | ||
return insertPos < mask.length; | ||
}); | ||
return value; | ||
} | ||
function getInsertStringLength(maskOptions, value, insertStr, insertPos) { | ||
var mask = maskOptions.mask, | ||
maskChar = maskOptions.maskChar; | ||
var arrayInsertStr = insertStr.split(''); | ||
var initialInsertPos = insertPos; | ||
var isAllowed = isAllowedChar(maskOptions, insertPos, insertCharacter) || insertCharacter === maskChar; | ||
var isUsablePosition = function isUsablePosition(pos, character) { | ||
return !isPermanentChar(maskOptions, pos) || character === mask[pos]; | ||
}; | ||
if (!isAllowed) { | ||
return true; | ||
} | ||
arrayInsertStr.every(function (insertCharacter) { | ||
while (!isUsablePosition(insertPos, insertCharacter)) { | ||
insertPos++; // stop iteration if maximum value length reached | ||
if (insertPos < value.length) { | ||
if (maskChar || isInputFilled || insertPos < prefix.length) { | ||
value = value.slice(0, insertPos) + insertCharacter + value.slice(insertPos + 1); | ||
} else { | ||
value = value.slice(0, insertPos) + insertCharacter + value.slice(insertPos); | ||
value = formatValue(maskOptions, value); | ||
if (insertPos >= mask.length) { | ||
return false; | ||
} | ||
} | ||
} else if (!maskChar) { | ||
value += insertCharacter; | ||
} | ||
insertPos++; // stop iteration if maximum value length reached | ||
var isAllowed = isAllowedChar(maskOptions, insertPos, insertCharacter) || insertCharacter === maskChar; | ||
return insertPos < mask.length; | ||
}); | ||
return value; | ||
} | ||
function getInsertStringLength(maskOptions, value, insertStr, insertPos) { | ||
var mask = maskOptions.mask, | ||
maskChar = maskOptions.maskChar; | ||
var arrayInsertStr = insertStr.split(''); | ||
var initialInsertPos = insertPos; | ||
if (isAllowed) { | ||
insertPos++; | ||
} // stop iteration if maximum value length reached | ||
var isUsablePosition = function isUsablePosition(pos, character) { | ||
return !isPermanentChar(maskOptions, pos) || character === mask[pos]; | ||
}; | ||
arrayInsertStr.every(function (insertCharacter) { | ||
while (!isUsablePosition(insertPos, insertCharacter)) { | ||
insertPos++; // stop iteration if maximum value length reached | ||
if (insertPos >= mask.length) { | ||
return false; | ||
return insertPos < mask.length; | ||
}); | ||
return insertPos - initialInsertPos; | ||
} | ||
function getLeftEditablePos(maskOptions, pos) { | ||
for (var i = pos; i >= 0; --i) { | ||
if (!isPermanentChar(maskOptions, i)) { | ||
return i; | ||
} | ||
} | ||
var isAllowed = isAllowedChar(maskOptions, insertPos, insertCharacter) || insertCharacter === maskChar; | ||
return null; | ||
} | ||
function getRightEditablePos(maskOptions, pos) { | ||
var mask = maskOptions.mask; | ||
if (isAllowed) { | ||
insertPos++; | ||
} // stop iteration if maximum value length reached | ||
for (var i = pos; i < mask.length; ++i) { | ||
if (!isPermanentChar(maskOptions, i)) { | ||
return i; | ||
} | ||
} | ||
return null; | ||
} | ||
function getStringValue(value) { | ||
return !value && value !== 0 ? '' : value + ''; | ||
} | ||
return insertPos < mask.length; | ||
}); | ||
return insertPos - initialInsertPos; | ||
} | ||
function isDOMElement(element) { | ||
return typeof HTMLElement === 'object' ? element instanceof HTMLElement // DOM2 | ||
: element.nodeType === 1 && typeof element.nodeName === 'string'; | ||
} | ||
function isFunction(value) { | ||
return typeof value === 'function'; | ||
} | ||
function defer (fn) { | ||
var defer = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function () { | ||
return setTimeout(fn, 0); | ||
}; | ||
function defer (fn, timeoutDelay) { | ||
if (timeoutDelay === void 0) { | ||
timeoutDelay = 0; | ||
} | ||
return defer(fn); | ||
} | ||
var defer = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function () { | ||
return setTimeout(fn, timeoutDelay); | ||
}; | ||
// https://github.com/sanniassin/react-input-mask | ||
var InputElement = | ||
/*#__PURE__*/ | ||
function (_React$Component) { | ||
_inheritsLoose(InputElement, _React$Component); | ||
return defer(fn); | ||
} | ||
function InputElement(props) { | ||
var _this; | ||
var InputElement = | ||
/*#__PURE__*/ | ||
function (_React$Component) { | ||
function InputElement(props) { | ||
var _this; | ||
_this = _React$Component.call(this, props) || this; | ||
_this = _React$Component.call(this, props) || this; | ||
_initialiseProps.call(_assertThisInitialized(_this)); | ||
_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "focused", false), "mounted", false), "previousSelection", null), "saveSelectionLoop", function () { | ||
if (!_this.mounted || !_this.maskOptions.mask || !_this.focused || _this.saveSelectionLoopRunning) { | ||
return; | ||
} | ||
var mask = props.mask, | ||
maskChar = props.maskChar, | ||
formatChars = props.formatChars, | ||
defaultValue = props.defaultValue, | ||
value = props.value, | ||
alwaysShowMask = props.alwaysShowMask; | ||
_this.hasValue = value != null; | ||
_this.maskOptions = parseMask(mask, maskChar, formatChars); | ||
_this.saveSelectionLoopRunning = true; | ||
_this.previousSelection = _this.getSelection(); | ||
defer(function () { | ||
_this.saveSelectionLoopRunning = false; | ||
if (defaultValue == null) { | ||
defaultValue = ''; | ||
} | ||
_this.saveSelectionLoop(); | ||
}, 1000 / 60); | ||
}), "getInputDOMNode", function () { | ||
var input = _this.input; | ||
if (value == null) { | ||
value = defaultValue; | ||
} | ||
if (!input) { | ||
return null; | ||
} | ||
value = _this.getStringValue(value); | ||
if (isDOMElement(input)) { | ||
return input; | ||
} // React 0.13 | ||
if (_this.maskOptions.mask && (alwaysShowMask || value)) { | ||
value = formatValue(_this.maskOptions, value); | ||
} | ||
_this.value = value; | ||
return _this; | ||
} | ||
return React.findDOMNode(input); | ||
}), "getInputValue", function () { | ||
var input = _this.getInputDOMNode(); | ||
var _proto = InputElement.prototype; | ||
if (!input) { | ||
return null; | ||
} | ||
_proto.componentDidMount = function componentDidMount() { | ||
this.isAndroidBrowser = isAndroidBrowser(); | ||
this.isWindowsPhoneBrowser = isWindowsPhoneBrowser(); | ||
this.isAndroidFirefox = isAndroidFirefox(); | ||
return input.value; | ||
}), "setInputValue", function (value) { | ||
var input = _this.getInputDOMNode(); | ||
if (this.maskOptions.mask && this.getInputValue() !== this.value) { | ||
this.setInputValue(this.value); | ||
} | ||
}; | ||
if (!input) { | ||
return; | ||
} | ||
_proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { | ||
var oldMaskOptions = this.maskOptions; | ||
this.hasValue = nextProps.value != null; | ||
this.maskOptions = parseMask(nextProps.mask, nextProps.maskChar, nextProps.formatChars); | ||
_this.value = value; | ||
input.value = value; | ||
}), "setCursorToEnd", function () { | ||
var filledLen = getFilledLength(_this.maskOptions, _this.value); | ||
var pos = getRightEditablePos(_this.maskOptions, filledLen); | ||
if (!this.maskOptions.mask) { | ||
this.backspaceOrDeleteRemoval = null; | ||
this.lastCursorPos = null; | ||
return; | ||
} | ||
if (pos !== null) { | ||
_this.setCursorPos(pos); | ||
} | ||
}), "setSelection", function (start, end) { | ||
var input = _this.getInputDOMNode(); | ||
var isMaskChanged = this.maskOptions.mask && this.maskOptions.mask !== oldMaskOptions.mask; | ||
var showEmpty = nextProps.alwaysShowMask || this.isFocused(); | ||
var newValue = this.hasValue ? this.getStringValue(nextProps.value) : this.value; | ||
if (!input) { | ||
return; | ||
} | ||
if (!oldMaskOptions.mask && !this.hasValue) { | ||
newValue = this.getInputValue(); | ||
} | ||
setInputSelection(input, start, end); | ||
defer(function () { | ||
setInputSelection(input, start, end); | ||
}); | ||
_this.previousSelection = { | ||
start: start, | ||
end: end, | ||
length: Math.abs(end - start) | ||
}; | ||
}), "getSelection", function () { | ||
var input = _this.getInputDOMNode(); | ||
if (isMaskChanged || this.maskOptions.mask && (newValue || showEmpty)) { | ||
newValue = formatValue(this.maskOptions, newValue); | ||
return getInputSelection(input); | ||
}), "getCursorPos", function () { | ||
return _this.getSelection().start; | ||
}), "setCursorPos", function (pos) { | ||
_this.setSelection(pos, pos); | ||
}), "isFocused", function () { | ||
return _this.focused; | ||
}), "getModifyMaskedValueConfig", function () { | ||
var _this$maskOptions = _this.maskOptions, | ||
mask = _this$maskOptions.mask, | ||
maskChar = _this$maskOptions.maskChar, | ||
permanents = _this$maskOptions.permanents, | ||
formatChars = _this$maskOptions.formatChars; | ||
var alwaysShowMask = _this.props.alwaysShowMask; | ||
return { | ||
mask: mask, | ||
maskChar: maskChar, | ||
permanents: permanents, | ||
alwaysShowMask: !!alwaysShowMask, | ||
formatChars: formatChars | ||
}; | ||
}), "isInputAutofilled", function () { | ||
var input = _this.getInputDOMNode(); | ||
if (isMaskChanged) { | ||
var pos = this.lastCursorPos; | ||
var filledLen = getFilledLength(this.maskOptions, newValue); | ||
var isAutofilled = false; // input.matches throws an exception if selector isn't supported | ||
if (pos === null || filledLen < pos) { | ||
if (isFilled(this.maskOptions, newValue)) { | ||
pos = filledLen; | ||
} else { | ||
pos = this.getRightEditablePos(filledLen); | ||
try { | ||
if (isFunction(input.matches) && input.matches(':-webkit-autofill')) { | ||
isAutofilled = true; | ||
} | ||
} catch (e) {} | ||
this.setCursorPos(pos); | ||
} | ||
} | ||
} | ||
return isAutofilled; | ||
}), "onChange", function (event) { | ||
var _assertThisInitialize = _assertThisInitialized(_assertThisInitialized(_this)), | ||
beforePasteState = _assertThisInitialize.beforePasteState, | ||
previousSelection = _assertThisInitialize.previousSelection; | ||
if (this.maskOptions.mask && isEmpty(this.maskOptions, newValue) && !showEmpty && (!this.hasValue || !nextProps.value)) { | ||
newValue = ''; | ||
} | ||
var beforeMaskedValueChange = _this.props.beforeMaskedValueChange; | ||
var _this$maskOptions2 = _this.maskOptions, | ||
mask = _this$maskOptions2.mask, | ||
prefix = _this$maskOptions2.prefix, | ||
lastEditablePos = _this$maskOptions2.lastEditablePos; | ||
this.value = newValue; | ||
}; | ||
var value = _this.getInputValue(); | ||
_proto.componentDidUpdate = function componentDidUpdate() { | ||
if (this.maskOptions.mask && this.getInputValue() !== this.value) { | ||
this.setInputValue(this.value); | ||
} | ||
}; | ||
var newValue = value; | ||
var previousValue = _this.value; // autofill replaces entire value, ignore old one | ||
// https://github.com/sanniassin/react-input-mask/issues/113 | ||
_proto.render = function render() { | ||
var _this2 = this; | ||
if (_this.isInputAutofilled()) { | ||
previousValue = formatValue(_this.maskOptions, ''); | ||
previousSelection = { | ||
start: 0, | ||
end: 0, | ||
length: 0 | ||
}; | ||
} | ||
var _props = this.props, | ||
mask = _props.mask, | ||
alwaysShowMask = _props.alwaysShowMask, | ||
maskChar = _props.maskChar, | ||
formatChars = _props.formatChars, | ||
inputRef = _props.inputRef, | ||
props = _objectWithoutProperties(_props, ["mask", "alwaysShowMask", "maskChar", "formatChars", "inputRef"]); | ||
var enteredString = ''; | ||
if (this.maskOptions.mask) { | ||
if (!props.disabled && !props.readOnly) { | ||
var handlersKeys = ['onChange', 'onKeyDown', 'onPaste', 'onMouseDown']; | ||
handlersKeys.forEach(function (key) { | ||
props[key] = _this2[key]; | ||
}); | ||
} | ||
var selection = _this.getSelection(); | ||
if (props.value != null) { | ||
props.value = this.value; | ||
} | ||
} | ||
var cursorPos = selection.end; | ||
selection.start = selection.end; | ||
selection.length = 0; // set value and selection as if we haven't | ||
// cleared input in onPaste handler | ||
return React.createElement("input", _extends({ | ||
ref: this.handleRef | ||
}, props, { | ||
onFocus: this.onFocus, | ||
onBlur: this.onBlur | ||
})); | ||
}; | ||
if (beforePasteState) { | ||
previousSelection = beforePasteState.selection; | ||
previousValue = beforePasteState.value; | ||
cursorPos = previousSelection.start + newValue.length; | ||
selection = { | ||
start: cursorPos, | ||
end: cursorPos, | ||
length: 0 | ||
}; | ||
newValue = previousValue.slice(0, previousSelection.start) + newValue + previousValue.slice(previousSelection.end); | ||
_this.beforePasteState = null; | ||
} | ||
return InputElement; | ||
}(React.Component); | ||
var formattedEnteredStringLen = 0; | ||
var removedLen = previousSelection.length; | ||
cursorPos = Math.min(previousSelection.start, selection.start); | ||
var _initialiseProps = function _initialiseProps() { | ||
var _this3 = this; | ||
if (selection.end > previousSelection.start) { | ||
enteredString = newValue.slice(previousSelection.start, selection.end); | ||
formattedEnteredStringLen = getInsertStringLength(_this.maskOptions, previousValue, enteredString, cursorPos); | ||
Object.defineProperty(this, "lastCursorPos", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: null | ||
}); | ||
Object.defineProperty(this, "focused", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: false | ||
}); | ||
Object.defineProperty(this, "isDOMElement", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(element) { | ||
return (typeof HTMLElement === "undefined" ? "undefined" : _typeof(HTMLElement)) === 'object' ? _instanceof(element, HTMLElement) // DOM2 | ||
: element.nodeType === 1 && typeof element.nodeName === 'string'; | ||
} | ||
}); | ||
Object.defineProperty(this, "getInputDOMNode", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value() { | ||
var input = _this3.input; | ||
if (!formattedEnteredStringLen) { | ||
removedLen = 0; | ||
} | ||
} else if (newValue.length < previousValue.length) { | ||
removedLen = previousValue.length - newValue.length; | ||
} | ||
if (!input) { | ||
return null; | ||
} | ||
newValue = previousValue; | ||
if (_this3.isDOMElement(input)) { | ||
return input; | ||
} // React 0.13 | ||
if (removedLen) { | ||
if (removedLen === 1 && !previousSelection.length) { | ||
var deleteFromRight = previousSelection.start === selection.start; | ||
cursorPos = deleteFromRight ? getRightEditablePos(_this.maskOptions, selection.start) : getLeftEditablePos(_this.maskOptions, selection.start); | ||
} | ||
newValue = clearRange(_this.maskOptions, newValue, cursorPos, removedLen); | ||
} | ||
return React.findDOMNode(input); | ||
} | ||
}); | ||
Object.defineProperty(this, "getInputValue", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value() { | ||
var input = _this3.getInputDOMNode(); | ||
newValue = insertString(_this.maskOptions, newValue, enteredString, cursorPos); | ||
cursorPos = cursorPos + formattedEnteredStringLen; | ||
if (!input) { | ||
return null; | ||
} | ||
if (cursorPos >= mask.length) { | ||
cursorPos = mask.length; | ||
} else if (cursorPos < prefix.length && !formattedEnteredStringLen) { | ||
cursorPos = prefix.length; | ||
} else if (cursorPos >= prefix.length && cursorPos < lastEditablePos && formattedEnteredStringLen) { | ||
cursorPos = getRightEditablePos(_this.maskOptions, cursorPos); | ||
} | ||
return input.value; | ||
} | ||
}); | ||
Object.defineProperty(this, "setInputValue", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(_value) { | ||
var input = _this3.getInputDOMNode(); | ||
newValue = formatValue(_this.maskOptions, newValue); | ||
if (!input) { | ||
return; | ||
} | ||
_this3.value = _value; | ||
input.value = _value; | ||
} | ||
}); | ||
Object.defineProperty(this, "getLeftEditablePos", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(pos) { | ||
for (var i = pos; i >= 0; --i) { | ||
if (!isPermanentChar(_this3.maskOptions, i)) { | ||
return i; | ||
if (!enteredString) { | ||
enteredString = null; | ||
} | ||
} | ||
return null; | ||
} | ||
}); | ||
Object.defineProperty(this, "getRightEditablePos", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(pos) { | ||
var mask = _this3.maskOptions.mask; | ||
var newSelection = { | ||
start: cursorPos, | ||
end: cursorPos | ||
}; | ||
for (var i = pos; i < mask.length; ++i) { | ||
if (!isPermanentChar(_this3.maskOptions, i)) { | ||
return i; | ||
if (isFunction(beforeMaskedValueChange)) { | ||
var modifiedValue = beforeMaskedValueChange({ | ||
value: newValue, | ||
selection: newSelection | ||
}, { | ||
value: previousValue, | ||
selection: previousSelection | ||
}, enteredString, _this.getModifyMaskedValueConfig()); | ||
newValue = modifiedValue.value; | ||
newSelection = modifiedValue.selection; | ||
} | ||
} | ||
return null; | ||
} | ||
}); | ||
Object.defineProperty(this, "setCursorToEnd", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value() { | ||
var filledLen = getFilledLength(_this3.maskOptions, _this3.value); | ||
_this.setInputValue(newValue); | ||
var pos = _this3.getRightEditablePos(filledLen); | ||
if (isFunction(_this.props.onChange)) { | ||
_this.props.onChange(event); | ||
} | ||
if (pos !== null) { | ||
_this3.setCursorPos(pos); | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "setSelection", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(start, len) { | ||
if (len === void 0) { | ||
len = 0; | ||
} | ||
if (_this.isWindowsPhoneBrowser) { | ||
defer(function () { | ||
if (!_this.mounted || !_this.isFocused()) { | ||
return; | ||
} | ||
var input = _this3.getInputDOMNode(); | ||
var input = _this.getInputDOMNode(); | ||
if (!input) { | ||
return; | ||
} | ||
var end = start + len; | ||
if ('selectionStart' in input && 'selectionEnd' in input) { | ||
input.selectionStart = start; | ||
input.selectionEnd = end; | ||
} else { | ||
var range = input.createTextRange(); | ||
range.collapse(true); | ||
range.moveStart('character', start); | ||
range.moveEnd('character', end - start); | ||
range.select(); | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "getSelection", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value() { | ||
var input = _this3.getInputDOMNode(); | ||
var start = 0; | ||
var end = 0; | ||
if ('selectionStart' in input && 'selectionEnd' in input) { | ||
start = input.selectionStart; | ||
end = input.selectionEnd; | ||
} else { | ||
var range = document.selection.createRange(); | ||
if (range.parentElement() === input) { | ||
start = -range.moveStart('character', -input.value.length); | ||
end = -range.moveEnd('character', -input.value.length); | ||
setInputSelection(input, newSelection.start, newSelection.end); | ||
_this.previousSelection = _objectSpread({}, newSelection, { | ||
length: newSelection.end - newSelection.start | ||
}); | ||
}); | ||
} else { | ||
_this.setSelection(newSelection.start, newSelection.end); | ||
} | ||
} | ||
}), "onFocus", function (event) { | ||
var beforeMaskedValueChange = _this.props.beforeMaskedValueChange; | ||
var _this$maskOptions3 = _this.maskOptions, | ||
mask = _this$maskOptions3.mask, | ||
prefix = _this$maskOptions3.prefix; | ||
_this.focused = true; | ||
return { | ||
start: start, | ||
end: end, | ||
length: end - start | ||
}; | ||
} | ||
}); | ||
Object.defineProperty(this, "getCursorPos", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value() { | ||
return _this3.getSelection().start; | ||
} | ||
}); | ||
Object.defineProperty(this, "setCursorPos", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(pos) { | ||
_this3.setSelection(pos, 0); | ||
if (mask) { | ||
if (!_this.value) { | ||
var emptyValue = formatValue(_this.maskOptions, prefix); | ||
var newValue = formatValue(_this.maskOptions, emptyValue); | ||
var filledLen = getFilledLength(_this.maskOptions, newValue); | ||
var cursorPos = getRightEditablePos(_this.maskOptions, filledLen); | ||
var newSelection = { | ||
start: cursorPos, | ||
end: cursorPos | ||
}; | ||
defer(function () { | ||
_this3.setSelection(pos, 0); | ||
}); | ||
_this3.lastCursorPos = pos; | ||
} | ||
}); | ||
Object.defineProperty(this, "isFocused", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value() { | ||
return _this3.focused; | ||
} | ||
}); | ||
Object.defineProperty(this, "getStringValue", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(_value2) { | ||
return !_value2 && _value2 !== 0 ? '' : _value2 + ''; | ||
} | ||
}); | ||
Object.defineProperty(this, "onKeyDown", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(event) { | ||
_this3.backspaceOrDeleteRemoval = null; | ||
if (isFunction(beforeMaskedValueChange)) { | ||
var modifiedValue = beforeMaskedValueChange({ | ||
value: newValue, | ||
selection: newSelection | ||
}, { | ||
value: _this.value, | ||
selection: null | ||
}, null, _this.getModifyMaskedValueConfig()); | ||
newValue = modifiedValue.value; | ||
newSelection = modifiedValue.selection; | ||
} // do not use this.getInputValue and this.setInputValue as this.input | ||
// will be undefined if it's an initial mount of input with autoFocus attribute | ||
if (typeof _this3.props.onKeyDown === 'function') { | ||
_this3.props.onKeyDown(event); | ||
} | ||
var key = event.key, | ||
ctrlKey = event.ctrlKey, | ||
metaKey = event.metaKey, | ||
defaultPrevented = event.defaultPrevented; | ||
var isInputValueChanged = newValue !== event.target.value; | ||
if (ctrlKey || metaKey || defaultPrevented) { | ||
return; | ||
} | ||
if (isInputValueChanged) { | ||
event.target.value = newValue; | ||
} | ||
if (key === 'Backspace' || key === 'Delete') { | ||
var selection = _this3.getSelection(); | ||
_this.value = newValue; | ||
var canRemove = key === 'Backspace' && selection.end > 0 || key === 'Delete' && _this3.value.length > selection.start; | ||
if (isInputValueChanged && isFunction(_this.props.onChange)) { | ||
_this.props.onChange(event); | ||
} | ||
if (!canRemove) { | ||
return; | ||
_this.setSelection(newSelection.start, newSelection.end); | ||
} else if (getFilledLength(_this.maskOptions, _this.value) < _this.maskOptions.mask.length) { | ||
_this.setCursorToEnd(); | ||
} | ||
} | ||
_this3.backspaceOrDeleteRemoval = { | ||
key: key, | ||
selection: _this3.getSelection() | ||
}; | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "onChange", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(event) { | ||
var beforePasteState = _this3.beforePasteState; | ||
var _this3$maskOptions = _this3.maskOptions, | ||
mask = _this3$maskOptions.mask, | ||
maskChar = _this3$maskOptions.maskChar, | ||
lastEditablePos = _this3$maskOptions.lastEditablePos, | ||
prefix = _this3$maskOptions.prefix; | ||
_this.saveSelectionLoop(); | ||
var value = _this3.getInputValue(); | ||
if (isFunction(_this.props.onFocus)) { | ||
_this.props.onFocus(event); | ||
} | ||
}), "onBlur", function (event) { | ||
var beforeMaskedValueChange = _this.props.beforeMaskedValueChange; | ||
var mask = _this.maskOptions.mask; | ||
_this.previousSelection = null; | ||
_this.focused = false; | ||
if (beforePasteState) { | ||
_this3.beforePasteState = null; | ||
if (mask && !_this.props.alwaysShowMask && isEmpty(_this.maskOptions, _this.value)) { | ||
var newValue = ''; | ||
_this3.pasteText(beforePasteState.value, value, beforePasteState.selection, event); | ||
if (isFunction(beforeMaskedValueChange)) { | ||
var modifiedValue = beforeMaskedValueChange({ | ||
value: newValue, | ||
selection: null | ||
}, { | ||
value: _this.value, | ||
selection: _this.previousSelection | ||
}, null, _this.getModifyMaskedValueConfig()); | ||
newValue = modifiedValue.value; | ||
} | ||
return; | ||
} | ||
var isInputValueChanged = newValue !== _this.getInputValue(); | ||
var oldValue = _this3.value; | ||
if (isInputValueChanged) { | ||
_this.setInputValue(newValue); | ||
} | ||
var input = _this3.getInputDOMNode(); // autofill replaces whole value, ignore old one | ||
// https://github.com/sanniassin/react-input-mask/issues/113 | ||
// | ||
// input.matches throws exception if selector isn't supported | ||
if (isInputValueChanged && isFunction(_this.props.onChange)) { | ||
_this.props.onChange(event); | ||
} | ||
} | ||
try { | ||
if (typeof input.matches === 'function' && input.matches(':-webkit-autofill')) { | ||
oldValue = ''; | ||
if (isFunction(_this.props.onBlur)) { | ||
_this.props.onBlur(event); | ||
} | ||
} catch (e) {} | ||
}), "onMouseDown", function (event) { | ||
// tiny unintentional mouse movements can break cursor | ||
// position on focus, so we have to restore it in that case | ||
// | ||
// https://github.com/sanniassin/react-input-mask/issues/108 | ||
if (!_this.focused && document.addEventListener) { | ||
_this.mouseDownX = event.clientX; | ||
_this.mouseDownY = event.clientY; | ||
_this.mouseDownTime = new Date().getTime(); | ||
var selection = _this3.getSelection(); | ||
var mouseUpHandler = function mouseUpHandler(mouseUpEvent) { | ||
document.removeEventListener('mouseup', mouseUpHandler); | ||
var cursorPos = selection.end; | ||
var maskLen = mask.length; | ||
var valueLen = value.length; | ||
var oldValueLen = oldValue.length; | ||
var clearedValue; | ||
var enteredString; | ||
if (!_this.focused) { | ||
return; | ||
} | ||
if (_this3.backspaceOrDeleteRemoval) { | ||
var deleteFromRight = _this3.backspaceOrDeleteRemoval.key === 'Delete'; | ||
value = _this3.value; | ||
selection = _this3.backspaceOrDeleteRemoval.selection; | ||
cursorPos = selection.start; | ||
_this3.backspaceOrDeleteRemoval = null; | ||
var deltaX = Math.abs(mouseUpEvent.clientX - _this.mouseDownX); | ||
var deltaY = Math.abs(mouseUpEvent.clientY - _this.mouseDownY); | ||
var axisDelta = Math.max(deltaX, deltaY); | ||
if (selection.length) { | ||
value = clearRange(_this3.maskOptions, value, selection.start, selection.length); | ||
} else if (selection.start < prefix.length || !deleteFromRight && selection.start === prefix.length) { | ||
cursorPos = prefix.length; | ||
} else { | ||
var editablePos = deleteFromRight ? _this3.getRightEditablePos(cursorPos) : _this3.getLeftEditablePos(cursorPos - 1); | ||
var timeDelta = new Date().getTime() - _this.mouseDownTime; | ||
if (editablePos !== null) { | ||
if (!maskChar) { | ||
value = value.substr(0, getFilledLength(_this3.maskOptions, value)); | ||
if (axisDelta <= 10 && timeDelta <= 200 || axisDelta <= 5 && timeDelta <= 300) { | ||
_this.setCursorToEnd(); | ||
} | ||
}; | ||
value = clearRange(_this3.maskOptions, value, editablePos, 1); | ||
cursorPos = editablePos; | ||
} | ||
document.addEventListener('mouseup', mouseUpHandler); | ||
} | ||
} else if (valueLen > oldValueLen) { | ||
var enteredStringLen = valueLen - oldValueLen; | ||
var startPos = selection.end - enteredStringLen; | ||
enteredString = value.substr(startPos, enteredStringLen); | ||
if (startPos < lastEditablePos && (enteredStringLen !== 1 || enteredString !== mask[startPos])) { | ||
cursorPos = _this3.getRightEditablePos(startPos); | ||
} else { | ||
cursorPos = startPos; | ||
if (isFunction(_this.props.onMouseDown)) { | ||
_this.props.onMouseDown(event); | ||
} | ||
}), "onPaste", function (event) { | ||
if (isFunction(_this.props.onPaste)) { | ||
_this.props.onPaste(event); | ||
} // event.clipboardData might not work in Android browser | ||
// cleaning input to get raw text inside onChange handler | ||
value = value.substr(0, startPos) + value.substr(startPos + enteredStringLen); | ||
clearedValue = clearRange(_this3.maskOptions, value, startPos, maskLen - startPos); | ||
clearedValue = insertString(_this3.maskOptions, clearedValue, enteredString, cursorPos); | ||
value = insertString(_this3.maskOptions, oldValue, enteredString, cursorPos); | ||
if (enteredStringLen !== 1 || cursorPos >= prefix.length && cursorPos < lastEditablePos) { | ||
cursorPos = Math.max(getFilledLength(_this3.maskOptions, clearedValue), cursorPos); | ||
if (!event.defaultPrevented) { | ||
_this.beforePasteState = { | ||
value: _this.getInputValue(), | ||
selection: _this.getSelection() | ||
}; | ||
if (cursorPos < lastEditablePos) { | ||
cursorPos = _this3.getRightEditablePos(cursorPos); | ||
} | ||
} else if (cursorPos < lastEditablePos) { | ||
cursorPos++; | ||
_this.setInputValue(''); | ||
} | ||
} else if (valueLen < oldValueLen) { | ||
var removedLen = maskLen - valueLen; | ||
enteredString = value.substr(0, selection.end); | ||
var clearOnly = enteredString === oldValue.substr(0, selection.end); | ||
clearedValue = clearRange(_this3.maskOptions, oldValue, selection.end, removedLen); | ||
}), "handleRef", function (ref) { | ||
_this.input = ref; | ||
if (maskChar) { | ||
value = insertString(_this3.maskOptions, clearedValue, enteredString, 0); | ||
if (isFunction(_this.props.inputRef)) { | ||
_this.props.inputRef(ref); | ||
} | ||
}); | ||
clearedValue = clearRange(_this3.maskOptions, clearedValue, selection.end, maskLen - selection.end); | ||
clearedValue = insertString(_this3.maskOptions, clearedValue, enteredString, 0); | ||
var _mask = props.mask, | ||
_maskChar = props.maskChar, | ||
_formatChars = props.formatChars, | ||
defaultValue = props.defaultValue, | ||
_value = props.value, | ||
_alwaysShowMask = props.alwaysShowMask; | ||
_this.hasValue = _value != null; | ||
_this.maskOptions = parseMask(_mask, _maskChar, _formatChars); | ||
if (!clearOnly) { | ||
cursorPos = Math.max(getFilledLength(_this3.maskOptions, clearedValue), cursorPos); | ||
if (defaultValue == null) { | ||
defaultValue = ''; | ||
} | ||
if (cursorPos < lastEditablePos) { | ||
cursorPos = _this3.getRightEditablePos(cursorPos); | ||
} | ||
} else if (cursorPos < prefix.length) { | ||
cursorPos = prefix.length; | ||
} | ||
if (_value == null) { | ||
_value = defaultValue; | ||
} | ||
value = formatValue(_this3.maskOptions, value); | ||
_value = getStringValue(_value); | ||
_this3.setInputValue(value); | ||
if (typeof _this3.props.onChange === 'function') { | ||
_this3.props.onChange(event); | ||
if (_this.maskOptions.mask && (_alwaysShowMask || _value)) { | ||
_value = formatValue(_this.maskOptions, _value); | ||
} | ||
if (_this3.isWindowsPhoneBrowser) { | ||
defer(function () { | ||
_this3.setSelection(cursorPos, 0); | ||
}); | ||
} else { | ||
_this3.setCursorPos(cursorPos); | ||
} | ||
_this.value = _value; | ||
return _this; | ||
} | ||
}); | ||
Object.defineProperty(this, "onFocus", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(event) { | ||
_this3.focused = true; | ||
if (_this3.maskOptions.mask) { | ||
if (!_this3.value) { | ||
var prefix = _this3.maskOptions.prefix; | ||
var value = formatValue(_this3.maskOptions, prefix); | ||
var inputValue = formatValue(_this3.maskOptions, value); // do not use this.getInputValue and this.setInputValue as this.input | ||
// can be undefined at this moment if autoFocus attribute is set | ||
var _proto = InputElement.prototype; | ||
var isInputValueChanged = inputValue !== event.target.value; | ||
_proto.componentDidMount = function componentDidMount() { | ||
this.mounted = true; | ||
this.isWindowsPhoneBrowser = isWindowsPhoneBrowser(); | ||
if (isInputValueChanged) { | ||
event.target.value = inputValue; | ||
} | ||
if (this.maskOptions.mask && this.getInputValue() !== this.value) { | ||
this.setInputValue(this.value); | ||
} | ||
_this3.value = inputValue; | ||
if (this.props.maxLength && this.maskOptions.mask && typeof console === 'object' && console.error) { | ||
console.error('react-input-mask: You shouldn\'t pass maxLength property to the masked input. It breaks masking and unnecessary because length is limited by the mask length.'); | ||
} | ||
}; | ||
if (isInputValueChanged && typeof _this3.props.onChange === 'function') { | ||
_this3.props.onChange(event); | ||
} | ||
_proto.componentDidUpdate = function componentDidUpdate() { | ||
var previousSelection = this.previousSelection; | ||
var beforeMaskedValueChange = this.props.beforeMaskedValueChange; | ||
var oldMaskOptions = this.maskOptions; | ||
this.hasValue = this.props.value != null; | ||
this.maskOptions = parseMask(this.props.mask, this.props.maskChar, this.props.formatChars); | ||
_this3.setCursorToEnd(); | ||
} else if (getFilledLength(_this3.maskOptions, _this3.value) < _this3.maskOptions.mask.length) { | ||
_this3.setCursorToEnd(); | ||
} | ||
if (!this.maskOptions.mask) { | ||
this.backspaceOrDeleteRemoval = null; | ||
this.previousSelection = null; | ||
return; | ||
} | ||
if (typeof _this3.props.onFocus === 'function') { | ||
_this3.props.onFocus(event); | ||
var cursorPos = previousSelection ? previousSelection.start : null; | ||
var isMaskChanged = this.maskOptions.mask && this.maskOptions.mask !== oldMaskOptions.mask; | ||
var showEmpty = this.props.alwaysShowMask || this.isFocused(); | ||
var newValue = this.hasValue ? getStringValue(this.props.value) : this.value; | ||
if (!oldMaskOptions.mask && !this.hasValue) { | ||
newValue = this.getInputValue(); | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "onBlur", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(event) { | ||
_this3.focused = false; | ||
if (_this3.maskOptions.mask && !_this3.props.alwaysShowMask && isEmpty(_this3.maskOptions, _this3.value)) { | ||
var inputValue = ''; | ||
if (isMaskChanged || this.maskOptions.mask && (newValue || showEmpty)) { | ||
newValue = formatValue(this.maskOptions, newValue); | ||
var isInputValueChanged = inputValue !== _this3.getInputValue(); | ||
if (isMaskChanged) { | ||
var filledLen = getFilledLength(this.maskOptions, newValue); | ||
if (isInputValueChanged) { | ||
_this3.setInputValue(inputValue); | ||
} | ||
if (cursorPos === null || filledLen < cursorPos) { | ||
if (isFilled(this.maskOptions, newValue)) { | ||
cursorPos = filledLen; | ||
} else { | ||
cursorPos = getRightEditablePos(this.maskOptions, filledLen); | ||
} | ||
} | ||
if (isInputValueChanged && typeof _this3.props.onChange === 'function') { | ||
_this3.props.onChange(event); | ||
if (!oldMaskOptions.mask) { | ||
this.saveSelectionLoop(); | ||
} | ||
} | ||
} | ||
if (typeof _this3.props.onBlur === 'function') { | ||
_this3.props.onBlur(event); | ||
if (this.maskOptions.mask && isEmpty(this.maskOptions, newValue) && !showEmpty && (!this.hasValue || !this.props.value)) { | ||
newValue = ''; | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "onMouseDown", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(event) { | ||
// tiny unintentional mouse movements can break cursor | ||
// position on focus, so we have to restore it in that case | ||
// | ||
// https://github.com/sanniassin/react-input-mask/issues/108 | ||
if (!_this3.focused && document.addEventListener) { | ||
_this3.mouseDownX = event.clientX; | ||
_this3.mouseDownY = event.clientY; | ||
_this3.mouseDownTime = new Date().getTime(); | ||
var mouseUpHandler = function mouseUpHandler(mouseUpEvent) { | ||
document.removeEventListener('mouseup', mouseUpHandler); | ||
var newSelection = { | ||
start: cursorPos, | ||
end: cursorPos | ||
}; | ||
if (!_this3.focused) { | ||
return; | ||
} | ||
if (isFunction(beforeMaskedValueChange)) { | ||
var modifiedValue = beforeMaskedValueChange({ | ||
value: newValue, | ||
selection: newSelection | ||
}, { | ||
value: this.value, | ||
selection: this.previousSelection | ||
}, null, this.getModifyMaskedValueConfig()); | ||
newValue = modifiedValue.value; | ||
newSelection = modifiedValue.selection; | ||
} | ||
var deltaX = Math.abs(mouseUpEvent.clientX - _this3.mouseDownX); | ||
var deltaY = Math.abs(mouseUpEvent.clientY - _this3.mouseDownY); | ||
var axisDelta = Math.max(deltaX, deltaY); | ||
this.value = newValue; | ||
var timeDelta = new Date().getTime() - _this3.mouseDownTime; | ||
if (this.maskOptions.mask && this.getInputValue() !== this.value) { | ||
this.setInputValue(this.value); | ||
this.forceUpdate(); | ||
} | ||
if (axisDelta <= 10 && timeDelta <= 200 || axisDelta <= 5 && timeDelta <= 300) { | ||
_this3.setCursorToEnd(); | ||
} | ||
}; | ||
var isSelectionChanged = !previousSelection || previousSelection.start !== newSelection.start || previousSelection.end !== newSelection.end; | ||
document.addEventListener('mouseup', mouseUpHandler); | ||
if (isSelectionChanged) { | ||
this.setSelection(newSelection.start, newSelection.end); | ||
} | ||
}; | ||
if (typeof _this3.props.onMouseDown === 'function') { | ||
_this3.props.onMouseDown(event); | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "onPaste", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(event) { | ||
if (typeof _this3.props.onPaste === 'function') { | ||
_this3.props.onPaste(event); | ||
} // we need raw pasted text, but event.clipboardData | ||
// may not work in Android browser, so we clean input | ||
// to get raw text in onChange handler | ||
_proto.componentWillUnmount = function componentWillUnmount() { | ||
this.mounted = false; | ||
}; | ||
_proto.render = function render() { | ||
var _this2 = this; | ||
if (!event.defaultPrevented) { | ||
_this3.beforePasteState = { | ||
value: _this3.getInputValue(), | ||
selection: _this3.getSelection() | ||
}; | ||
var _this$props = this.props, | ||
mask = _this$props.mask, | ||
alwaysShowMask = _this$props.alwaysShowMask, | ||
maskChar = _this$props.maskChar, | ||
formatChars = _this$props.formatChars, | ||
inputRef = _this$props.inputRef, | ||
beforeMaskedValueChange = _this$props.beforeMaskedValueChange, | ||
props = _objectWithoutProperties(_this$props, ["mask", "alwaysShowMask", "maskChar", "formatChars", "inputRef", "beforeMaskedValueChange"]); | ||
_this3.setInputValue(''); | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "pasteText", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(_value3, text, selection, event) { | ||
var cursorPos = selection.start; | ||
if (this.maskOptions.mask) { | ||
if (!props.disabled && !props.readOnly) { | ||
var handlersKeys = ['onChange', 'onPaste', 'onMouseDown']; | ||
handlersKeys.forEach(function (key) { | ||
props[key] = _this2[key]; | ||
}); | ||
} | ||
if (selection.length) { | ||
_value3 = clearRange(_this3.maskOptions, _value3, cursorPos, selection.length); | ||
if (props.value != null) { | ||
props.value = this.value; | ||
} | ||
} | ||
var textLen = getInsertStringLength(_this3.maskOptions, _value3, text, cursorPos); | ||
_value3 = insertString(_this3.maskOptions, _value3, text, cursorPos); | ||
cursorPos += textLen; | ||
cursorPos = _this3.getRightEditablePos(cursorPos) || cursorPos; | ||
return React.createElement("input", _extends({ | ||
ref: this.handleRef | ||
}, props, { | ||
onFocus: this.onFocus, | ||
onBlur: this.onBlur | ||
})); | ||
}; | ||
_this3.setInputValue(_value3); | ||
_inheritsLoose(InputElement, _React$Component); | ||
if (event && typeof _this3.props.onChange === 'function') { | ||
_this3.props.onChange(event); | ||
} | ||
return InputElement; | ||
}(React.Component); | ||
_this3.setCursorPos(cursorPos); | ||
} | ||
}); | ||
Object.defineProperty(this, "handleRef", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(ref) { | ||
_this3.input = ref; | ||
return InputElement; | ||
if (typeof _this3.props.inputRef === 'function') { | ||
_this3.props.inputRef(ref); | ||
} | ||
} | ||
}); | ||
}; | ||
return InputElement; | ||
}))); |
@@ -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(e){return(t="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})(e)}function n(){return(n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var a in n)Object.prototype.hasOwnProperty.call(n,a)&&(e[a]=n[a])}return e}).apply(this,arguments)}var a={9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},r="_";function o(e,t,n){if(t===undefined&&(t=r),null==n&&(n=a),!e||"string"!=typeof e)return{maskChar:t,charsRules:n,mask:null,prefix:null,lastEditablePos:null,permanents:[]};var o="",s="",i=[],l=!1,u=null;return e.split("").forEach(function(e){l||"\\"!==e?(l||!n[e]?(i.push(o.length),o.length===i.length-1&&(s+=e)):u=o.length+1,o+=e,l=!1):l=!0}),{maskChar:t,charsRules:n,prefix:s,mask:o,lastEditablePos:u,permanents:i}}function s(e,t){return-1!==e.permanents.indexOf(t)}function i(e,t,n){var a=e.mask,r=e.charsRules;if(!n)return!1;if(s(e,t))return a[t]===n;var o=r[a[t]];return new RegExp(o).test(n)}function l(e,t){return t.split("").every(function(t,n){return s(e,n)||!i(e,n,t)})}function u(e,t){var n=e.maskChar,a=e.prefix;if(!n){for(;t.length>a.length&&s(e,t.length-1);)t=t.slice(0,t.length-1);return t.length}for(var r=a.length,o=t.length;o>=a.length;o--){var l=t[o];if(!s(e,o)&&i(e,o,l)){r=o+1;break}}return r}function f(e,t){return u(e,t)===e.mask.length}function c(e,t){var n=e.maskChar,a=e.mask,r=e.prefix;if(!n){for((t=h(e,"",t,0)).length<r.length&&(t=r);t.length<a.length&&s(e,t.length);)t+=a[t.length];return t}if(t)return h(e,c(e,""),t,0);for(var o=0;o<a.length;o++)s(e,o)?t+=a[o]:t+=n;return t}function p(e,t,n,a){var r=n+a,o=e.maskChar,i=e.mask,l=e.prefix,u=t.split("");if(!o){for(var f=r;f<u.length;f++)s(e,f)&&(u[f]="");return n=Math.max(l.length,n),u.splice(n,r-n),t=u.join(""),c(e,t)}return u.map(function(t,a){return a<n||a>=r?t:s(e,a)?i[a]:o}).join("")}function h(e,t,n,a){var r=e.mask,o=e.maskChar,l=e.prefix,u=n.split(""),p=f(e,t),h=function(t,n){return!o||!s(e,n)||t!==o};return!o&&a>t.length&&(t+=r.slice(t.length,a)),u.every(function(n){for(;f=n,s(e,u=a)&&f!==r[u];){if(a>=t.length&&(t+=r[a]),!h(n,a))return!0;if(++a>=r.length)return!1}var u,f;return!i(e,a,n)&&n!==o||(a<t.length?o||p||a<l.length?t=t.slice(0,a)+n+t.slice(a+1):(t=t.slice(0,a)+n+t.slice(a),t=c(e,t)):o||(t+=n),++a<r.length)}),t}function m(e){return(window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(){return setTimeout(e,0)})(e)}var g=function(t){var a,r;function s(e){var n;n=t.call(this,e)||this,b.call(function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(n));var a=e.mask,r=e.maskChar,s=e.formatChars,i=e.defaultValue,l=e.value,u=e.alwaysShowMask;return n.hasValue=null!=l,n.maskOptions=o(a,r,s),null==i&&(i=""),null==l&&(l=i),l=n.getStringValue(l),n.maskOptions.mask&&(u||l)&&(l=c(n.maskOptions,l)),n.value=l,n}r=t,(a=s).prototype=Object.create(r.prototype),a.prototype.constructor=a,a.__proto__=r;var i=s.prototype;return i.componentDidMount=function(){var e,t,n,a;this.isAndroidBrowser=(e=new RegExp("windows","i"),t=new RegExp("firefox","i"),n=new RegExp("android","i"),a=navigator.userAgent,!e.test(a)&&!t.test(a)&&n.test(a)),this.isWindowsPhoneBrowser=function(){var e=new RegExp("windows","i"),t=new RegExp("phone","i"),n=navigator.userAgent;return e.test(n)&&t.test(n)}(),this.isAndroidFirefox=function(){var e=new RegExp("windows","i"),t=new RegExp("firefox","i"),n=new RegExp("android","i"),a=navigator.userAgent;return!e.test(a)&&t.test(a)&&n.test(a)}(),this.maskOptions.mask&&this.getInputValue()!==this.value&&this.setInputValue(this.value)},i.componentWillReceiveProps=function(e){var t=this.maskOptions;if(this.hasValue=null!=e.value,this.maskOptions=o(e.mask,e.maskChar,e.formatChars),!this.maskOptions.mask)return this.backspaceOrDeleteRemoval=null,void(this.lastCursorPos=null);var n=this.maskOptions.mask&&this.maskOptions.mask!==t.mask,a=e.alwaysShowMask||this.isFocused(),r=this.hasValue?this.getStringValue(e.value):this.value;if(t.mask||this.hasValue||(r=this.getInputValue()),(n||this.maskOptions.mask&&(r||a))&&(r=c(this.maskOptions,r),n)){var s=this.lastCursorPos,i=u(this.maskOptions,r);(null===s||i<s)&&(s=f(this.maskOptions,r)?i:this.getRightEditablePos(i),this.setCursorPos(s))}!this.maskOptions.mask||!l(this.maskOptions,r)||a||this.hasValue&&e.value||(r=""),this.value=r},i.componentDidUpdate=function(){this.maskOptions.mask&&this.getInputValue()!==this.value&&this.setInputValue(this.value)},i.render=function(){var t=this,a=this.props,r=(a.mask,a.alwaysShowMask,a.maskChar,a.formatChars,a.inputRef,function(e,t){if(null==e)return{};var n,a,r={},o=Object.keys(e);for(a=0;a<o.length;a++)n=o[a],t.indexOf(n)>=0||(r[n]=e[n]);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);for(a=0;a<s.length;a++)n=s[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}(a,["mask","alwaysShowMask","maskChar","formatChars","inputRef"]));if(this.maskOptions.mask){if(!r.disabled&&!r.readOnly){["onChange","onKeyDown","onPaste","onMouseDown"].forEach(function(e){r[e]=t[e]})}null!=r.value&&(r.value=this.value)}return e.createElement("input",n({ref:this.handleRef},r,{onFocus:this.onFocus,onBlur:this.onBlur}))},s}((e=e&&e.hasOwnProperty("default")?e["default"]:e).Component),b=function(){var n=this;Object.defineProperty(this,"lastCursorPos",{configurable:!0,enumerable:!0,writable:!0,value:null}),Object.defineProperty(this,"focused",{configurable:!0,enumerable:!0,writable:!0,value:!1}),Object.defineProperty(this,"isDOMElement",{configurable:!0,enumerable:!0,writable:!0,value:function(e){return"object"===("undefined"==typeof HTMLElement?"undefined":t(HTMLElement))?(n=e,null!=(a=HTMLElement)&&"undefined"!=typeof Symbol&&a[Symbol.hasInstance]?a[Symbol.hasInstance](n):n instanceof a):1===e.nodeType&&"string"==typeof e.nodeName;var n,a}}),Object.defineProperty(this,"getInputDOMNode",{configurable:!0,enumerable:!0,writable:!0,value:function(){var t=n.input;return t?n.isDOMElement(t)?t:e.findDOMNode(t):null}}),Object.defineProperty(this,"getInputValue",{configurable:!0,enumerable:!0,writable:!0,value:function(){var e=n.getInputDOMNode();return e?e.value:null}}),Object.defineProperty(this,"setInputValue",{configurable:!0,enumerable:!0,writable:!0,value:function(e){var t=n.getInputDOMNode();t&&(n.value=e,t.value=e)}}),Object.defineProperty(this,"getLeftEditablePos",{configurable:!0,enumerable:!0,writable:!0,value:function(e){for(var t=e;t>=0;--t)if(!s(n.maskOptions,t))return t;return null}}),Object.defineProperty(this,"getRightEditablePos",{configurable:!0,enumerable:!0,writable:!0,value:function(e){for(var t=n.maskOptions.mask,a=e;a<t.length;++a)if(!s(n.maskOptions,a))return a;return null}}),Object.defineProperty(this,"setCursorToEnd",{configurable:!0,enumerable:!0,writable:!0,value:function(){var e=u(n.maskOptions,n.value),t=n.getRightEditablePos(e);null!==t&&n.setCursorPos(t)}}),Object.defineProperty(this,"setSelection",{configurable:!0,enumerable:!0,writable:!0,value:function(e,t){void 0===t&&(t=0);var a=n.getInputDOMNode();if(a){var r=e+t;if("selectionStart"in a&&"selectionEnd"in a)a.selectionStart=e,a.selectionEnd=r;else{var o=a.createTextRange();o.collapse(!0),o.moveStart("character",e),o.moveEnd("character",r-e),o.select()}}}}),Object.defineProperty(this,"getSelection",{configurable:!0,enumerable:!0,writable:!0,value:function(){var e=n.getInputDOMNode(),t=0,a=0;if("selectionStart"in e&&"selectionEnd"in e)t=e.selectionStart,a=e.selectionEnd;else{var r=document.selection.createRange();r.parentElement()===e&&(t=-r.moveStart("character",-e.value.length),a=-r.moveEnd("character",-e.value.length))}return{start:t,end:a,length:a-t}}}),Object.defineProperty(this,"getCursorPos",{configurable:!0,enumerable:!0,writable:!0,value:function(){return n.getSelection().start}}),Object.defineProperty(this,"setCursorPos",{configurable:!0,enumerable:!0,writable:!0,value:function(e){n.setSelection(e,0),m(function(){n.setSelection(e,0)}),n.lastCursorPos=e}}),Object.defineProperty(this,"isFocused",{configurable:!0,enumerable:!0,writable:!0,value:function(){return n.focused}}),Object.defineProperty(this,"getStringValue",{configurable:!0,enumerable:!0,writable:!0,value:function(e){return e||0===e?e+"":""}}),Object.defineProperty(this,"onKeyDown",{configurable:!0,enumerable:!0,writable:!0,value:function(e){n.backspaceOrDeleteRemoval=null,"function"==typeof n.props.onKeyDown&&n.props.onKeyDown(e);var t=e.key,a=e.ctrlKey,r=e.metaKey,o=e.defaultPrevented;if(!(a||r||o||"Backspace"!==t&&"Delete"!==t)){var s=n.getSelection();if(!("Backspace"===t&&s.end>0||"Delete"===t&&n.value.length>s.start))return;n.backspaceOrDeleteRemoval={key:t,selection:n.getSelection()}}}}),Object.defineProperty(this,"onChange",{configurable:!0,enumerable:!0,writable:!0,value:function(e){var t=n.beforePasteState,a=n.maskOptions,r=a.mask,o=a.maskChar,s=a.lastEditablePos,i=a.prefix,l=n.getInputValue();if(t)return n.beforePasteState=null,void n.pasteText(t.value,l,t.selection,e);var f=n.value,g=n.getInputDOMNode();try{"function"==typeof g.matches&&g.matches(":-webkit-autofill")&&(f="")}catch(j){}var b,v,d=n.getSelection(),k=d.end,O=r.length,y=l.length,w=f.length;if(n.backspaceOrDeleteRemoval){var P="Delete"===n.backspaceOrDeleteRemoval.key;if(l=n.value,d=n.backspaceOrDeleteRemoval.selection,k=d.start,n.backspaceOrDeleteRemoval=null,d.length)l=p(n.maskOptions,l,d.start,d.length);else if(d.start<i.length||!P&&d.start===i.length)k=i.length;else{var E=P?n.getRightEditablePos(k):n.getLeftEditablePos(k-1);null!==E&&(o||(l=l.substr(0,u(n.maskOptions,l))),l=p(n.maskOptions,l,E,1),k=E)}}else if(y>w){var C=y-w,R=d.end-C;v=l.substr(R,C),k=R<s&&(1!==C||v!==r[R])?n.getRightEditablePos(R):R,l=l.substr(0,R)+l.substr(R+C),b=p(n.maskOptions,l,R,O-R),b=h(n.maskOptions,b,v,k),l=h(n.maskOptions,f,v,k),1!==C||k>=i.length&&k<s?(k=Math.max(u(n.maskOptions,b),k))<s&&(k=n.getRightEditablePos(k)):k<s&&k++}else if(y<w){var D=O-y,S=(v=l.substr(0,d.end))===f.substr(0,d.end);b=p(n.maskOptions,f,d.end,D),o&&(l=h(n.maskOptions,b,v,0)),b=p(n.maskOptions,b,d.end,O-d.end),b=h(n.maskOptions,b,v,0),S?k<i.length&&(k=i.length):(k=Math.max(u(n.maskOptions,b),k))<s&&(k=n.getRightEditablePos(k))}l=c(n.maskOptions,l),n.setInputValue(l),"function"==typeof n.props.onChange&&n.props.onChange(e),n.isWindowsPhoneBrowser?m(function(){n.setSelection(k,0)}):n.setCursorPos(k)}}),Object.defineProperty(this,"onFocus",{configurable:!0,enumerable:!0,writable:!0,value:function(e){if(n.focused=!0,n.maskOptions.mask)if(n.value)u(n.maskOptions,n.value)<n.maskOptions.mask.length&&n.setCursorToEnd();else{var t=n.maskOptions.prefix,a=c(n.maskOptions,t),r=c(n.maskOptions,a),o=r!==e.target.value;o&&(e.target.value=r),n.value=r,o&&"function"==typeof n.props.onChange&&n.props.onChange(e),n.setCursorToEnd()}"function"==typeof n.props.onFocus&&n.props.onFocus(e)}}),Object.defineProperty(this,"onBlur",{configurable:!0,enumerable:!0,writable:!0,value:function(e){if(n.focused=!1,n.maskOptions.mask&&!n.props.alwaysShowMask&&l(n.maskOptions,n.value)){var t=""!==n.getInputValue();t&&n.setInputValue(""),t&&"function"==typeof n.props.onChange&&n.props.onChange(e)}"function"==typeof n.props.onBlur&&n.props.onBlur(e)}}),Object.defineProperty(this,"onMouseDown",{configurable:!0,enumerable:!0,writable:!0,value:function(e){if(!n.focused&&document.addEventListener){n.mouseDownX=e.clientX,n.mouseDownY=e.clientY,n.mouseDownTime=(new Date).getTime();document.addEventListener("mouseup",function t(e){if(document.removeEventListener("mouseup",t),n.focused){var a=Math.abs(e.clientX-n.mouseDownX),r=Math.abs(e.clientY-n.mouseDownY),o=Math.max(a,r),s=(new Date).getTime()-n.mouseDownTime;(o<=10&&s<=200||o<=5&&s<=300)&&n.setCursorToEnd()}})}"function"==typeof n.props.onMouseDown&&n.props.onMouseDown(e)}}),Object.defineProperty(this,"onPaste",{configurable:!0,enumerable:!0,writable:!0,value:function(e){"function"==typeof n.props.onPaste&&n.props.onPaste(e),e.defaultPrevented||(n.beforePasteState={value:n.getInputValue(),selection:n.getSelection()},n.setInputValue(""))}}),Object.defineProperty(this,"pasteText",{configurable:!0,enumerable:!0,writable:!0,value:function(e,t,a,r){var o=a.start;a.length&&(e=p(n.maskOptions,e,o,a.length));var l,u,f,c,m,g,b,v=(l=n.maskOptions,u=t,f=o,c=l.mask,m=l.maskChar,g=u.split(""),b=f,g.every(function(e){for(;n=e,s(l,t=f)&&n!==c[t];)if(++f>=c.length)return!1;var t,n;return(i(l,f,e)||e===m)&&f++,f<c.length}),f-b);e=h(n.maskOptions,e,t,o),o+=v,o=n.getRightEditablePos(o)||o,n.setInputValue(e),r&&"function"==typeof n.props.onChange&&n.props.onChange(r),n.setCursorPos(o)}}),Object.defineProperty(this,"handleRef",{configurable:!0,enumerable:!0,writable:!0,value:function(e){n.input=e,"function"==typeof n.props.inputRef&&n.props.inputRef(e)}})};return g}); | ||
!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(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function n(){return(n=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}).apply(this,arguments)}function s(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function a(e,t,n){if("selectionStart"in e&&"selectionEnd"in e)e.selectionStart=t,e.selectionEnd=n;else{var s=e.createTextRange();s.collapse(!0),s.moveStart("character",t),s.moveEnd("character",n-t),s.select()}}var o={9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},r="_";function i(e,t,n){if(t===undefined&&(t=r),null==n&&(n=o),!e||"string"!=typeof e)return{maskChar:t,formatChars:n,mask:null,prefix:null,lastEditablePos:null,permanents:[]};var s="",a="",i=[],u=!1,l=null;return e.split("").forEach(function(e){u||"\\"!==e?(u||!n[e]?(i.push(s.length),s.length===i.length-1&&(a+=e)):l=s.length+1,s+=e,u=!1):u=!0}),{maskChar:t,formatChars:n,prefix:a,mask:s,lastEditablePos:l,permanents:i}}function u(e,t){return-1!==e.permanents.indexOf(t)}function l(e,t,n){var s=e.mask,a=e.formatChars;if(!n)return!1;if(u(e,t))return s[t]===n;var o=a[s[t]];return new RegExp(o).test(n)}function p(e,t){return t.split("").every(function(t,n){return u(e,n)||!l(e,n,t)})}function c(e,t){var n=e.maskChar,s=e.prefix;if(!n){for(;t.length>s.length&&u(e,t.length-1);)t=t.slice(0,t.length-1);return t.length}for(var a=s.length,o=t.length;o>=s.length;o--){var r=t[o];if(!u(e,o)&&l(e,o,r)){a=o+1;break}}return a}function h(e,t){return c(e,t)===e.mask.length}function f(e,t){var n=e.maskChar,s=e.mask,a=e.prefix;if(!n){for((t=m(e,"",t,0)).length<a.length&&(t=a);t.length<s.length&&u(e,t.length);)t+=s[t.length];return t}if(t)return m(e,f(e,""),t,0);for(var o=0;o<s.length;o++)u(e,o)?t+=s[o]:t+=n;return t}function m(e,t,n,s){var a=e.mask,o=e.maskChar,r=e.prefix,i=n.split(""),p=h(e,t),c=function(t,n){return!o||!u(e,n)||t!==o};return!o&&s>t.length&&(t+=a.slice(t.length,s)),i.every(function(n){for(;h=n,u(e,i=s)&&h!==a[i];){if(s>=t.length&&(t+=a[s]),!c(n,s))return!0;if(++s>=a.length)return!1}var i,h;return!l(e,s,n)&&n!==o||(s<t.length?o||p||s<r.length?t=t.slice(0,s)+n+t.slice(s+1):(t=t.slice(0,s)+n+t.slice(s),t=f(e,t)):o||(t+=n),++s<a.length)}),t}function v(e,t){for(var n=e.mask,s=t;s<n.length;++s)if(!u(e,s))return s;return null}function g(e){return e||0===e?e+"":""}function d(e){return"function"==typeof e}function k(e,t){return void 0===t&&(t=0),(window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(){return setTimeout(e,t)})(e)}return function(o){function r(n){var r;t(t(t(t(t(t(t(t(t(t(t(t(t(t(t(t(t(t(t(t(t(s(s(r=o.call(this,n)||this)),"focused",!1),"mounted",!1),"previousSelection",null),"saveSelectionLoop",function(){r.mounted&&r.maskOptions.mask&&r.focused&&!r.saveSelectionLoopRunning&&(r.saveSelectionLoopRunning=!0,r.previousSelection=r.getSelection(),k(function(){r.saveSelectionLoopRunning=!1,r.saveSelectionLoop()},1e3/60))}),"getInputDOMNode",function(){var t,n=r.input;return n?(t=n,("object"==typeof HTMLElement?t instanceof HTMLElement:1===t.nodeType&&"string"==typeof t.nodeName)?n:e.findDOMNode(n)):null}),"getInputValue",function(){var e=r.getInputDOMNode();return e?e.value:null}),"setInputValue",function(e){var t=r.getInputDOMNode();t&&(r.value=e,t.value=e)}),"setCursorToEnd",function(){var e=c(r.maskOptions,r.value),t=v(r.maskOptions,e);null!==t&&r.setCursorPos(t)}),"setSelection",function(e,t){var n=r.getInputDOMNode();n&&(a(n,e,t),k(function(){a(n,e,t)}),r.previousSelection={start:e,end:t,length:Math.abs(t-e)})}),"getSelection",function(){return function(e){var 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}}(r.getInputDOMNode())}),"getCursorPos",function(){return r.getSelection().start}),"setCursorPos",function(e){r.setSelection(e,e)}),"isFocused",function(){return r.focused}),"getModifyMaskedValueConfig",function(){var e=r.maskOptions,t=e.mask,n=e.maskChar,s=e.permanents,a=e.formatChars;return{mask:t,maskChar:n,permanents:s,alwaysShowMask:!!r.props.alwaysShowMask,formatChars:a}}),"isInputAutofilled",function(){var e=r.getInputDOMNode(),t=!1;try{d(e.matches)&&e.matches(":-webkit-autofill")&&(t=!0)}catch(n){}return t}),"onChange",function(e){var n=s(s(r)),o=n.beforePasteState,i=n.previousSelection,p=r.props.beforeMaskedValueChange,c=r.maskOptions,h=c.mask,g=c.prefix,O=c.lastEditablePos,w=r.getInputValue(),y=r.value;r.isInputAutofilled()&&(y=f(r.maskOptions,""),i={start:0,end:0,length:0});var b="",C=r.getSelection(),S=C.end;C.start=C.end,C.length=0,o&&(i=o.selection,y=o.value,C={start:S=i.start+w.length,end:S,length:0},w=y.slice(0,i.start)+w+y.slice(i.end),r.beforePasteState=null);var M=0,V=i.length;if(S=Math.min(i.start,C.start),C.end>i.start?(b=w.slice(i.start,C.end),(M=function(e,t,n,s){var a=e.mask,o=e.maskChar,r=n.split(""),i=s;return r.every(function(t){for(;r=t,u(e,n=s)&&r!==a[n];)if(++s>=a.length)return!1;var n,r;return(l(e,s,t)||t===o)&&s++,s<a.length}),s-i}(r.maskOptions,0,b,S))||(V=0)):w.length<y.length&&(V=y.length-w.length),w=y,V){if(1===V&&!i.length)S=i.start===C.start?v(r.maskOptions,C.start):function(e,t){for(var n=t;n>=0;--n)if(!u(e,n))return n;return null}(r.maskOptions,C.start);w=function(e,t,n,s){var a=n+s,o=e.maskChar,r=e.mask,i=e.prefix,l=t.split("");if(!o){for(var p=a;p<l.length;p++)u(e,p)&&(l[p]="");return n=Math.max(i.length,n),l.splice(n,a-n),t=l.join(""),f(e,t)}return l.map(function(t,s){return s<n||s>=a?t:u(e,s)?r[s]:o}).join("")}(r.maskOptions,w,S,V)}w=m(r.maskOptions,w,b,S),(S+=M)>=h.length?S=h.length:S<g.length&&!M?S=g.length:S>=g.length&&S<O&&M&&(S=v(r.maskOptions,S)),w=f(r.maskOptions,w),b||(b=null);var E={start:S,end:S};if(d(p)){var I=p({value:w,selection:E},{value:y,selection:i},b,r.getModifyMaskedValueConfig());w=I.value,E=I.selection}r.setInputValue(w),d(r.props.onChange)&&r.props.onChange(e),r.isWindowsPhoneBrowser?k(function(){r.mounted&&r.isFocused()&&(a(r.getInputDOMNode(),E.start,E.end),r.previousSelection=function(e){for(var n=1;n<arguments.length;n++){var s=null!=arguments[n]?arguments[n]:{},a=Object.keys(s);"function"==typeof Object.getOwnPropertySymbols&&(a=a.concat(Object.getOwnPropertySymbols(s).filter(function(e){return Object.getOwnPropertyDescriptor(s,e).enumerable}))),a.forEach(function(n){t(e,n,s[n])})}return e}({},E,{length:E.end-E.start}))}):r.setSelection(E.start,E.end)}),"onFocus",function(e){var t=r.props.beforeMaskedValueChange,n=r.maskOptions,s=n.mask,a=n.prefix;if(r.focused=!0,s)if(r.value)c(r.maskOptions,r.value)<r.maskOptions.mask.length&&r.setCursorToEnd();else{var o=f(r.maskOptions,a),i=f(r.maskOptions,o),u=c(r.maskOptions,i),l=v(r.maskOptions,u),p={start:l,end:l};if(d(t)){var h=t({value:i,selection:p},{value:r.value,selection:null},null,r.getModifyMaskedValueConfig());i=h.value,p=h.selection}var m=i!==e.target.value;m&&(e.target.value=i),r.value=i,m&&d(r.props.onChange)&&r.props.onChange(e),r.setSelection(p.start,p.end)}r.saveSelectionLoop(),d(r.props.onFocus)&&r.props.onFocus(e)}),"onBlur",function(e){var t=r.props.beforeMaskedValueChange,n=r.maskOptions.mask;if(r.previousSelection=null,r.focused=!1,n&&!r.props.alwaysShowMask&&p(r.maskOptions,r.value)){var s="";if(d(t))s=t({value:s,selection:null},{value:r.value,selection:r.previousSelection},null,r.getModifyMaskedValueConfig()).value;var a=s!==r.getInputValue();a&&r.setInputValue(s),a&&d(r.props.onChange)&&r.props.onChange(e)}d(r.props.onBlur)&&r.props.onBlur(e)}),"onMouseDown",function(e){if(!r.focused&&document.addEventListener){r.mouseDownX=e.clientX,r.mouseDownY=e.clientY,r.mouseDownTime=(new Date).getTime();document.addEventListener("mouseup",function t(e){if(document.removeEventListener("mouseup",t),r.focused){var n=Math.abs(e.clientX-r.mouseDownX),s=Math.abs(e.clientY-r.mouseDownY),a=Math.max(n,s),o=(new Date).getTime()-r.mouseDownTime;(a<=10&&o<=200||a<=5&&o<=300)&&r.setCursorToEnd()}})}d(r.props.onMouseDown)&&r.props.onMouseDown(e)}),"onPaste",function(e){d(r.props.onPaste)&&r.props.onPaste(e),e.defaultPrevented||(r.beforePasteState={value:r.getInputValue(),selection:r.getSelection()},r.setInputValue(""))}),"handleRef",function(e){r.input=e,d(r.props.inputRef)&&r.props.inputRef(e)});var h=n.mask,O=n.maskChar,w=n.formatChars,y=n.defaultValue,b=n.value,C=n.alwaysShowMask;return r.hasValue=null!=b,r.maskOptions=i(h,O,w),null==y&&(y=""),null==b&&(b=y),b=g(b),r.maskOptions.mask&&(C||b)&&(b=f(r.maskOptions,b)),r.value=b,r}var O,w,y=r.prototype;return y.componentDidMount=function(){var e,t,n;this.mounted=!0,this.isWindowsPhoneBrowser=(e=new RegExp("windows","i"),t=new RegExp("phone","i"),n=navigator.userAgent,e.test(n)&&t.test(n)),this.maskOptions.mask&&this.getInputValue()!==this.value&&this.setInputValue(this.value),this.props.maxLength&&this.maskOptions.mask&&"object"==typeof console&&console.error&&console.error("react-input-mask: You shouldn't pass maxLength property to the masked input. It breaks masking and unnecessary because length is limited by the mask length.")},y.componentDidUpdate=function(){var e=this.previousSelection,t=this.props.beforeMaskedValueChange,n=this.maskOptions;if(this.hasValue=null!=this.props.value,this.maskOptions=i(this.props.mask,this.props.maskChar,this.props.formatChars),!this.maskOptions.mask)return this.backspaceOrDeleteRemoval=null,void(this.previousSelection=null);var s=e?e.start:null,a=this.maskOptions.mask&&this.maskOptions.mask!==n.mask,o=this.props.alwaysShowMask||this.isFocused(),r=this.hasValue?g(this.props.value):this.value;if(n.mask||this.hasValue||(r=this.getInputValue()),(a||this.maskOptions.mask&&(r||o))&&(r=f(this.maskOptions,r),a)){var u=c(this.maskOptions,r);(null===s||u<s)&&(s=h(this.maskOptions,r)?u:v(this.maskOptions,u)),n.mask||this.saveSelectionLoop()}!this.maskOptions.mask||!p(this.maskOptions,r)||o||this.hasValue&&this.props.value||(r="");var l={start:s,end:s};if(d(t)){var m=t({value:r,selection:l},{value:this.value,selection:this.previousSelection},null,this.getModifyMaskedValueConfig());r=m.value,l=m.selection}this.value=r,this.maskOptions.mask&&this.getInputValue()!==this.value&&(this.setInputValue(this.value),this.forceUpdate()),(!e||e.start!==l.start||e.end!==l.end)&&this.setSelection(l.start,l.end)},y.componentWillUnmount=function(){this.mounted=!1},y.render=function(){var t=this,s=this.props,a=(s.mask,s.alwaysShowMask,s.maskChar,s.formatChars,s.inputRef,s.beforeMaskedValueChange,function(e,t){if(null==e)return{};var n,s,a={},o=Object.keys(e);for(s=0;s<o.length;s++)n=o[s],t.indexOf(n)>=0||(a[n]=e[n]);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);for(s=0;s<r.length;s++)n=r[s],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}(s,["mask","alwaysShowMask","maskChar","formatChars","inputRef","beforeMaskedValueChange"]));if(this.maskOptions.mask){if(!a.disabled&&!a.readOnly){["onChange","onPaste","onMouseDown"].forEach(function(e){a[e]=t[e]})}null!=a.value&&(a.value=this.value)}return e.createElement("input",n({ref:this.handleRef},a,{onFocus:this.onFocus,onBlur:this.onBlur}))},w=o,(O=r).prototype.__proto__=w&&w.prototype,O.__proto__=w,r}((e=e&&e.hasOwnProperty("default")?e["default"]:e).Component)}); |
1317
lib/index.js
@@ -1,616 +0,782 @@ | ||
"use strict"; | ||
'use strict'; | ||
exports.__esModule = true; | ||
exports.default = void 0; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var _react = _interopRequireDefault(require("react")); | ||
var React = _interopDefault(require('react')); | ||
var _parseMask = _interopRequireDefault(require("./utils/parseMask")); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
var _environment = require("./utils/environment"); | ||
return obj; | ||
} | ||
var _string = require("./utils/string"); | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
var _defer = _interopRequireDefault(require("./utils/defer")); | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
return target; | ||
}; | ||
function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return right[Symbol.hasInstance](left); } else { return left instanceof right; } } | ||
return _extends.apply(this, arguments); | ||
} | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
function _extends() { _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; }; return _extends.apply(this, arguments); } | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
return target; | ||
} | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype.__proto__ = superClass && superClass.prototype; | ||
subClass.__proto__ = superClass; | ||
} | ||
var InputElement = | ||
/*#__PURE__*/ | ||
function (_React$Component) { | ||
_inheritsLoose(InputElement, _React$Component); | ||
function _objectWithoutProperties(source, excluded) { | ||
if (source == null) return {}; | ||
var target = {}; | ||
var sourceKeys = Object.keys(source); | ||
var key, i; | ||
function InputElement(props) { | ||
var _this; | ||
for (i = 0; i < sourceKeys.length; i++) { | ||
key = sourceKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
target[key] = source[key]; | ||
} | ||
_this = _React$Component.call(this, props) || this; | ||
if (Object.getOwnPropertySymbols) { | ||
var sourceSymbolKeys = Object.getOwnPropertySymbols(source); | ||
_initialiseProps.call(_assertThisInitialized(_this)); | ||
var mask = props.mask, | ||
maskChar = props.maskChar, | ||
formatChars = props.formatChars, | ||
defaultValue = props.defaultValue, | ||
value = props.value, | ||
alwaysShowMask = props.alwaysShowMask; | ||
_this.hasValue = value != null; | ||
_this.maskOptions = (0, _parseMask.default)(mask, maskChar, formatChars); | ||
if (defaultValue == null) { | ||
defaultValue = ''; | ||
for (i = 0; i < sourceSymbolKeys.length; i++) { | ||
key = sourceSymbolKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; | ||
target[key] = source[key]; | ||
} | ||
} | ||
if (value == null) { | ||
value = defaultValue; | ||
} | ||
return target; | ||
} | ||
value = _this.getStringValue(value); | ||
function _assertThisInitialized(self) { | ||
if (self === void 0) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
if (_this.maskOptions.mask && (alwaysShowMask || value)) { | ||
value = (0, _string.formatValue)(_this.maskOptions, value); | ||
} | ||
return self; | ||
} | ||
_this.value = value; | ||
return _this; | ||
function setInputSelection(input, start, end) { | ||
if ('selectionStart' in input && 'selectionEnd' in input) { | ||
input.selectionStart = start; | ||
input.selectionEnd = end; | ||
} else { | ||
var range = input.createTextRange(); | ||
range.collapse(true); | ||
range.moveStart('character', start); | ||
range.moveEnd('character', end - start); | ||
range.select(); | ||
} | ||
} | ||
function getInputSelection(input) { | ||
var start = 0; | ||
var end = 0; | ||
var _proto = InputElement.prototype; | ||
if ('selectionStart' in input && 'selectionEnd' in input) { | ||
start = input.selectionStart; | ||
end = input.selectionEnd; | ||
} else { | ||
var range = document.selection.createRange(); | ||
_proto.componentDidMount = function componentDidMount() { | ||
this.isAndroidBrowser = (0, _environment.isAndroidBrowser)(); | ||
this.isWindowsPhoneBrowser = (0, _environment.isWindowsPhoneBrowser)(); | ||
this.isAndroidFirefox = (0, _environment.isAndroidFirefox)(); | ||
if (range.parentElement() === input) { | ||
start = -range.moveStart('character', -input.value.length); | ||
end = -range.moveEnd('character', -input.value.length); | ||
} | ||
} | ||
if (this.maskOptions.mask && this.getInputValue() !== this.value) { | ||
this.setInputValue(this.value); | ||
} | ||
return { | ||
start: start, | ||
end: end, | ||
length: end - start | ||
}; | ||
} | ||
_proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { | ||
var oldMaskOptions = this.maskOptions; | ||
this.hasValue = nextProps.value != null; | ||
this.maskOptions = (0, _parseMask.default)(nextProps.mask, nextProps.maskChar, nextProps.formatChars); | ||
var defaultFormatChars = { | ||
'9': '[0-9]', | ||
'a': '[A-Za-z]', | ||
'*': '[A-Za-z0-9]' | ||
}; | ||
var defaultMaskChar = '_'; | ||
if (!this.maskOptions.mask) { | ||
this.backspaceOrDeleteRemoval = null; | ||
this.lastCursorPos = null; | ||
return; | ||
} | ||
function parseMask (mask, maskChar, formatChars) { | ||
if (maskChar === undefined) { | ||
maskChar = defaultMaskChar; | ||
} | ||
var isMaskChanged = this.maskOptions.mask && this.maskOptions.mask !== oldMaskOptions.mask; | ||
var showEmpty = nextProps.alwaysShowMask || this.isFocused(); | ||
var newValue = this.hasValue ? this.getStringValue(nextProps.value) : this.value; | ||
if (formatChars == null) { | ||
formatChars = defaultFormatChars; | ||
} | ||
if (!oldMaskOptions.mask && !this.hasValue) { | ||
newValue = this.getInputValue(); | ||
} | ||
if (!mask || typeof mask !== 'string') { | ||
return { | ||
maskChar: maskChar, | ||
formatChars: formatChars, | ||
mask: null, | ||
prefix: null, | ||
lastEditablePos: null, | ||
permanents: [] | ||
}; | ||
} | ||
if (isMaskChanged || this.maskOptions.mask && (newValue || showEmpty)) { | ||
newValue = (0, _string.formatValue)(this.maskOptions, newValue); | ||
var str = ''; | ||
var prefix = ''; | ||
var permanents = []; | ||
var isPermanent = false; | ||
var lastEditablePos = null; | ||
mask.split('').forEach(function (character) { | ||
if (!isPermanent && character === '\\') { | ||
isPermanent = true; | ||
} else { | ||
if (isPermanent || !formatChars[character]) { | ||
permanents.push(str.length); | ||
if (isMaskChanged) { | ||
var pos = this.lastCursorPos; | ||
var filledLen = (0, _string.getFilledLength)(this.maskOptions, newValue); | ||
if (pos === null || filledLen < pos) { | ||
if ((0, _string.isFilled)(this.maskOptions, newValue)) { | ||
pos = filledLen; | ||
} else { | ||
pos = this.getRightEditablePos(filledLen); | ||
} | ||
this.setCursorPos(pos); | ||
if (str.length === permanents.length - 1) { | ||
prefix += character; | ||
} | ||
} else { | ||
lastEditablePos = str.length + 1; | ||
} | ||
} | ||
if (this.maskOptions.mask && (0, _string.isEmpty)(this.maskOptions, newValue) && !showEmpty && (!this.hasValue || !nextProps.value)) { | ||
newValue = ''; | ||
str += character; | ||
isPermanent = false; | ||
} | ||
this.value = newValue; | ||
}); | ||
return { | ||
maskChar: maskChar, | ||
formatChars: formatChars, | ||
prefix: prefix, | ||
mask: str, | ||
lastEditablePos: lastEditablePos, | ||
permanents: permanents | ||
}; | ||
} | ||
_proto.componentDidUpdate = function componentDidUpdate() { | ||
if (this.maskOptions.mask && this.getInputValue() !== this.value) { | ||
this.setInputValue(this.value); | ||
} | ||
}; | ||
function isWindowsPhoneBrowser() { | ||
var windows = new RegExp('windows', 'i'); | ||
var phone = new RegExp('phone', 'i'); | ||
var ua = navigator.userAgent; | ||
return windows.test(ua) && phone.test(ua); | ||
} | ||
_proto.render = function render() { | ||
var _this2 = this; | ||
function isPermanentChar(maskOptions, pos) { | ||
return maskOptions.permanents.indexOf(pos) !== -1; | ||
} | ||
function isAllowedChar(maskOptions, pos, character) { | ||
var mask = maskOptions.mask, | ||
formatChars = maskOptions.formatChars; | ||
var _props = this.props, | ||
mask = _props.mask, | ||
alwaysShowMask = _props.alwaysShowMask, | ||
maskChar = _props.maskChar, | ||
formatChars = _props.formatChars, | ||
inputRef = _props.inputRef, | ||
props = _objectWithoutProperties(_props, ["mask", "alwaysShowMask", "maskChar", "formatChars", "inputRef"]); | ||
if (!character) { | ||
return false; | ||
} | ||
if (this.maskOptions.mask) { | ||
if (!props.disabled && !props.readOnly) { | ||
var handlersKeys = ['onChange', 'onKeyDown', 'onPaste', 'onMouseDown']; | ||
handlersKeys.forEach(function (key) { | ||
props[key] = _this2[key]; | ||
}); | ||
} | ||
if (isPermanentChar(maskOptions, pos)) { | ||
return mask[pos] === character; | ||
} | ||
if (props.value != null) { | ||
props.value = this.value; | ||
} | ||
var ruleChar = mask[pos]; | ||
var charRule = formatChars[ruleChar]; | ||
return new RegExp(charRule).test(character); | ||
} | ||
function isEmpty(maskOptions, value) { | ||
return value.split('').every(function (character, i) { | ||
return isPermanentChar(maskOptions, i) || !isAllowedChar(maskOptions, i, character); | ||
}); | ||
} | ||
function getFilledLength(maskOptions, value) { | ||
var maskChar = maskOptions.maskChar, | ||
prefix = maskOptions.prefix; | ||
if (!maskChar) { | ||
while (value.length > prefix.length && isPermanentChar(maskOptions, value.length - 1)) { | ||
value = value.slice(0, value.length - 1); | ||
} | ||
return _react.default.createElement("input", _extends({ | ||
ref: this.handleRef | ||
}, props, { | ||
onFocus: this.onFocus, | ||
onBlur: this.onBlur | ||
})); | ||
}; | ||
return value.length; | ||
} | ||
return InputElement; | ||
}(_react.default.Component); | ||
var filledLength = prefix.length; | ||
var _initialiseProps = function _initialiseProps() { | ||
var _this3 = this; | ||
for (var i = value.length; i >= prefix.length; i--) { | ||
var character = value[i]; | ||
var isEnteredCharacter = !isPermanentChar(maskOptions, i) && isAllowedChar(maskOptions, i, character); | ||
Object.defineProperty(this, "lastCursorPos", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: null | ||
}); | ||
Object.defineProperty(this, "focused", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: false | ||
}); | ||
Object.defineProperty(this, "isDOMElement", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(element) { | ||
return (typeof HTMLElement === "undefined" ? "undefined" : _typeof(HTMLElement)) === 'object' ? _instanceof(element, HTMLElement) // DOM2 | ||
: element.nodeType === 1 && typeof element.nodeName === 'string'; | ||
if (isEnteredCharacter) { | ||
filledLength = i + 1; | ||
break; | ||
} | ||
}); | ||
Object.defineProperty(this, "getInputDOMNode", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value() { | ||
var input = _this3.input; | ||
} | ||
if (!input) { | ||
return null; | ||
} | ||
return filledLength; | ||
} | ||
function isFilled(maskOptions, value) { | ||
return getFilledLength(maskOptions, value) === maskOptions.mask.length; | ||
} | ||
function formatValue(maskOptions, value) { | ||
var maskChar = maskOptions.maskChar, | ||
mask = maskOptions.mask, | ||
prefix = maskOptions.prefix; | ||
if (_this3.isDOMElement(input)) { | ||
return input; | ||
} // React 0.13 | ||
if (!maskChar) { | ||
value = insertString(maskOptions, '', value, 0); | ||
if (value.length < prefix.length) { | ||
value = prefix; | ||
} | ||
return _react.default.findDOMNode(input); | ||
while (value.length < mask.length && isPermanentChar(maskOptions, value.length)) { | ||
value += mask[value.length]; | ||
} | ||
}); | ||
Object.defineProperty(this, "getInputValue", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value() { | ||
var input = _this3.getInputDOMNode(); | ||
if (!input) { | ||
return null; | ||
} | ||
return value; | ||
} | ||
return input.value; | ||
if (value) { | ||
var emptyValue = formatValue(maskOptions, ''); | ||
return insertString(maskOptions, emptyValue, value, 0); | ||
} | ||
for (var i = 0; i < mask.length; i++) { | ||
if (isPermanentChar(maskOptions, i)) { | ||
value += mask[i]; | ||
} else { | ||
value += maskChar; | ||
} | ||
}); | ||
Object.defineProperty(this, "setInputValue", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(_value) { | ||
var input = _this3.getInputDOMNode(); | ||
} | ||
if (!input) { | ||
return; | ||
return value; | ||
} | ||
function clearRange(maskOptions, value, start, len) { | ||
var end = start + len; | ||
var maskChar = maskOptions.maskChar, | ||
mask = maskOptions.mask, | ||
prefix = maskOptions.prefix; | ||
var arrayValue = value.split(''); | ||
if (!maskChar) { | ||
// remove any permanent chars after clear range, they will be added back by formatValue | ||
for (var i = end; i < arrayValue.length; i++) { | ||
if (isPermanentChar(maskOptions, i)) { | ||
arrayValue[i] = ''; | ||
} | ||
} | ||
_this3.value = _value; | ||
input.value = _value; | ||
start = Math.max(prefix.length, start); | ||
arrayValue.splice(start, end - start); | ||
value = arrayValue.join(''); | ||
return formatValue(maskOptions, value); | ||
} | ||
return arrayValue.map(function (character, i) { | ||
if (i < start || i >= end) { | ||
return character; | ||
} | ||
}); | ||
Object.defineProperty(this, "getLeftEditablePos", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(pos) { | ||
for (var i = pos; i >= 0; --i) { | ||
if (!(0, _string.isPermanentChar)(_this3.maskOptions, i)) { | ||
return i; | ||
} | ||
} | ||
return null; | ||
if (isPermanentChar(maskOptions, i)) { | ||
return mask[i]; | ||
} | ||
}); | ||
Object.defineProperty(this, "getRightEditablePos", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(pos) { | ||
var mask = _this3.maskOptions.mask; | ||
for (var i = pos; i < mask.length; ++i) { | ||
if (!(0, _string.isPermanentChar)(_this3.maskOptions, i)) { | ||
return i; | ||
} | ||
} | ||
return maskChar; | ||
}).join(''); | ||
} | ||
function insertString(maskOptions, value, insertStr, insertPos) { | ||
var mask = maskOptions.mask, | ||
maskChar = maskOptions.maskChar, | ||
prefix = maskOptions.prefix; | ||
var arrayInsertStr = insertStr.split(''); | ||
var isInputFilled = isFilled(maskOptions, value); | ||
return null; | ||
} | ||
}); | ||
Object.defineProperty(this, "setCursorToEnd", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value() { | ||
var filledLen = (0, _string.getFilledLength)(_this3.maskOptions, _this3.value); | ||
var isUsablePosition = function isUsablePosition(pos, character) { | ||
return !isPermanentChar(maskOptions, pos) || character === mask[pos]; | ||
}; | ||
var pos = _this3.getRightEditablePos(filledLen); | ||
var isUsableCharacter = function isUsableCharacter(character, pos) { | ||
return !maskChar || !isPermanentChar(maskOptions, pos) || character !== maskChar; | ||
}; | ||
if (pos !== null) { | ||
_this3.setCursorPos(pos); | ||
if (!maskChar && insertPos > value.length) { | ||
value += mask.slice(value.length, insertPos); | ||
} | ||
arrayInsertStr.every(function (insertCharacter) { | ||
while (!isUsablePosition(insertPos, insertCharacter)) { | ||
if (insertPos >= value.length) { | ||
value += mask[insertPos]; | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "setSelection", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(start, len) { | ||
if (len === void 0) { | ||
len = 0; | ||
if (!isUsableCharacter(insertCharacter, insertPos)) { | ||
return true; | ||
} | ||
var input = _this3.getInputDOMNode(); | ||
insertPos++; // stop iteration if maximum value length reached | ||
if (!input) { | ||
return; | ||
if (insertPos >= mask.length) { | ||
return false; | ||
} | ||
} | ||
var end = start + len; | ||
var isAllowed = isAllowedChar(maskOptions, insertPos, insertCharacter) || insertCharacter === maskChar; | ||
if ('selectionStart' in input && 'selectionEnd' in input) { | ||
input.selectionStart = start; | ||
input.selectionEnd = end; | ||
if (!isAllowed) { | ||
return true; | ||
} | ||
if (insertPos < value.length) { | ||
if (maskChar || isInputFilled || insertPos < prefix.length) { | ||
value = value.slice(0, insertPos) + insertCharacter + value.slice(insertPos + 1); | ||
} else { | ||
var range = input.createTextRange(); | ||
range.collapse(true); | ||
range.moveStart('character', start); | ||
range.moveEnd('character', end - start); | ||
range.select(); | ||
value = value.slice(0, insertPos) + insertCharacter + value.slice(insertPos); | ||
value = formatValue(maskOptions, value); | ||
} | ||
} else if (!maskChar) { | ||
value += insertCharacter; | ||
} | ||
insertPos++; // stop iteration if maximum value length reached | ||
return insertPos < mask.length; | ||
}); | ||
Object.defineProperty(this, "getSelection", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value() { | ||
var input = _this3.getInputDOMNode(); | ||
return value; | ||
} | ||
function getInsertStringLength(maskOptions, value, insertStr, insertPos) { | ||
var mask = maskOptions.mask, | ||
maskChar = maskOptions.maskChar; | ||
var arrayInsertStr = insertStr.split(''); | ||
var initialInsertPos = insertPos; | ||
var start = 0; | ||
var end = 0; | ||
var isUsablePosition = function isUsablePosition(pos, character) { | ||
return !isPermanentChar(maskOptions, pos) || character === mask[pos]; | ||
}; | ||
if ('selectionStart' in input && 'selectionEnd' in input) { | ||
start = input.selectionStart; | ||
end = input.selectionEnd; | ||
} else { | ||
var range = document.selection.createRange(); | ||
arrayInsertStr.every(function (insertCharacter) { | ||
while (!isUsablePosition(insertPos, insertCharacter)) { | ||
insertPos++; // stop iteration if maximum value length reached | ||
if (range.parentElement() === input) { | ||
start = -range.moveStart('character', -input.value.length); | ||
end = -range.moveEnd('character', -input.value.length); | ||
} | ||
if (insertPos >= mask.length) { | ||
return false; | ||
} | ||
} | ||
return { | ||
start: start, | ||
end: end, | ||
length: end - start | ||
}; | ||
} | ||
var isAllowed = isAllowedChar(maskOptions, insertPos, insertCharacter) || insertCharacter === maskChar; | ||
if (isAllowed) { | ||
insertPos++; | ||
} // stop iteration if maximum value length reached | ||
return insertPos < mask.length; | ||
}); | ||
Object.defineProperty(this, "getCursorPos", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value() { | ||
return _this3.getSelection().start; | ||
return insertPos - initialInsertPos; | ||
} | ||
function getLeftEditablePos(maskOptions, pos) { | ||
for (var i = pos; i >= 0; --i) { | ||
if (!isPermanentChar(maskOptions, i)) { | ||
return i; | ||
} | ||
}); | ||
Object.defineProperty(this, "setCursorPos", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(pos) { | ||
_this3.setSelection(pos, 0); | ||
} | ||
(0, _defer.default)(function () { | ||
_this3.setSelection(pos, 0); | ||
}); | ||
_this3.lastCursorPos = pos; | ||
return null; | ||
} | ||
function getRightEditablePos(maskOptions, pos) { | ||
var mask = maskOptions.mask; | ||
for (var i = pos; i < mask.length; ++i) { | ||
if (!isPermanentChar(maskOptions, i)) { | ||
return i; | ||
} | ||
}); | ||
Object.defineProperty(this, "isFocused", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value() { | ||
return _this3.focused; | ||
} | ||
}); | ||
Object.defineProperty(this, "getStringValue", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(_value2) { | ||
return !_value2 && _value2 !== 0 ? '' : _value2 + ''; | ||
} | ||
}); | ||
Object.defineProperty(this, "onKeyDown", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(event) { | ||
_this3.backspaceOrDeleteRemoval = null; | ||
} | ||
if (typeof _this3.props.onKeyDown === 'function') { | ||
_this3.props.onKeyDown(event); | ||
} | ||
return null; | ||
} | ||
function getStringValue(value) { | ||
return !value && value !== 0 ? '' : value + ''; | ||
} | ||
var key = event.key, | ||
ctrlKey = event.ctrlKey, | ||
metaKey = event.metaKey, | ||
defaultPrevented = event.defaultPrevented; | ||
function isDOMElement(element) { | ||
return typeof HTMLElement === 'object' ? element instanceof HTMLElement // DOM2 | ||
: element.nodeType === 1 && typeof element.nodeName === 'string'; | ||
} | ||
function isFunction(value) { | ||
return typeof value === 'function'; | ||
} | ||
if (ctrlKey || metaKey || defaultPrevented) { | ||
function defer (fn, timeoutDelay) { | ||
if (timeoutDelay === void 0) { | ||
timeoutDelay = 0; | ||
} | ||
var defer = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function () { | ||
return setTimeout(fn, timeoutDelay); | ||
}; | ||
return defer(fn); | ||
} | ||
var InputElement = | ||
/*#__PURE__*/ | ||
function (_React$Component) { | ||
function InputElement(props) { | ||
var _this; | ||
_this = _React$Component.call(this, props) || this; | ||
_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "focused", false), "mounted", false), "previousSelection", null), "saveSelectionLoop", function () { | ||
if (!_this.mounted || !_this.maskOptions.mask || !_this.focused || _this.saveSelectionLoopRunning) { | ||
return; | ||
} | ||
if (key === 'Backspace' || key === 'Delete') { | ||
var selection = _this3.getSelection(); | ||
_this.saveSelectionLoopRunning = true; | ||
_this.previousSelection = _this.getSelection(); | ||
defer(function () { | ||
_this.saveSelectionLoopRunning = false; | ||
var canRemove = key === 'Backspace' && selection.end > 0 || key === 'Delete' && _this3.value.length > selection.start; | ||
_this.saveSelectionLoop(); | ||
}, 1000 / 60); | ||
}), "getInputDOMNode", function () { | ||
var input = _this.input; | ||
if (!canRemove) { | ||
return; | ||
} | ||
if (!input) { | ||
return null; | ||
} | ||
_this3.backspaceOrDeleteRemoval = { | ||
key: key, | ||
selection: _this3.getSelection() | ||
}; | ||
if (isDOMElement(input)) { | ||
return input; | ||
} // React 0.13 | ||
return React.findDOMNode(input); | ||
}), "getInputValue", function () { | ||
var input = _this.getInputDOMNode(); | ||
if (!input) { | ||
return null; | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "onChange", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(event) { | ||
var beforePasteState = _this3.beforePasteState; | ||
var _this3$maskOptions = _this3.maskOptions, | ||
mask = _this3$maskOptions.mask, | ||
maskChar = _this3$maskOptions.maskChar, | ||
lastEditablePos = _this3$maskOptions.lastEditablePos, | ||
prefix = _this3$maskOptions.prefix; | ||
var value = _this3.getInputValue(); | ||
return input.value; | ||
}), "setInputValue", function (value) { | ||
var input = _this.getInputDOMNode(); | ||
if (beforePasteState) { | ||
_this3.beforePasteState = null; | ||
if (!input) { | ||
return; | ||
} | ||
_this3.pasteText(beforePasteState.value, value, beforePasteState.selection, event); | ||
_this.value = value; | ||
input.value = value; | ||
}), "setCursorToEnd", function () { | ||
var filledLen = getFilledLength(_this.maskOptions, _this.value); | ||
var pos = getRightEditablePos(_this.maskOptions, filledLen); | ||
if (pos !== null) { | ||
_this.setCursorPos(pos); | ||
} | ||
}), "setSelection", function (start, end) { | ||
var input = _this.getInputDOMNode(); | ||
if (!input) { | ||
return; | ||
} | ||
var oldValue = _this3.value; | ||
setInputSelection(input, start, end); | ||
defer(function () { | ||
setInputSelection(input, start, end); | ||
}); | ||
_this.previousSelection = { | ||
start: start, | ||
end: end, | ||
length: Math.abs(end - start) | ||
}; | ||
}), "getSelection", function () { | ||
var input = _this.getInputDOMNode(); | ||
var input = _this3.getInputDOMNode(); // autofill replaces whole value, ignore old one | ||
// https://github.com/sanniassin/react-input-mask/issues/113 | ||
// | ||
// input.matches throws exception if selector isn't supported | ||
return getInputSelection(input); | ||
}), "getCursorPos", function () { | ||
return _this.getSelection().start; | ||
}), "setCursorPos", function (pos) { | ||
_this.setSelection(pos, pos); | ||
}), "isFocused", function () { | ||
return _this.focused; | ||
}), "getModifyMaskedValueConfig", function () { | ||
var _this$maskOptions = _this.maskOptions, | ||
mask = _this$maskOptions.mask, | ||
maskChar = _this$maskOptions.maskChar, | ||
permanents = _this$maskOptions.permanents, | ||
formatChars = _this$maskOptions.formatChars; | ||
var alwaysShowMask = _this.props.alwaysShowMask; | ||
return { | ||
mask: mask, | ||
maskChar: maskChar, | ||
permanents: permanents, | ||
alwaysShowMask: !!alwaysShowMask, | ||
formatChars: formatChars | ||
}; | ||
}), "isInputAutofilled", function () { | ||
var input = _this.getInputDOMNode(); | ||
var isAutofilled = false; // input.matches throws an exception if selector isn't supported | ||
try { | ||
if (typeof input.matches === 'function' && input.matches(':-webkit-autofill')) { | ||
oldValue = ''; | ||
if (isFunction(input.matches) && input.matches(':-webkit-autofill')) { | ||
isAutofilled = true; | ||
} | ||
} catch (e) {} | ||
var selection = _this3.getSelection(); | ||
return isAutofilled; | ||
}), "onChange", function (event) { | ||
var _assertThisInitialize = _assertThisInitialized(_assertThisInitialized(_this)), | ||
beforePasteState = _assertThisInitialize.beforePasteState, | ||
previousSelection = _assertThisInitialize.previousSelection; | ||
var cursorPos = selection.end; | ||
var maskLen = mask.length; | ||
var valueLen = value.length; | ||
var oldValueLen = oldValue.length; | ||
var clearedValue; | ||
var enteredString; | ||
var beforeMaskedValueChange = _this.props.beforeMaskedValueChange; | ||
var _this$maskOptions2 = _this.maskOptions, | ||
mask = _this$maskOptions2.mask, | ||
prefix = _this$maskOptions2.prefix, | ||
lastEditablePos = _this$maskOptions2.lastEditablePos; | ||
if (_this3.backspaceOrDeleteRemoval) { | ||
var deleteFromRight = _this3.backspaceOrDeleteRemoval.key === 'Delete'; | ||
value = _this3.value; | ||
selection = _this3.backspaceOrDeleteRemoval.selection; | ||
cursorPos = selection.start; | ||
_this3.backspaceOrDeleteRemoval = null; | ||
var value = _this.getInputValue(); | ||
if (selection.length) { | ||
value = (0, _string.clearRange)(_this3.maskOptions, value, selection.start, selection.length); | ||
} else if (selection.start < prefix.length || !deleteFromRight && selection.start === prefix.length) { | ||
cursorPos = prefix.length; | ||
} else { | ||
var editablePos = deleteFromRight ? _this3.getRightEditablePos(cursorPos) : _this3.getLeftEditablePos(cursorPos - 1); | ||
var newValue = value; | ||
var previousValue = _this.value; // autofill replaces entire value, ignore old one | ||
// https://github.com/sanniassin/react-input-mask/issues/113 | ||
if (editablePos !== null) { | ||
if (!maskChar) { | ||
value = value.substr(0, (0, _string.getFilledLength)(_this3.maskOptions, value)); | ||
} | ||
if (_this.isInputAutofilled()) { | ||
previousValue = formatValue(_this.maskOptions, ''); | ||
previousSelection = { | ||
start: 0, | ||
end: 0, | ||
length: 0 | ||
}; | ||
} | ||
value = (0, _string.clearRange)(_this3.maskOptions, value, editablePos, 1); | ||
cursorPos = editablePos; | ||
} | ||
} | ||
} else if (valueLen > oldValueLen) { | ||
var enteredStringLen = valueLen - oldValueLen; | ||
var startPos = selection.end - enteredStringLen; | ||
enteredString = value.substr(startPos, enteredStringLen); | ||
var enteredString = ''; | ||
if (startPos < lastEditablePos && (enteredStringLen !== 1 || enteredString !== mask[startPos])) { | ||
cursorPos = _this3.getRightEditablePos(startPos); | ||
} else { | ||
cursorPos = startPos; | ||
} | ||
var selection = _this.getSelection(); | ||
value = value.substr(0, startPos) + value.substr(startPos + enteredStringLen); | ||
clearedValue = (0, _string.clearRange)(_this3.maskOptions, value, startPos, maskLen - startPos); | ||
clearedValue = (0, _string.insertString)(_this3.maskOptions, clearedValue, enteredString, cursorPos); | ||
value = (0, _string.insertString)(_this3.maskOptions, oldValue, enteredString, cursorPos); | ||
var cursorPos = selection.end; | ||
selection.start = selection.end; | ||
selection.length = 0; // set value and selection as if we haven't | ||
// cleared input in onPaste handler | ||
if (enteredStringLen !== 1 || cursorPos >= prefix.length && cursorPos < lastEditablePos) { | ||
cursorPos = Math.max((0, _string.getFilledLength)(_this3.maskOptions, clearedValue), cursorPos); | ||
if (beforePasteState) { | ||
previousSelection = beforePasteState.selection; | ||
previousValue = beforePasteState.value; | ||
cursorPos = previousSelection.start + newValue.length; | ||
selection = { | ||
start: cursorPos, | ||
end: cursorPos, | ||
length: 0 | ||
}; | ||
newValue = previousValue.slice(0, previousSelection.start) + newValue + previousValue.slice(previousSelection.end); | ||
_this.beforePasteState = null; | ||
} | ||
if (cursorPos < lastEditablePos) { | ||
cursorPos = _this3.getRightEditablePos(cursorPos); | ||
} | ||
} else if (cursorPos < lastEditablePos) { | ||
cursorPos++; | ||
var formattedEnteredStringLen = 0; | ||
var removedLen = previousSelection.length; | ||
cursorPos = Math.min(previousSelection.start, selection.start); | ||
if (selection.end > previousSelection.start) { | ||
enteredString = newValue.slice(previousSelection.start, selection.end); | ||
formattedEnteredStringLen = getInsertStringLength(_this.maskOptions, previousValue, enteredString, cursorPos); | ||
if (!formattedEnteredStringLen) { | ||
removedLen = 0; | ||
} | ||
} else if (valueLen < oldValueLen) { | ||
var removedLen = maskLen - valueLen; | ||
enteredString = value.substr(0, selection.end); | ||
var clearOnly = enteredString === oldValue.substr(0, selection.end); | ||
clearedValue = (0, _string.clearRange)(_this3.maskOptions, oldValue, selection.end, removedLen); | ||
} else if (newValue.length < previousValue.length) { | ||
removedLen = previousValue.length - newValue.length; | ||
} | ||
if (maskChar) { | ||
value = (0, _string.insertString)(_this3.maskOptions, clearedValue, enteredString, 0); | ||
newValue = previousValue; | ||
if (removedLen) { | ||
if (removedLen === 1 && !previousSelection.length) { | ||
var deleteFromRight = previousSelection.start === selection.start; | ||
cursorPos = deleteFromRight ? getRightEditablePos(_this.maskOptions, selection.start) : getLeftEditablePos(_this.maskOptions, selection.start); | ||
} | ||
clearedValue = (0, _string.clearRange)(_this3.maskOptions, clearedValue, selection.end, maskLen - selection.end); | ||
clearedValue = (0, _string.insertString)(_this3.maskOptions, clearedValue, enteredString, 0); | ||
newValue = clearRange(_this.maskOptions, newValue, cursorPos, removedLen); | ||
} | ||
if (!clearOnly) { | ||
cursorPos = Math.max((0, _string.getFilledLength)(_this3.maskOptions, clearedValue), cursorPos); | ||
newValue = insertString(_this.maskOptions, newValue, enteredString, cursorPos); | ||
cursorPos = cursorPos + formattedEnteredStringLen; | ||
if (cursorPos < lastEditablePos) { | ||
cursorPos = _this3.getRightEditablePos(cursorPos); | ||
} | ||
} else if (cursorPos < prefix.length) { | ||
cursorPos = prefix.length; | ||
} | ||
if (cursorPos >= mask.length) { | ||
cursorPos = mask.length; | ||
} else if (cursorPos < prefix.length && !formattedEnteredStringLen) { | ||
cursorPos = prefix.length; | ||
} else if (cursorPos >= prefix.length && cursorPos < lastEditablePos && formattedEnteredStringLen) { | ||
cursorPos = getRightEditablePos(_this.maskOptions, cursorPos); | ||
} | ||
value = (0, _string.formatValue)(_this3.maskOptions, value); | ||
newValue = formatValue(_this.maskOptions, newValue); | ||
_this3.setInputValue(value); | ||
if (!enteredString) { | ||
enteredString = null; | ||
} | ||
if (typeof _this3.props.onChange === 'function') { | ||
_this3.props.onChange(event); | ||
var newSelection = { | ||
start: cursorPos, | ||
end: cursorPos | ||
}; | ||
if (isFunction(beforeMaskedValueChange)) { | ||
var modifiedValue = beforeMaskedValueChange({ | ||
value: newValue, | ||
selection: newSelection | ||
}, { | ||
value: previousValue, | ||
selection: previousSelection | ||
}, enteredString, _this.getModifyMaskedValueConfig()); | ||
newValue = modifiedValue.value; | ||
newSelection = modifiedValue.selection; | ||
} | ||
if (_this3.isWindowsPhoneBrowser) { | ||
(0, _defer.default)(function () { | ||
_this3.setSelection(cursorPos, 0); | ||
_this.setInputValue(newValue); | ||
if (isFunction(_this.props.onChange)) { | ||
_this.props.onChange(event); | ||
} | ||
if (_this.isWindowsPhoneBrowser) { | ||
defer(function () { | ||
if (!_this.mounted || !_this.isFocused()) { | ||
return; | ||
} | ||
var input = _this.getInputDOMNode(); | ||
setInputSelection(input, newSelection.start, newSelection.end); | ||
_this.previousSelection = _objectSpread({}, newSelection, { | ||
length: newSelection.end - newSelection.start | ||
}); | ||
}); | ||
} else { | ||
_this3.setCursorPos(cursorPos); | ||
_this.setSelection(newSelection.start, newSelection.end); | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "onFocus", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(event) { | ||
_this3.focused = true; | ||
}), "onFocus", function (event) { | ||
var beforeMaskedValueChange = _this.props.beforeMaskedValueChange; | ||
var _this$maskOptions3 = _this.maskOptions, | ||
mask = _this$maskOptions3.mask, | ||
prefix = _this$maskOptions3.prefix; | ||
_this.focused = true; | ||
if (_this3.maskOptions.mask) { | ||
if (!_this3.value) { | ||
var prefix = _this3.maskOptions.prefix; | ||
var value = (0, _string.formatValue)(_this3.maskOptions, prefix); | ||
var inputValue = (0, _string.formatValue)(_this3.maskOptions, value); // do not use this.getInputValue and this.setInputValue as this.input | ||
// can be undefined at this moment if autoFocus attribute is set | ||
if (mask) { | ||
if (!_this.value) { | ||
var emptyValue = formatValue(_this.maskOptions, prefix); | ||
var newValue = formatValue(_this.maskOptions, emptyValue); | ||
var filledLen = getFilledLength(_this.maskOptions, newValue); | ||
var cursorPos = getRightEditablePos(_this.maskOptions, filledLen); | ||
var newSelection = { | ||
start: cursorPos, | ||
end: cursorPos | ||
}; | ||
var isInputValueChanged = inputValue !== event.target.value; | ||
if (isFunction(beforeMaskedValueChange)) { | ||
var modifiedValue = beforeMaskedValueChange({ | ||
value: newValue, | ||
selection: newSelection | ||
}, { | ||
value: _this.value, | ||
selection: null | ||
}, null, _this.getModifyMaskedValueConfig()); | ||
newValue = modifiedValue.value; | ||
newSelection = modifiedValue.selection; | ||
} // do not use this.getInputValue and this.setInputValue as this.input | ||
// will be undefined if it's an initial mount of input with autoFocus attribute | ||
var isInputValueChanged = newValue !== event.target.value; | ||
if (isInputValueChanged) { | ||
event.target.value = inputValue; | ||
event.target.value = newValue; | ||
} | ||
_this3.value = inputValue; | ||
_this.value = newValue; | ||
if (isInputValueChanged && typeof _this3.props.onChange === 'function') { | ||
_this3.props.onChange(event); | ||
if (isInputValueChanged && isFunction(_this.props.onChange)) { | ||
_this.props.onChange(event); | ||
} | ||
_this3.setCursorToEnd(); | ||
} else if ((0, _string.getFilledLength)(_this3.maskOptions, _this3.value) < _this3.maskOptions.mask.length) { | ||
_this3.setCursorToEnd(); | ||
_this.setSelection(newSelection.start, newSelection.end); | ||
} else if (getFilledLength(_this.maskOptions, _this.value) < _this.maskOptions.mask.length) { | ||
_this.setCursorToEnd(); | ||
} | ||
} | ||
if (typeof _this3.props.onFocus === 'function') { | ||
_this3.props.onFocus(event); | ||
_this.saveSelectionLoop(); | ||
if (isFunction(_this.props.onFocus)) { | ||
_this.props.onFocus(event); | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "onBlur", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(event) { | ||
_this3.focused = false; | ||
}), "onBlur", function (event) { | ||
var beforeMaskedValueChange = _this.props.beforeMaskedValueChange; | ||
var mask = _this.maskOptions.mask; | ||
_this.previousSelection = null; | ||
_this.focused = false; | ||
if (_this3.maskOptions.mask && !_this3.props.alwaysShowMask && (0, _string.isEmpty)(_this3.maskOptions, _this3.value)) { | ||
var inputValue = ''; | ||
if (mask && !_this.props.alwaysShowMask && isEmpty(_this.maskOptions, _this.value)) { | ||
var newValue = ''; | ||
var isInputValueChanged = inputValue !== _this3.getInputValue(); | ||
if (isFunction(beforeMaskedValueChange)) { | ||
var modifiedValue = beforeMaskedValueChange({ | ||
value: newValue, | ||
selection: null | ||
}, { | ||
value: _this.value, | ||
selection: _this.previousSelection | ||
}, null, _this.getModifyMaskedValueConfig()); | ||
newValue = modifiedValue.value; | ||
} | ||
var isInputValueChanged = newValue !== _this.getInputValue(); | ||
if (isInputValueChanged) { | ||
_this3.setInputValue(inputValue); | ||
_this.setInputValue(newValue); | ||
} | ||
if (isInputValueChanged && typeof _this3.props.onChange === 'function') { | ||
_this3.props.onChange(event); | ||
if (isInputValueChanged && isFunction(_this.props.onChange)) { | ||
_this.props.onChange(event); | ||
} | ||
} | ||
if (typeof _this3.props.onBlur === 'function') { | ||
_this3.props.onBlur(event); | ||
if (isFunction(_this.props.onBlur)) { | ||
_this.props.onBlur(event); | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "onMouseDown", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(event) { | ||
}), "onMouseDown", function (event) { | ||
// tiny unintentional mouse movements can break cursor | ||
@@ -620,6 +786,6 @@ // position on focus, so we have to restore it in that case | ||
// https://github.com/sanniassin/react-input-mask/issues/108 | ||
if (!_this3.focused && document.addEventListener) { | ||
_this3.mouseDownX = event.clientX; | ||
_this3.mouseDownY = event.clientY; | ||
_this3.mouseDownTime = new Date().getTime(); | ||
if (!_this.focused && document.addEventListener) { | ||
_this.mouseDownX = event.clientX; | ||
_this.mouseDownY = event.clientY; | ||
_this.mouseDownTime = new Date().getTime(); | ||
@@ -629,14 +795,14 @@ var mouseUpHandler = function mouseUpHandler(mouseUpEvent) { | ||
if (!_this3.focused) { | ||
if (!_this.focused) { | ||
return; | ||
} | ||
var deltaX = Math.abs(mouseUpEvent.clientX - _this3.mouseDownX); | ||
var deltaY = Math.abs(mouseUpEvent.clientY - _this3.mouseDownY); | ||
var deltaX = Math.abs(mouseUpEvent.clientX - _this.mouseDownX); | ||
var deltaY = Math.abs(mouseUpEvent.clientY - _this.mouseDownY); | ||
var axisDelta = Math.max(deltaX, deltaY); | ||
var timeDelta = new Date().getTime() - _this3.mouseDownTime; | ||
var timeDelta = new Date().getTime() - _this.mouseDownTime; | ||
if (axisDelta <= 10 && timeDelta <= 200 || axisDelta <= 5 && timeDelta <= 300) { | ||
_this3.setCursorToEnd(); | ||
_this.setCursorToEnd(); | ||
} | ||
@@ -648,70 +814,189 @@ }; | ||
if (typeof _this3.props.onMouseDown === 'function') { | ||
_this3.props.onMouseDown(event); | ||
if (isFunction(_this.props.onMouseDown)) { | ||
_this.props.onMouseDown(event); | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "onPaste", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(event) { | ||
if (typeof _this3.props.onPaste === 'function') { | ||
_this3.props.onPaste(event); | ||
} // we need raw pasted text, but event.clipboardData | ||
// may not work in Android browser, so we clean input | ||
// to get raw text in onChange handler | ||
}), "onPaste", function (event) { | ||
if (isFunction(_this.props.onPaste)) { | ||
_this.props.onPaste(event); | ||
} // event.clipboardData might not work in Android browser | ||
// cleaning input to get raw text inside onChange handler | ||
if (!event.defaultPrevented) { | ||
_this3.beforePasteState = { | ||
value: _this3.getInputValue(), | ||
selection: _this3.getSelection() | ||
_this.beforePasteState = { | ||
value: _this.getInputValue(), | ||
selection: _this.getSelection() | ||
}; | ||
_this3.setInputValue(''); | ||
_this.setInputValue(''); | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, "pasteText", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(_value3, text, selection, event) { | ||
var cursorPos = selection.start; | ||
}), "handleRef", function (ref) { | ||
_this.input = ref; | ||
if (selection.length) { | ||
_value3 = (0, _string.clearRange)(_this3.maskOptions, _value3, cursorPos, selection.length); | ||
if (isFunction(_this.props.inputRef)) { | ||
_this.props.inputRef(ref); | ||
} | ||
}); | ||
var textLen = (0, _string.getInsertStringLength)(_this3.maskOptions, _value3, text, cursorPos); | ||
_value3 = (0, _string.insertString)(_this3.maskOptions, _value3, text, cursorPos); | ||
cursorPos += textLen; | ||
cursorPos = _this3.getRightEditablePos(cursorPos) || cursorPos; | ||
var _mask = props.mask, | ||
_maskChar = props.maskChar, | ||
_formatChars = props.formatChars, | ||
defaultValue = props.defaultValue, | ||
_value = props.value, | ||
_alwaysShowMask = props.alwaysShowMask; | ||
_this.hasValue = _value != null; | ||
_this.maskOptions = parseMask(_mask, _maskChar, _formatChars); | ||
_this3.setInputValue(_value3); | ||
if (defaultValue == null) { | ||
defaultValue = ''; | ||
} | ||
if (event && typeof _this3.props.onChange === 'function') { | ||
_this3.props.onChange(event); | ||
if (_value == null) { | ||
_value = defaultValue; | ||
} | ||
_value = getStringValue(_value); | ||
if (_this.maskOptions.mask && (_alwaysShowMask || _value)) { | ||
_value = formatValue(_this.maskOptions, _value); | ||
} | ||
_this.value = _value; | ||
return _this; | ||
} | ||
var _proto = InputElement.prototype; | ||
_proto.componentDidMount = function componentDidMount() { | ||
this.mounted = true; | ||
this.isWindowsPhoneBrowser = isWindowsPhoneBrowser(); | ||
if (this.maskOptions.mask && this.getInputValue() !== this.value) { | ||
this.setInputValue(this.value); | ||
} | ||
if (this.props.maxLength && this.maskOptions.mask && typeof console === 'object' && console.error) { | ||
console.error('react-input-mask: You shouldn\'t pass maxLength property to the masked input. It breaks masking and unnecessary because length is limited by the mask length.'); | ||
} | ||
}; | ||
_proto.componentDidUpdate = function componentDidUpdate() { | ||
var previousSelection = this.previousSelection; | ||
var beforeMaskedValueChange = this.props.beforeMaskedValueChange; | ||
var oldMaskOptions = this.maskOptions; | ||
this.hasValue = this.props.value != null; | ||
this.maskOptions = parseMask(this.props.mask, this.props.maskChar, this.props.formatChars); | ||
if (!this.maskOptions.mask) { | ||
this.backspaceOrDeleteRemoval = null; | ||
this.previousSelection = null; | ||
return; | ||
} | ||
var cursorPos = previousSelection ? previousSelection.start : null; | ||
var isMaskChanged = this.maskOptions.mask && this.maskOptions.mask !== oldMaskOptions.mask; | ||
var showEmpty = this.props.alwaysShowMask || this.isFocused(); | ||
var newValue = this.hasValue ? getStringValue(this.props.value) : this.value; | ||
if (!oldMaskOptions.mask && !this.hasValue) { | ||
newValue = this.getInputValue(); | ||
} | ||
if (isMaskChanged || this.maskOptions.mask && (newValue || showEmpty)) { | ||
newValue = formatValue(this.maskOptions, newValue); | ||
if (isMaskChanged) { | ||
var filledLen = getFilledLength(this.maskOptions, newValue); | ||
if (cursorPos === null || filledLen < cursorPos) { | ||
if (isFilled(this.maskOptions, newValue)) { | ||
cursorPos = filledLen; | ||
} else { | ||
cursorPos = getRightEditablePos(this.maskOptions, filledLen); | ||
} | ||
} | ||
if (!oldMaskOptions.mask) { | ||
this.saveSelectionLoop(); | ||
} | ||
} | ||
} | ||
_this3.setCursorPos(cursorPos); | ||
if (this.maskOptions.mask && isEmpty(this.maskOptions, newValue) && !showEmpty && (!this.hasValue || !this.props.value)) { | ||
newValue = ''; | ||
} | ||
}); | ||
Object.defineProperty(this, "handleRef", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function value(ref) { | ||
_this3.input = ref; | ||
if (typeof _this3.props.inputRef === 'function') { | ||
_this3.props.inputRef(ref); | ||
var newSelection = { | ||
start: cursorPos, | ||
end: cursorPos | ||
}; | ||
if (isFunction(beforeMaskedValueChange)) { | ||
var modifiedValue = beforeMaskedValueChange({ | ||
value: newValue, | ||
selection: newSelection | ||
}, { | ||
value: this.value, | ||
selection: this.previousSelection | ||
}, null, this.getModifyMaskedValueConfig()); | ||
newValue = modifiedValue.value; | ||
newSelection = modifiedValue.selection; | ||
} | ||
this.value = newValue; | ||
if (this.maskOptions.mask && this.getInputValue() !== this.value) { | ||
this.setInputValue(this.value); | ||
this.forceUpdate(); | ||
} | ||
var isSelectionChanged = !previousSelection || previousSelection.start !== newSelection.start || previousSelection.end !== newSelection.end; | ||
if (isSelectionChanged) { | ||
this.setSelection(newSelection.start, newSelection.end); | ||
} | ||
}; | ||
_proto.componentWillUnmount = function componentWillUnmount() { | ||
this.mounted = false; | ||
}; | ||
_proto.render = function render() { | ||
var _this2 = this; | ||
var _this$props = this.props, | ||
mask = _this$props.mask, | ||
alwaysShowMask = _this$props.alwaysShowMask, | ||
maskChar = _this$props.maskChar, | ||
formatChars = _this$props.formatChars, | ||
inputRef = _this$props.inputRef, | ||
beforeMaskedValueChange = _this$props.beforeMaskedValueChange, | ||
props = _objectWithoutProperties(_this$props, ["mask", "alwaysShowMask", "maskChar", "formatChars", "inputRef", "beforeMaskedValueChange"]); | ||
if (this.maskOptions.mask) { | ||
if (!props.disabled && !props.readOnly) { | ||
var handlersKeys = ['onChange', 'onPaste', 'onMouseDown']; | ||
handlersKeys.forEach(function (key) { | ||
props[key] = _this2[key]; | ||
}); | ||
} | ||
if (props.value != null) { | ||
props.value = this.value; | ||
} | ||
} | ||
}); | ||
}; | ||
var _default = InputElement; | ||
exports.default = _default; | ||
module.exports = exports["default"]; | ||
return React.createElement("input", _extends({ | ||
ref: this.handleRef | ||
}, props, { | ||
onFocus: this.onFocus, | ||
onBlur: this.onBlur | ||
})); | ||
}; | ||
_inheritsLoose(InputElement, _React$Component); | ||
return InputElement; | ||
}(React.Component); | ||
module.exports = InputElement; |
{ | ||
"name": "react-input-mask", | ||
"description": "Masked input component for React", | ||
"version": "1.2.2", | ||
"version": "2.0.0-beta.0", | ||
"homepage": "https://github.com/sanniassin/react-input-mask", | ||
@@ -19,44 +19,41 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@babel/cli": "^7.0.0-beta.39", | ||
"@babel/core": "^7.0.0-beta.39", | ||
"@babel/polyfill": "^7.0.0-beta.39", | ||
"@babel/preset-es2015": "^7.0.0-beta.39", | ||
"@babel/preset-react": "^7.0.0-beta.39", | ||
"@babel/preset-stage-2": "^7.0.0-beta.39", | ||
"@babel/register": "^7.0.0-beta.39", | ||
"@babel/cli": "^7.0.0-beta.46", | ||
"@babel/core": "^7.0.0-beta.46", | ||
"@babel/plugin-transform-modules-commonjs": "^7.0.0-beta.46", | ||
"@babel/plugin-transform-proto-to-assign": "^7.0.0-beta.46", | ||
"@babel/polyfill": "^7.0.0-beta.46", | ||
"@babel/preset-env": "^7.0.0-beta.46", | ||
"@babel/preset-react": "^7.0.0-beta.46", | ||
"@babel/preset-stage-3": "^7.0.0-beta.46", | ||
"@babel/register": "^7.0.0-beta.46", | ||
"babel-eslint": "^7.2.3", | ||
"babel-loader": "^8.0.0-beta.0", | ||
"babel-plugin-add-module-exports": "^0.2.1", | ||
"babel-loader": "^8.0.0-beta.2", | ||
"chai": "^4.1.2", | ||
"console-polyfill": "^0.3.0", | ||
"cross-env": "^5.1.3", | ||
"cross-env": "^5.1.4", | ||
"es3ify-loader": "^0.2.0", | ||
"eslint": "^3.19.0", | ||
"eslint-config-airbnb": "^15.1.0", | ||
"eslint-plugin-import": "^2.8.0", | ||
"eslint-plugin-import": "^2.11.0", | ||
"eslint-plugin-jsx-a11y": "^5.1.1", | ||
"eslint-plugin-react": "^7.6.1", | ||
"eslint-plugin-react": "^7.7.0", | ||
"html-webpack-plugin": "^2.30.1", | ||
"karma": "^2.0.0", | ||
"karma": "^2.0.2", | ||
"karma-browserstack-launcher": "^1.3.0", | ||
"karma-chrome-launcher": "^2.2.0", | ||
"karma-mocha": "^1.3.0", | ||
"karma-webpack": "^2.0.9", | ||
"mocha": "^5.0.0", | ||
"puppeteer": "^1.0.0", | ||
"react": "^16.2.0", | ||
"react-dom": "^16.2.0", | ||
"rollup": "^0.55.3", | ||
"rollup-plugin-babel": "^4.0.0-beta.1", | ||
"rollup-plugin-node-resolve": "^3.0.2", | ||
"rollup-plugin-replace": "^2.0.0", | ||
"karma-webpack": "^2.0.13", | ||
"mocha": "^5.1.1", | ||
"puppeteer": "^1.3.0", | ||
"react": "^16.3.2", | ||
"react-dom": "^16.3.2", | ||
"rollup": "^0.58.2", | ||
"rollup-plugin-babel": "^4.0.0-beta.4", | ||
"rollup-plugin-size-snapshot": "^0.4.0", | ||
"rollup-plugin-uglify": "^3.0.0", | ||
"webpack": "^3.10.0", | ||
"webpack-dev-server": "^2.11.1" | ||
"webpack": "^3.11.0", | ||
"webpack-dev-server": "^2.11.2" | ||
}, | ||
"main": "lib/index.js", | ||
"module": "es/index.js", | ||
"jsnext:main": "es/index.js", | ||
"files": [ | ||
"es", | ||
"lib", | ||
@@ -66,15 +63,11 @@ "dist" | ||
"scripts": { | ||
"clean": "rimraf lib dist es", | ||
"build": "npm run build:commonjs && npm run build:es && npm run build:umd && npm run build:umd:min", | ||
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib", | ||
"build:es": "cross-env BABEL_ENV=es babel src --out-dir es", | ||
"build:umd": "cross-env BABEL_ENV=es NODE_ENV=development rollup -c -i src/index.js -o dist/react-input-mask.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", | ||
"clean": "rimraf lib dist", | ||
"build": "rollup -c", | ||
"dev": "cross-env BABEL_ENV=test NODE_ENV=development webpack-dev-server", | ||
"dev-ie8": "cross-env BABEL_ENV=test NODE_ENV=development webpack-dev-server --inline=false", | ||
"prepare": "npm test && npm run clean && npm run build", | ||
"test": "npm run build && npm run test:input && npm run test:server-render && npm run test:build", | ||
"test:input": "cross-env BABEL_ENV=tests karma start", | ||
"test:server-render": "cross-env BABEL_ENV=tests mocha --require @babel/register ./tests/server-render", | ||
"test:build": "cross-env BABEL_ENV=tests mocha --require @babel/register ./tests/build" | ||
"test:input": "cross-env BABEL_ENV=test karma start", | ||
"test:server-render": "cross-env BABEL_ENV=test mocha --require @babel/register ./tests/server-render", | ||
"test:build": "cross-env BABEL_ENV=test mocha --require @babel/register ./tests/build" | ||
}, | ||
@@ -81,0 +74,0 @@ "repository": { |
@@ -54,2 +54,30 @@ # react-input-mask | ||
### `beforeMaskedValueChange` : `function` | ||
In case you need to implement more complex masking behavior, you can provide `beforeMaskedValueChange` function to change masked value and cursor position before it will be applied to the input. `beforeMaskedValueChange` receives following arguments: | ||
1. **newState** (object): New input state. Contains `value` and `selection` fields. `selection` is null on input blur. Example: `{ value: '12/1_/____', selection: { start: 4, end: 4 } }` | ||
2. **oldState** (object): Input state before change. Contains `value` and `selection` fields. `selection` is null on input focus. | ||
3. **userInput** (string): Raw entered or pasted string. `null` if user didn't enter anything (e.g. triggered by deletion or rerender due to props change). | ||
4. **maskOptions** (object): Mask options. Example: | ||
```js | ||
{ | ||
mask: '99/99/9999', | ||
maskChar: '_', | ||
alwaysShowMask: false, | ||
formatChars: { | ||
'9': '[0-9]', | ||
'a': '[A-Za-z]', | ||
'*': '[A-Za-z0-9]' | ||
}, | ||
permanents: [2, 5] // permanents is an array of indexes of the non-editable characters in the mask | ||
} | ||
``` | ||
`beforeMaskedValueChange` must return an object with following fields: | ||
1. **value** (string): New value. | ||
2. **selection** (object): New selection. If `selection` in `newState` argument is null, it must be null too. | ||
Please note that `beforeMaskedValueChange` executes more often than `onChange` and must be pure. | ||
## Example | ||
@@ -67,3 +95,45 @@ ```jsx | ||
Mask for ZIP Code. Uses beforeMaskedValueChange to omit trailing minus if it wasn't entered by user: | ||
```jsx | ||
import React from 'react'; | ||
import InputMask from 'react-input-mask'; | ||
class Input extends React.Component { | ||
state = { | ||
value: '' | ||
} | ||
onChange = (event) => { | ||
this.setState({ | ||
value: event.target.value | ||
}); | ||
} | ||
beforeMaskedValueChange = (newState, oldState, userInput) => { | ||
var { value } = newState; | ||
var selection = newState.selection; | ||
var cursorPosition = selection ? selection.start : null; | ||
// keep minus if entered by user | ||
if (value.endsWith('-') && userInput !== '-' && !this.state.value.endsWith('-')) { | ||
if (cursorPosition === value.length) { | ||
cursorPosition--; | ||
selection = { start: cursorPosition, end: cursorPosition }; | ||
} | ||
value = value.slice(0, -1); | ||
} | ||
return { | ||
value, | ||
selection | ||
}; | ||
} | ||
render() { | ||
return <InputMask mask="99999-9999" maskChar={null} value={this.state.value} onChange={this.onChange} beforeMaskedValueChange={this.beforeMaskedValueChange} />; | ||
} | ||
} | ||
``` | ||
## Thanks | ||
Thanks to [BrowserStack](https://www.browserstack.com/) for the help with testing on real devices | ||
Thanks to [BrowserStack](https://www.browserstack.com/) for the help with testing on real devices |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
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
137
81239
6
1678
2
1