react-maskedinput-autofill
Version:
Masked <input/> React component
6 lines • 16.4 kB
JavaScript
/*!
* react-maskedinput-autofill 3.3.6 - https://github.com/aesopwolf/react-maskedinput
* MIT Licensed
*/
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):"object"==typeof exports?exports.MaskedInput=e(require("react")):t.MaskedInput=e(t.React)}(this,function(t){return function(t){function e(i){if(s[i])return s[i].exports;var n=s[i]={exports:{},id:i,loaded:!1};return t[i].call(n.exports,n,n.exports,e),n.loaded=!0,n.exports}var s={};return e.m=t,e.c=s,e.p="",e(0)}([function(t,e,s){"use strict";function i(t,e){var s={};for(var i in t)e.indexOf(i)>=0||Object.prototype.hasOwnProperty.call(t,i)&&(s[i]=t[i]);return s}function n(t,e,s){return e in t?Object.defineProperty(t,e,{value:s,enumerable:!0,configurable:!0,writable:!0}):t[e]=s,t}function a(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function r(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function o(t){return(t.ctrlKey||t.metaKey)&&t.keyCode===(t.shiftKey?m:g)}function l(t){return(t.ctrlKey||t.metaKey)&&t.keyCode===(t.shiftKey?g:m)}function h(t){var e,s,i,n;if(void 0!==t.selectionStart)e=t.selectionStart,s=t.selectionEnd;else try{t.focus(),i=t.createTextRange(),n=i.duplicate(),i.moveToBookmark(document.selection.createRange().getBookmark()),n.setEndPoint("EndToStart",i),e=n.text.length,s=e+i.text.length}catch(a){}return{start:e,end:s}}function u(t,e){var s;try{void 0!==t.selectionStart?(t.focus(),t.setSelectionRange(e.start,e.end)):(t.focus(),s=t.createTextRange(),s.collapse(!0),s.moveStart("character",e.start),s.moveEnd("character",e.end-e.start),s.select())}catch(i){}}Object.defineProperty(e,"__esModule",{value:!0});var p=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var s=arguments[e];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(t[i]=s[i])}return t},c=function(){function t(t,e){for(var s=0;s<e.length;s++){var i=e[s];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,s,i){return s&&t(e.prototype,s),i&&t(e,i),e}}(),d=function(t,e,s){for(var i=!0;i;){var n=t,a=e,r=s;i=!1,null===n&&(n=Function.prototype);var o=Object.getOwnPropertyDescriptor(n,a);if(void 0!==o){if("value"in o)return o.value;var l=o.get;if(void 0===l)return;return l.call(r)}var h=Object.getPrototypeOf(n);if(null===h)return;t=h,e=a,s=r,i=!0,o=h=void 0}},f=s(5),v=s(7),y=s(3),g=90,m=89,k=function(t){function e(t){a(this,e),d(Object.getPrototypeOf(e.prototype),"constructor",this).call(this,t),this.updatePattern=this.updatePattern.bind(this),this.updateMaskSelection=this.updateMaskSelection.bind(this),this.updateInputSelection=this.updateInputSelection.bind(this),this.onChange=this.onChange.bind(this),this.onKeyDown=this.onKeyDown.bind(this),this.onKeyPress=this.onKeyPress.bind(this),this.onPaste=this.onPaste.bind(this),this.getDisplayValue=this.getDisplayValue.bind(this),this.keyPressPropName=this.keyPressPropName.bind(this),this.getEventHandlers=this.getEventHandlers.bind(this)}return r(e,t),c(e,[{key:"componentWillMount",value:function(){var t={pattern:this.props.mask,value:this.props.value,formatCharacters:this.props.formatCharacters};this.props.placeholderChar&&(t.placeholderChar=this.props.placeholderChar),this.mask=new y(t)}},{key:"componentWillReceiveProps",value:function(t){this.props.mask!==t.mask&&this.props.value!==t.mask?this.mask.getValue()===this.mask.emptyValue?this.mask.setPattern(t.mask,{value:t.value}):this.mask.setPattern(t.mask,{value:this.mask.getRawValue()}):this.props.mask!==t.mask?this.mask.setPattern(t.mask,{value:this.mask.getRawValue()}):this.props.value!==t.value&&this.mask.setValue(t.value)}},{key:"componentWillUpdate",value:function(t,e){t.mask!==this.props.mask&&this.updatePattern(t)}},{key:"componentDidUpdate",value:function(t){t.mask!==this.props.mask&&this.mask.selection.start&&this.updateInputSelection()}},{key:"updatePattern",value:function(t){this.mask.setPattern(t.mask,{value:this.mask.getRawValue(),selection:h(this.input)})}},{key:"updateMaskSelection",value:function(){this.mask.selection=h(this.input)}},{key:"updateInputSelection",value:function(){u(this.input,this.mask.selection)}},{key:"onChange",value:function(t){var e=this.mask.getValue();if(t.target.value!==e){if(t.target.value.length<e.length){var s=e.length-t.target.value.length;this.updateMaskSelection(),this.mask.selection.end=this.mask.selection.start+s,this.mask.backspace()}this.mask.setValue(t.target.value);var i=this.getDisplayValue();t.target.value=i,i&&this.updateInputSelection()}this.props.onChange&&this.props.onChange(t)}},{key:"onKeyDown",value:function(t){if(o(t))return t.preventDefault(),void(this.mask.undo()&&(t.target.value=this.getDisplayValue(),this.updateInputSelection(),this.props.onChange&&this.props.onChange(t)));if(l(t))return t.preventDefault(),void(this.mask.redo()&&(t.target.value=this.getDisplayValue(),this.updateInputSelection(),this.props.onChange&&this.props.onChange(t)));if("Backspace"===t.key&&(t.preventDefault(),this.updateMaskSelection(),this.mask.backspace())){var e=this.getDisplayValue();t.target.value=e,e&&this.updateInputSelection(),this.props.onChange&&this.props.onChange(t)}}},{key:"onKeyPress",value:function(t){t.metaKey||t.altKey||t.ctrlKey||"Enter"===t.key||(t.preventDefault(),this.updateMaskSelection(),this.mask.input(t.key||t.data)&&(t.target.value=this.mask.getValue(),this.updateInputSelection(),this.props.onChange&&this.props.onChange(t)))}},{key:"onPaste",value:function(t){if(t.preventDefault(),this.updateMaskSelection(),this.mask.paste(t.clipboardData.getData("Text")))t.target.value=this.mask.getValue(),setTimeout(this.updateInputSelection,0),this.props.onChange&&this.props.onChange(t);else{this.mask.setValue(t.clipboardData.getData("Text"));var e=this.getDisplayValue();t.target.value=e,e&&this.updateInputSelection()}}},{key:"getDisplayValue",value:function(){var t=this.mask.getValue();return t===this.mask.emptyValue?"":t}},{key:"keyPressPropName",value:function(){return"undefined"!=typeof navigator&&navigator.userAgent.match(/Android/i)?"onBeforeInput":"onKeyPress"}},{key:"getEventHandlers",value:function(){return n({onChange:this.onChange,onKeyDown:this.onKeyDown,onPaste:this.onPaste},this.keyPressPropName(),this.onKeyPress)}},{key:"focus",value:function(){this.input.focus()}},{key:"blur",value:function(){this.input.blur()}},{key:"render",value:function(){var t=this,e=function(e){return t.input=e},s=this.mask.pattern.length,n=this.getDisplayValue(),a=this.getEventHandlers(),r=this.props,o=r.size,l=void 0===o?s:o,h=r.placeholder,u=void 0===h?this.mask.emptyValue:h,c=this.props,d=(c.placeholderChar,c.formatCharacters,i(c,["placeholderChar","formatCharacters"])),f=p({},d,a,{ref:e,maxLength:s,value:n,size:l,placeholder:u});return v.createElement("input",f)}}]),e}(v.Component);k.propTypes={mask:f.string.isRequired,formatCharacters:f.object,onChange:f.func,placeholderChar:f.string,value:f.string},k.defaultProps={formatCharacters:null,onChange:function(){},placeholderChar:null,value:""},e.default=k,t.exports=e.default},function(t,e){"use strict";function s(t){return function(){return t}}var i=function(){};i.thatReturns=s,i.thatReturnsFalse=s(!1),i.thatReturnsTrue=s(!0),i.thatReturnsNull=s(null),i.thatReturnsThis=function(){return this},i.thatReturnsArgument=function(t){return t},t.exports=i},function(t,e,s){"use strict";function i(t,e,s,i,a,r,o,l){if(n(e),!t){var h;if(void 0===e)h=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var u=[s,i,a,r,o,l],p=0;h=new Error(e.replace(/%s/g,function(){return u[p++]})),h.name="Invariant Violation"}throw h.framesToPop=1,h}}var n=function(t){};t.exports=i},function(t,e){"use strict";function s(t,e){if(e)for(var s=Object.keys(e),i=0,n=s.length;i<n;i++)t[s[i]]=e[s[i]];return t}function i(t){return s({},t)}function n(t){var e=i(c);if(t)for(var s=Object.keys(t),n=0,a=s.length;n<a;n++){var r=s[n];null==t[r]?delete e[r]:e[r]=t[r]}return e}function a(t,e,s,i){return this instanceof a?(this.placeholderChar=s||p,this.formatCharacters=e||c,this.source=t,this.pattern=[],this.length=0,this.firstEditableIndex=null,this.lastEditableIndex=null,this._editableIndices={},this.isRevealingMask=i||!1,void this._parse()):new a(t,e,s)}function r(t){if(!(this instanceof r))return new r(t);if(t=s({formatCharacters:null,pattern:null,isRevealingMask:!1,placeholderChar:p,selection:{start:0,end:0},value:""},t),null==t.pattern)throw new Error("InputMask: you must provide a pattern.");if("string"!=typeof t.placeholderChar||t.placeholderChar.length>1)throw new Error("InputMask: placeholderChar should be a single character or an empty string.");this.placeholderChar=t.placeholderChar,this.formatCharacters=n(t.formatCharacters),this.setPattern(t.pattern,{value:t.value,selection:t.selection,isRevealingMask:t.isRevealingMask})}var o="\\",l=/^\d$/,h=/^[A-Za-z]$/,u=/^[\dA-Za-z]$/,p="_",c={"*":{validate:function(t){return u.test(t)}},1:{validate:function(t){return l.test(t)}},a:{validate:function(t){return h.test(t)}},A:{validate:function(t){return h.test(t)},transform:function(t){return t.toUpperCase()}},"#":{validate:function(t){return u.test(t)},transform:function(t){return t.toUpperCase()}}};a.prototype._parse=function(){for(var t=this.source.split(""),e=0,s=[],i=0,n=t.length;i<n;i++){var a=t[i];if(a===o){if(i===n-1)throw new Error("InputMask: pattern ends with a raw "+o);a=t[++i]}else a in this.formatCharacters&&(null===this.firstEditableIndex&&(this.firstEditableIndex=e),this.lastEditableIndex=e,this._editableIndices[e]=!0);s.push(a),e++}if(null===this.firstEditableIndex)throw new Error('InputMask: pattern "'+this.source+'" does not contain any editable characters.');this.pattern=s,this.length=s.length},a.prototype.formatValue=function(t){for(var e=new Array(this.length),s=0,i=0,n=this.length;i<n;i++)if(this.isEditableIndex(i)){if(this.isRevealingMask&&t.length<=s&&!this.isValidAtIndex(t[s],i))break;e[i]=t.length>s&&this.isValidAtIndex(t[s],i)?this.transform(t[s],i):this.placeholderChar,s++}else e[i]=this.pattern[i],t.length>s&&t[s]===this.pattern[i]&&s++;return e},a.prototype.isEditableIndex=function(t){return!!this._editableIndices[t]},a.prototype.isValidAtIndex=function(t,e){return this.formatCharacters[this.pattern[e]].validate(t)},a.prototype.transform=function(t,e){var s=this.formatCharacters[this.pattern[e]];return"function"==typeof s.transform?s.transform(t):t},r.prototype.input=function(t){if(this.selection.start===this.selection.end&&this.selection.start===this.pattern.length)return!1;var e=i(this.selection),s=this.getValue(),n=this.selection.start;if(n<this.pattern.firstEditableIndex&&(n=this.pattern.firstEditableIndex),this.pattern.isEditableIndex(n)){if(!this.pattern.isValidAtIndex(t,n))return!1;this.value[n]=this.pattern.transform(t,n)}for(var a=this.selection.end-1;a>n;)this.pattern.isEditableIndex(a)&&(this.value[a]=this.placeholderChar),a--;for(this.selection.start=this.selection.end=n+1;this.pattern.length>this.selection.start&&!this.pattern.isEditableIndex(this.selection.start);)this.selection.start++,this.selection.end++;return null!=this._historyIndex&&(this._history.splice(this._historyIndex,this._history.length-this._historyIndex),this._historyIndex=null),("input"!==this._lastOp||e.start!==e.end||null!==this._lastSelection&&e.start!==this._lastSelection.start)&&this._history.push({value:s,selection:e,lastOp:this._lastOp}),this._lastOp="input",this._lastSelection=i(this.selection),!0},r.prototype.backspace=function(){if(0===this.selection.start&&0===this.selection.end)return!1;var t=i(this.selection),e=this.getValue();if(this.selection.start===this.selection.end)this.pattern.isEditableIndex(this.selection.start-1)&&(this.value[this.selection.start-1]=this.placeholderChar),this.selection.start--,this.selection.end--;else{for(var s=this.selection.end-1;s>=this.selection.start;)this.pattern.isEditableIndex(s)&&(this.value[s]=this.placeholderChar),s--;this.selection.end=this.selection.start}return null!=this._historyIndex&&this._history.splice(this._historyIndex,this._history.length-this._historyIndex),("backspace"!==this._lastOp||t.start!==t.end||null!==this._lastSelection&&t.start!==this._lastSelection.start)&&this._history.push({value:e,selection:t,lastOp:this._lastOp}),this._lastOp="backspace",this._lastSelection=i(this.selection),!0},r.prototype.paste=function(t){var e={value:this.value.slice(),selection:i(this.selection),_lastOp:this._lastOp,_history:this._history.slice(),_historyIndex:this._historyIndex,_lastSelection:i(this._lastSelection)};if(this.selection.start<this.pattern.firstEditableIndex){for(var n=0,a=this.pattern.firstEditableIndex-this.selection.start;n<a;n++)if(t.charAt(n)!==this.pattern.pattern[n])return!1;t=t.substring(this.pattern.firstEditableIndex-this.selection.start),this.selection.start=this.pattern.firstEditableIndex}for(n=0,a=t.length;n<a&&this.selection.start<=this.pattern.lastEditableIndex;n++){var r=this.input(t.charAt(n));if(!r){if(this.selection.start>0){var o=this.selection.start-1;if(!this.pattern.isEditableIndex(o)&&t.charAt(n)===this.pattern.pattern[o])continue}return s(this,e),!1}}return!0},r.prototype.undo=function(){if(0===this._history.length||0===this._historyIndex)return!1;var t;if(null==this._historyIndex){this._historyIndex=this._history.length-1,t=this._history[this._historyIndex];var e=this.getValue();t.value===e&&t.selection.start===this.selection.start&&t.selection.end===this.selection.end||this._history.push({value:e,selection:i(this.selection),lastOp:this._lastOp,startUndo:!0})}else t=this._history[--this._historyIndex];return this.value=t.value.split(""),this.selection=t.selection,this._lastOp=t.lastOp,!0},r.prototype.redo=function(){if(0===this._history.length||null==this._historyIndex)return!1;var t=this._history[++this._historyIndex];return this._historyIndex===this._history.length-1&&(this._historyIndex=null,t.startUndo&&this._history.pop()),this.value=t.value.split(""),this.selection=t.selection,this._lastOp=t.lastOp,!0},r.prototype.setPattern=function(t,e){e=s({selection:{start:0,end:0},value:""},e),this.pattern=new a(t,this.formatCharacters,this.placeholderChar,e.isRevealingMask),this.setValue(e.value),this.emptyValue=this.pattern.formatValue([]).join(""),this.selection=e.selection,this._resetHistory()},r.prototype.setSelection=function(t){if(this.selection=i(t),this.selection.start===this.selection.end){if(this.selection.start<this.pattern.firstEditableIndex)return this.selection.start=this.selection.end=this.pattern.firstEditableIndex,!0;for(var e=this.selection.start;e>=this.pattern.firstEditableIndex;){if(this.pattern.isEditableIndex(e-1)&&this.value[e-1]!==this.placeholderChar||e===this.pattern.firstEditableIndex){this.selection.start=this.selection.end=e;break}e--}return!0}return!1},r.prototype.setValue=function(t){null==t&&(t=""),this.value=this.pattern.formatValue(t.split(""))},r.prototype.getValue=function(){return this.value.join("")},r.prototype.getRawValue=function(){for(var t=[],e=0;e<this.value.length;e++)this.pattern._editableIndices[e]===!0&&t.push(this.value[e]);return t.join("")},r.prototype._resetHistory=function(){this._history=[],this._historyIndex=null,this._lastOp=null,this._lastSelection=i(this.selection)},r.Pattern=a,t.exports=r},function(t,e,s){"use strict";var i=s(1),n=s(2),a=s(6);t.exports=function(){function t(t,e,s,i,r,o){o!==a&&n(!1,"Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types")}function e(){return t}t.isRequired=t;var s={array:t,bool:t,func:t,number:t,object:t,string:t,symbol:t,any:t,arrayOf:e,element:t,instanceOf:e,node:t,objectOf:e,oneOf:e,oneOfType:e,shape:e};return s.checkPropTypes=i,s.PropTypes=s,s}},function(t,e,s){t.exports=s(4)()},function(t,e){"use strict";var s="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED";t.exports=s},function(e,s){e.exports=t}])});
//# sourceMappingURL=react-maskedinput-autofill.min.js.map