react-maskedinput
Version:
Masked <input/> React component
6 lines • 13.9 kB
JavaScript
/*!
* react-maskedinput v4.0.1 - https://github.com/insin/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)}("undefined"!=typeof self?self:this,function(t){return function(t){var e={};function s(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,s),i.l=!0,i.exports}return s.m=t,s.c=e,s.d=function(t,e,n){s.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:n})},s.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return s.d(e,"a",e),e},s.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},s.p="",s(s.s=0)}([function(t,e,s){t.exports=s(1)},function(t,e,s){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n,i,r=s(2),a=s.n(r),o=s(3),l=s.n(o),h=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var s=arguments[e];for(var n in s)Object.prototype.hasOwnProperty.call(s,n)&&(t[n]=s[n])}return t};function p(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}var u=90,c=89;function d(t){var e=void 0,s=void 0;if(void 0!==t.selectionStart)e=t.selectionStart,s=t.selectionEnd;else try{t.focus();var n=t.createTextRange(),i=n.duplicate();n.moveToBookmark(document.selection.createRange().getBookmark()),i.setEndPoint("EndToStart",n),s=(e=i.text.length)+n.text.length}catch(t){}return{start:e,end:s}}var f=(i=n=function(t){function e(){var s,n;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e);for(var i=arguments.length,r=Array(i),a=0;a<i;a++)r[a]=arguments[a];return s=n=p(this,t.call.apply(t,[this].concat(r))),n._onChange=function(t){var e=n.mask.getValue(),s=t.target.value;s!==e&&(n._updateMaskSelection(),n.mask.setValue(s),t.target.value=n._getDisplayValue(),n._updateInputSelection()),n.props.onChange&&n.props.onChange(t)},n._onKeyDown=function(t){if(function(t){return(t.ctrlKey||t.metaKey)&&t.keyCode===(t.shiftKey?c:u)}(t))return t.preventDefault(),void(n.mask.undo()&&(t.target.value=n._getDisplayValue(),n._updateInputSelection(),n.props.onChange&&n.props.onChange(t)));if(function(t){return(t.ctrlKey||t.metaKey)&&t.keyCode===(t.shiftKey?u:c)}(t))return t.preventDefault(),void(n.mask.redo()&&(t.target.value=n._getDisplayValue(),n._updateInputSelection(),n.props.onChange&&n.props.onChange(t)));if("Backspace"===t.key&&(t.preventDefault(),n._updateMaskSelection(),n.mask.backspace())){var e=n._getDisplayValue();t.target.value=e,e&&n._updateInputSelection(),n.props.onChange&&n.props.onChange(t)}},n._onKeyPress=function(t){t.metaKey||t.altKey||t.ctrlKey||"Enter"===t.key||(t.preventDefault(),n._updateMaskSelection(),n.mask.input(t.key||t.data)&&(t.target.value=n.mask.getValue(),n._updateInputSelection(),n.props.onChange&&n.props.onChange(t)))},n._onPaste=function(t){t.preventDefault(),n._updateMaskSelection(),n.mask.paste(t.clipboardData.getData("Text"))&&(t.target.value=n.mask.getValue(),setTimeout(function(){return n._updateInputSelection()},0),n.props.onChange&&n.props.onChange(t))},p(n,s)}return function(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)}(e,t),e.prototype.componentWillMount=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 l.a(t)},e.prototype.componentWillReceiveProps=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)},e.prototype.componentWillUpdate=function(t,e){t.mask!==this.props.mask&&this._updatePattern(t)},e.prototype.componentDidUpdate=function(t){t.mask!==this.props.mask&&this.mask.selection.start&&this._updateInputSelection()},e.prototype._updatePattern=function(t){this.mask.setPattern(t.mask,{value:this.mask.getRawValue(),selection:d(this.input)})},e.prototype._updateMaskSelection=function(){this.mask.selection=d(this.input)},e.prototype._updateInputSelection=function(){!function(t,e){try{if(void 0!==t.selectionStart)t.focus(),t.setSelectionRange(e.start,e.end);else{t.focus();var s=t.createTextRange();s.collapse(!0),s.moveStart("character",e.start),s.moveEnd("character",e.end-e.start),s.select()}}catch(t){}}(this.input,this.mask.selection)},e.prototype._getDisplayValue=function(){var t=this.mask.getValue();return t===this.mask.emptyValue?"":t},e.prototype._keyPressPropName=function(){return"undefined"!=typeof navigator&&navigator.userAgent.match(/Android/i)?"onBeforeInput":"onKeyPress"},e.prototype._getEventHandlers=function(){var t;return(t={onChange:this._onChange,onKeyDown:this._onKeyDown,onPaste:this._onPaste})[this._keyPressPropName()]=this._onKeyPress,t},e.prototype.focus=function(){this.input.focus()},e.prototype.blur=function(){this.input.blur()},e.prototype.render=function(){var t=this,e=this.mask.pattern.length,s=this._getDisplayValue(),n=this._getEventHandlers(),i=this.props,r=i.size,o=void 0===r?e:r,l=i.placeholder,p=void 0===l?this.mask.emptyValue:l,u=this.props,c=(u.placeholderChar,u.formatCharacters,function(t,e){var s={};for(var n in t)e.indexOf(n)>=0||Object.prototype.hasOwnProperty.call(t,n)&&(s[n]=t[n]);return s}(u,["placeholderChar","formatCharacters"])),d=h({},c,n,{ref:function(e){t.input=e},maxLength:e,value:s,size:o,placeholder:p});return a.a.createElement("input",d)},e}(a.a.Component),n.defaultProps={value:""},i);e.default=f},function(e,s){e.exports=t},function(t,e,s){"use strict";function n(t,e){if(e)for(var s=Object.keys(e),n=0,i=s.length;n<i;n++)t[s[n]]=e[s[n]];return t}function i(t){return n({},t)}var r=/^\d$/,a=/^[A-Za-z]$/,o=/^[\dA-Za-z]$/,l="_",h={"*":{validate:function(t){return o.test(t)}},1:{validate:function(t){return r.test(t)}},a:{validate:function(t){return a.test(t)}},A:{validate:function(t){return a.test(t)},transform:function(t){return t.toUpperCase()}},"#":{validate:function(t){return o.test(t)},transform:function(t){return t.toUpperCase()}}};function p(t,e,s,n){if(!(this instanceof p))return new p(t,e,s);this.placeholderChar=s||l,this.formatCharacters=e||h,this.source=t,this.pattern=[],this.length=0,this.firstEditableIndex=null,this.lastEditableIndex=null,this._editableIndices={},this.isRevealingMask=n||!1,this._parse()}function u(t){if(!(this instanceof u))return new u(t);if(null==(t=n({formatCharacters:null,pattern:null,isRevealingMask:!1,placeholderChar:l,selection:{start:0,end:0},value:""},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=function(t){var e=i(h);if(t)for(var s=Object.keys(t),n=0,r=s.length;n<r;n++){var a=s[n];null==t[a]?delete e[a]:e[a]=t[a]}return e}(t.formatCharacters),this.setPattern(t.pattern,{value:t.value,selection:t.selection,isRevealingMask:t.isRevealingMask})}p.prototype._parse=function(){for(var t=this.source.split(""),e=0,s=[],n=0,i=t.length;n<i;n++){var r=t[n];if("\\"===r){if(n===i-1)throw new Error("InputMask: pattern ends with a raw \\");r=t[++n]}else r in this.formatCharacters&&(null===this.firstEditableIndex&&(this.firstEditableIndex=e),this.lastEditableIndex=e,this._editableIndices[e]=!0);s.push(r),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},p.prototype.formatValue=function(t){for(var e=new Array(this.length),s=0,n=0,i=this.length;n<i;n++)if(this.isEditableIndex(n)){if(this.isRevealingMask&&t.length<=s&&!this.isValidAtIndex(t[s],n))break;e[n]=t.length>s&&this.isValidAtIndex(t[s],n)?this.transform(t[s],n):this.placeholderChar,s++}else e[n]=this.pattern[n],t.length>s&&t[s]===this.pattern[n]&&s++;return e},p.prototype.isEditableIndex=function(t){return!!this._editableIndices[t]},p.prototype.isValidAtIndex=function(t,e){return this.formatCharacters[this.pattern[e]].validate(t)},p.prototype.transform=function(t,e){var s=this.formatCharacters[this.pattern[e]];return"function"==typeof s.transform?s.transform(t):t},u.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 r=this.selection.end-1;r>n;)this.pattern.isEditableIndex(r)&&(this.value[r]=this.placeholderChar),r--;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},u.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},u.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 s=0,r=this.pattern.firstEditableIndex-this.selection.start;s<r;s++)if(t.charAt(s)!==this.pattern.pattern[s])return!1;t=t.substring(this.pattern.firstEditableIndex-this.selection.start),this.selection.start=this.pattern.firstEditableIndex}for(s=0,r=t.length;s<r&&this.selection.start<=this.pattern.lastEditableIndex;s++){if(!this.input(t.charAt(s))){if(this.selection.start>0){var a=this.selection.start-1;if(!this.pattern.isEditableIndex(a)&&t.charAt(s)===this.pattern.pattern[a])continue}return n(this,e),!1}}return!0},u.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},u.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},u.prototype.setPattern=function(t,e){e=n({selection:{start:0,end:0},value:""},e),this.pattern=new p(t,this.formatCharacters,this.placeholderChar,e.isRevealingMask),this.setValue(e.value),this.emptyValue=this.pattern.formatValue([]).join(""),this.selection=e.selection,this._resetHistory()},u.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},u.prototype.setValue=function(t){null==t&&(t=""),this.value=this.pattern.formatValue(t.split(""))},u.prototype.getValue=function(){return this.value.join("")},u.prototype.getRawValue=function(){for(var t=[],e=0;e<this.value.length;e++)!0===this.pattern._editableIndices[e]&&t.push(this.value[e]);return t.join("")},u.prototype._resetHistory=function(){this._history=[],this._historyIndex=null,this._lastOp=null,this._lastSelection=i(this.selection)},u.Pattern=p,t.exports=u}]).default});
//# sourceMappingURL=react-maskedinput.min.js.map