UNPKG

16.3 kBJavaScriptView Raw
1/*!
2 * react-maskedinput 3.2.1 - https://github.com/insin/react-maskedinput
3 * MIT Licensed
4 */
5!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(s){if(n[s])return n[s].exports;var i=n[s]={exports:{},id:s,loaded:!1};return t[s].call(i.exports,i,i.exports,e),i.loaded=!0,i.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){"use strict";function s(t,e){var n={};for(var s in t)e.indexOf(s)>=0||Object.prototype.hasOwnProperty.call(t,s)&&(n[s]=t[s]);return n}function i(t){return(t.ctrlKey||t.metaKey)&&t.keyCode===(t.shiftKey?d:p)}function r(t){return(t.ctrlKey||t.metaKey)&&t.keyCode===(t.shiftKey?p:d)}var a=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var s in n)Object.prototype.hasOwnProperty.call(n,s)&&(t[s]=n[s])}return t},o=n(12),l=n(9),h=l.getSelection,c=l.setSelection,u=n(7),p=90,d=89,f=o.createClass({displayName:"MaskedInput",propTypes:{mask:o.PropTypes.string.isRequired,formatCharacters:o.PropTypes.object,placeholderChar:o.PropTypes.string},getDefaultProps:function(){return{value:""}},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 u(t)},componentWillReceiveProps:function(t){this.props.value!==t.value&&this.mask.setValue(t.value),this.props.mask!==t.mask&&this.mask.setPattern(t.mask,{value:this.mask.getRawValue()})},componentWillUpdate:function(t,e){t.mask!==this.props.mask&&this._updatePattern(t)},componentDidUpdate:function(t){t.mask!==this.props.mask&&this.mask.selection.start&&this._updateInputSelection()},_updatePattern:function(t){this.mask.setPattern(t.mask,{value:this.mask.getRawValue(),selection:h(this.input)})},_updateMaskSelection:function(){this.mask.selection=h(this.input)},_updateInputSelection:function(){c(this.input,this.mask.selection)},_onChange:function(t){var e=this.mask.getValue();if(t.target.value!==e){if(t.target.value.length<e.length){var n=e.length-t.target.value.length;this._updateMaskSelection(),this.mask.selection.end=this.mask.selection.start+n,this.mask.backspace()}var s=this._getDisplayValue();t.target.value=s,s&&this._updateInputSelection()}this.props.onChange&&this.props.onChange(t)},_onKeyDown:function(t){if(i(t))return t.preventDefault(),void(this.mask.undo()&&(t.target.value=this._getDisplayValue(),this._updateInputSelection(),this.props.onChange&&this.props.onChange(t)));if(r(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)}},_onKeyPress:function(t){t.metaKey||t.altKey||t.ctrlKey||"Enter"===t.key||(t.preventDefault(),this._updateMaskSelection(),this.mask.input(t.key)&&(t.target.value=this.mask.getValue(),this._updateInputSelection(),this.props.onChange&&this.props.onChange(t)))},_onPaste:function(t){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))},_getDisplayValue:function(){var t=this.mask.getValue();return t===this.mask.emptyValue?"":t},focus:function(){this.input.focus()},blur:function(){this.input.blur()},render:function(){var t=this,e=this.props,n=(e.mask,e.formatCharacters,e.size),i=e.placeholder,r=(e.placeholderChar,s(e,["mask","formatCharacters","size","placeholder","placeholderChar"])),l=this.mask.pattern.length;return o.createElement("input",a({},r,{ref:function(e){return t.input=e},maxLength:l,onChange:this._onChange,onKeyDown:this._onKeyDown,onKeyPress:this._onKeyPress,onPaste:this._onPaste,placeholder:i||this.mask.emptyValue,size:n||l,value:this._getDisplayValue()}))}});t.exports=f},function(t,e){"use strict";var n=!("undefined"==typeof window||!window.document||!window.document.createElement),s={canUseDOM:n,canUseWorkers:"undefined"!=typeof Worker,canUseEventListeners:n&&!(!window.addEventListener&&!window.attachEvent),canUseViewport:n&&!!window.screen,isInWorker:!n};t.exports=s},function(t,e,n){"use strict";function s(t,e){return!(!t||!e)&&(t===e||!i(t)&&(i(e)?s(t,e.parentNode):"contains"in t?t.contains(e):!!t.compareDocumentPosition&&!!(16&t.compareDocumentPosition(e))))}var i=n(6);t.exports=s},function(t,e){"use strict";function n(t){try{t.focus()}catch(e){}}t.exports=n},function(t,e){"use strict";function n(){if("undefined"==typeof document)return null;try{return document.activeElement||document.body}catch(t){return document.body}}t.exports=n},function(t,e){"use strict";function n(t){return!(!t||!("function"==typeof Node?t instanceof Node:"object"==typeof t&&"number"==typeof t.nodeType&&"string"==typeof t.nodeName))}t.exports=n},function(t,e,n){"use strict";function s(t){return i(t)&&3==t.nodeType}var i=n(5);t.exports=s},function(t,e){"use strict";function n(t,e){if(e)for(var n=Object.keys(e),s=0,i=n.length;s<i;s++)t[n[s]]=e[n[s]];return t}function s(t){return n({},t)}function i(t){var e=s(p);if(t)for(var n=Object.keys(t),i=0,r=n.length;i<r;i++){var a=n[i];null==t[a]?delete e[a]:e[a]=t[a]}return e}function r(t,e,n){return this instanceof r?(this.placeholderChar=n||u,this.formatCharacters=e||p,this.source=t,this.pattern=[],this.length=0,this.firstEditableIndex=null,this.lastEditableIndex=null,this._editableIndices={},void this._parse()):new r(t,e,n)}function a(t){if(!(this instanceof a))return new a(t);if(t=n({formatCharacters:null,pattern:null,placeholderChar:u,selection:{start:0,end:0},value:""},t),null==t.pattern)throw new Error("InputMask: you must provide a pattern.");if(1!==t.placeholderChar.length)throw new Error("InputMask: placeholderChar should be a single character.");this.placeholderChar=t.placeholderChar,this.formatCharacters=i(t.formatCharacters),this.setPattern(t.pattern,{value:t.value,selection:t.selection})}var o="\\",l=/^\d$/,h=/^[A-Za-z]$/,c=/^[\dA-Za-z]$/,u="_",p={"*":{validate:function(t){return c.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 c.test(t)},transform:function(t){return t.toUpperCase()}}};r.prototype._parse=function(){for(var t=this.source.split(""),e=0,n=[],s=0,i=t.length;s<i;s++){var r=t[s];if(r===o){if(s===i-1)throw new Error("InputMask: pattern ends with a raw "+o);r=t[++s]}else r in this.formatCharacters&&(null===this.firstEditableIndex&&(this.firstEditableIndex=e),this.lastEditableIndex=e,this._editableIndices[e]=!0);n.push(r),e++}if(null===this.firstEditableIndex)throw new Error('InputMask: pattern "'+this.source+'" does not contain any editable characters.');this.pattern=n,this.length=n.length},r.prototype.formatValue=function(t){for(var e=new Array(this.length),n=0,s=0,i=this.length;s<i;s++)this.isEditableIndex(s)?(e[s]=t.length>n&&this.isValidAtIndex(t[n],s)?this.transform(t[n],s):this.placeholderChar,n++):(e[s]=this.pattern[s],t.length>n&&t[n]===this.pattern[s]&&n++);return e},r.prototype.isEditableIndex=function(t){return!!this._editableIndices[t]},r.prototype.isValidAtIndex=function(t,e){return this.formatCharacters[this.pattern[e]].validate(t)},r.prototype.transform=function(t,e){var n=this.formatCharacters[this.pattern[e]];return"function"==typeof n.transform?n.transform(t):t},a.prototype.input=function(t){if(this.selection.start===this.selection.end&&this.selection.start===this.pattern.length)return!1;var e=s(this.selection),n=this.getValue(),i=this.selection.start;if(i<this.pattern.firstEditableIndex&&(i=this.pattern.firstEditableIndex),this.pattern.isEditableIndex(i)){if(!this.pattern.isValidAtIndex(t,i))return!1;this.value[i]=this.pattern.transform(t,i)}for(var r=this.selection.end-1;r>i;)this.pattern.isEditableIndex(r)&&(this.value[r]=this.placeholderChar),r--;for(this.selection.start=this.selection.end=i+1;this.pattern.length>this.selection.start&&!this.pattern.isEditableIndex(this.selection.start);)this.selection.start++,this.selection.end++;return null!=this._historyIndex&&(console.log("splice(",this._historyIndex,this._history.length-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:n,selection:e,lastOp:this._lastOp}),this._lastOp="input",this._lastSelection=s(this.selection),!0},a.prototype.backspace=function(){if(0===this.selection.start&&0===this.selection.end)return!1;var t=s(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 n=this.selection.end-1;n>=this.selection.start;)this.pattern.isEditableIndex(n)&&(this.value[n]=this.placeholderChar),n--;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=s(this.selection),!0},a.prototype.paste=function(t){var e={value:this.value.slice(),selection:s(this.selection),_lastOp:this._lastOp,_history:this._history.slice(),_historyIndex:this._historyIndex,_lastSelection:s(this._lastSelection)};if(this.selection.start<this.pattern.firstEditableIndex){for(var i=0,r=this.pattern.firstEditableIndex-this.selection.start;i<r;i++)if(t.charAt(i)!==this.pattern.pattern[i])return!1;t=t.substring(this.pattern.firstEditableIndex-this.selection.start),this.selection.start=this.pattern.firstEditableIndex}for(i=0,r=t.length;i<r&&this.selection.start<=this.pattern.lastEditableIndex;i++){var a=this.input(t.charAt(i));if(!a){if(this.selection.start>0){var o=this.selection.start-1;if(!this.pattern.isEditableIndex(o)&&t.charAt(i)===this.pattern.pattern[o])continue}return n(this,e),!1}}return!0},a.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:s(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},a.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},a.prototype.setPattern=function(t,e){e=n({selection:{start:0,end:0},value:""},e),this.pattern=new r(t,this.formatCharacters,this.placeholderChar),this.setValue(e.value),this.emptyValue=this.pattern.formatValue([]).join(""),this.selection=e.selection,this._resetHistory()},a.prototype.setSelection=function(t){if(this.selection=s(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;if(this.selection.end>this.pattern.lastEditableIndex+1)return this.selection.start=this.selection.end=this.pattern.lastEditableIndex+1,!0}return!1},a.prototype.setValue=function(t){null==t&&(t=""),this.value=this.pattern.formatValue(t.split(""))},a.prototype.getValue=function(){return this.value.join("")},a.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("")},a.prototype._resetHistory=function(){this._history=[],this._historyIndex=null,this._lastOp=null,this._lastSelection=s(this.selection)},a.Pattern=r,t.exports=a},function(t,e,n){"use strict";function s(t,e,n,s){return t===n&&e===s}function i(t){var e=document.selection,n=e.createRange(),s=n.text.length,i=n.duplicate();i.moveToElementText(t),i.setEndPoint("EndToStart",n);var r=i.text.length,a=r+s;return{start:r,end:a}}function r(t){var e=window.getSelection&&window.getSelection();if(!e||0===e.rangeCount)return null;var n=e.anchorNode,i=e.anchorOffset,r=e.focusNode,a=e.focusOffset,o=e.getRangeAt(0);try{o.startContainer.nodeType,o.endContainer.nodeType}catch(l){return null}var h=s(e.anchorNode,e.anchorOffset,e.focusNode,e.focusOffset),c=h?0:o.toString().length,u=o.cloneRange();u.selectNodeContents(t),u.setEnd(o.startContainer,o.startOffset);var p=s(u.startContainer,u.startOffset,u.endContainer,u.endOffset),d=p?0:u.toString().length,f=d+c,v=document.createRange();v.setStart(n,i),v.setEnd(r,a);var m=v.collapsed;return{start:m?f:d,end:m?d:f}}function a(t,e){var n,s,i=document.selection.createRange().duplicate();void 0===e.end?(n=e.start,s=n):e.start>e.end?(n=e.end,s=e.start):(n=e.start,s=e.end),i.moveToElementText(t),i.moveStart("character",n),i.setEndPoint("EndToStart",i),i.moveEnd("character",s-n),i.select()}function o(t,e){if(window.getSelection){var n=window.getSelection(),s=t[c()].length,i=Math.min(e.start,s),r=void 0===e.end?i:Math.min(e.end,s);if(!n.extend&&i>r){var a=r;r=i,i=a}var o=h(t,i),l=h(t,r);if(o&&l){var u=document.createRange();u.setStart(o.node,o.offset),n.removeAllRanges(),i>r?(n.addRange(u),n.extend(l.node,l.offset)):(u.setEnd(l.node,l.offset),n.addRange(u))}}}var l=n(1),h=n(10),c=n(11),u=l.canUseDOM&&"selection"in document&&!("getSelection"in window),p={getOffsets:u?i:r,setOffsets:u?a:o};t.exports=p},function(t,e,n){"use strict";function s(t){return r(document.documentElement,t)}var i=n(8),r=n(2),a=n(3),o=n(4),l={hasSelectionCapabilities:function(t){var e=t&&t.nodeName&&t.nodeName.toLowerCase();return e&&("input"===e&&"text"===t.type||"textarea"===e||"true"===t.contentEditable)},getSelectionInformation:function(){var t=o();return{focusedElem:t,selectionRange:l.hasSelectionCapabilities(t)?l.getSelection(t):null}},restoreSelection:function(t){var e=o(),n=t.focusedElem,i=t.selectionRange;e!==n&&s(n)&&(l.hasSelectionCapabilities(n)&&l.setSelection(n,i),a(n))},getSelection:function(t){var e;if("selectionStart"in t)e={start:t.selectionStart,end:t.selectionEnd};else if(document.selection&&t.nodeName&&"input"===t.nodeName.toLowerCase()){var n=document.selection.createRange();n.parentElement()===t&&(e={start:-n.moveStart("character",-t.value.length),end:-n.moveEnd("character",-t.value.length)})}else e=i.getOffsets(t);return e||{start:0,end:0}},setSelection:function(t,e){var n=e.start,s=e.end;if(void 0===s&&(s=n),"selectionStart"in t)t.selectionStart=n,t.selectionEnd=Math.min(s,t.value.length);else if(document.selection&&t.nodeName&&"input"===t.nodeName.toLowerCase()){var r=t.createTextRange();r.collapse(!0),r.moveStart("character",n),r.moveEnd("character",s-n),r.select()}else i.setOffsets(t,e)}};t.exports=l},function(t,e){"use strict";function n(t){for(;t&&t.firstChild;)t=t.firstChild;return t}function s(t){for(;t;){if(t.nextSibling)return t.nextSibling;t=t.parentNode}}function i(t,e){for(var i=n(t),r=0,a=0;i;){if(3===i.nodeType){if(a=r+i.textContent.length,r<=e&&a>=e)return{node:i,offset:e-r};r=a}i=n(s(i))}}t.exports=i},function(t,e,n){"use strict";function s(){return!r&&i.canUseDOM&&(r="textContent"in document.documentElement?"textContent":"innerText"),r}var i=n(1),r=null;t.exports=s},function(e,n){e.exports=t}])});
6//# sourceMappingURL=react-maskedinput.min.js.map
\No newline at end of file