UNPKG

@react-input/mask

Version:
2 lines (1 loc) 16.2 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):((e="undefined"!=typeof globalThis?globalThis:e||self).ReactInput=e.ReactInput||{},e.ReactInput.Mask=t())}(this,(function(){"use strict";function e(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function t(e,t,n){return t=o(t),function(e,t){if(t&&("object"==typeof t||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}(e,a()?Reflect.construct(t,n||[],o(e).constructor):t.apply(e,n))}function n(t,n){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=function(t,n){if(t){if("string"==typeof t)return e(t,n);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?e(t,n):void 0}}(t))||n){r&&(t=r);var o=0,a=function(){};return{s:a,n:function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}},e:function(e){throw e},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,l=!0,c=!1;return{s:function(){r=r.call(t)},n:function(){var e=r.next();return l=e.done,e},e:function(e){c=!0,i=e},f:function(){try{l||null==r.return||r.return()}finally{if(c)throw i}}}}function r(e,t,n){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e){return o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},o(e)}function a(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(e){}return(a=function(){return!!e})()}function i(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function l(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?i(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):i(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function c(e,t){return c=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},c(e,t)}function u(e){return u="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},u(e)}function p(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function s(e,t,n){return Object.defineProperty(e,"prototype",{writable:!1}),e}function f(e,t,n){return(t=function(e){var t=function(e,t){if("object"!=u(e)||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,"string");if("object"!=u(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==u(t)?t:t+""}(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function d(e){return d=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},d(e)}function v(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(e){}return(v=function(){return!!e})()}function y(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function b(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?y(Object(n),!0).forEach((function(t){f(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):y(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function m(e,t){return m=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},m(e,t)}function h(e){var t="function"==typeof Map?new Map:void 0;return h=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,n)}function n(){return function(e,t,n){if(v())return Reflect.construct.apply(null,arguments);var r=[null];r.push.apply(r,t);var o=new(e.bind.apply(e,r));return n&&m(o,n.prototype),o}(e,arguments,d(this).constructor)}return n.prototype=Object.create(e.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),m(n,e)},h(e)}var g,O=function(e){function t(e){var n;return p(this,t),(n=function(e,t,n){return t=d(t),function(e,t){if(t&&("object"==u(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}(e,v()?Reflect.construct(t,n||[],d(e).constructor):t.apply(e,n))}(this,t,[e])).name="SyntheticChangeError",n}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&m(e,t)}(t,e),s(t)}(h(Error)),w=["options"],j=["text","email","tel","search","url"],k=s((function e(t){var n=t.init,r=t.tracking;p(this,e);var o=new WeakMap;this.register=function(e){var t;if(j.includes(e.type)){var a=null!==(t=e._wrapperState)&&void 0!==t?t:{},i=a.initialValue,l=void 0===i?"":i,c=a.controlled,u=void 0!==c&&c,p=n({initialValue:e.value||l,controlled:u}),s=p.value,f=p.options,d={value:s,options:f,fallbackOptions:f},v={id:-1,cachedId:-1},y={value:"",selectionStart:0,selectionEnd:0},m=Object.getOwnPropertyDescriptor("_valueTracker"in e?e:HTMLInputElement.prototype,"value");Object.defineProperty(e,"value",b(b({},m),{},{set:function(t){var n;y.value=t,null==m||null===(n=m.set)||void 0===n||n.call(e,t)}})),e.value=s;var h=function(){var t=function(){var n,r;y.selectionStart=null!==(n=e.selectionStart)&&void 0!==n?n:0,y.selectionEnd=null!==(r=e.selectionEnd)&&void 0!==r?r:0,v.id=window.setTimeout(t)};v.id=window.setTimeout(t)},g=function(){window.clearTimeout(v.id),v.id=-1,v.cachedId=-1},k=function(t){try{var n,o;if(v.cachedId===v.id)throw new O("The input selection has not been updated.");v.cachedId=v.id;var a=e.value,i=e.selectionStart,l=e.selectionEnd;if(null===i||null===l)throw new O("The selection attributes have not been initialized.");var c,u=y.value;if(void 0===t.inputType&&(y.selectionStart=0,y.selectionEnd=u.length),i>y.selectionStart?c="insert":i<=y.selectionStart&&i<y.selectionEnd?c="deleteBackward":i===y.selectionEnd&&a.length<u.length&&(c="deleteForward"),void 0===c||("deleteBackward"===c||"deleteForward"===c)&&a.length>u.length)throw new O("Input type detection error.");var p="",s=y.selectionStart,f=y.selectionEnd;if("insert"===c)p=a.slice(y.selectionStart,i);else{var b=u.length-a.length;s=i,f=i+b}d.value!==u?d.options=d.fallbackOptions:d.fallbackOptions=d.options;var m=d.options,h=r({inputType:c,previousValue:u,previousOptions:m,value:a,addedValue:p,changeStart:s,changeEnd:f,selectionStart:i,selectionEnd:l}),g=h.options,j=function(e,t){if(null==e)return{};var n,r,o=function(e,t){if(null==e)return{};var n={};for(var r in e)if({}.hasOwnProperty.call(e,r)){if(t.includes(r))continue;n[r]=e[r]}return n}(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)n=a[r],t.includes(n)||{}.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}(h,w);e.value=j.value,e.setSelectionRange(j.selectionStart,j.selectionEnd),d.value=j.value,d.options=g,y.selectionStart=j.selectionStart,y.selectionEnd=j.selectionEnd,null===(n=e._valueTracker)||void 0===n||null===(o=n.setValue)||void 0===o||o.call(n,u)}catch(n){if(e.value=y.value,e.setSelectionRange(y.selectionStart,y.selectionEnd),t.preventDefault(),t.stopPropagation(),"SyntheticChangeError"!==n.name)throw n}};document.activeElement===e&&h(),e.addEventListener("focus",h),e.addEventListener("blur",g),e.addEventListener("input",k),o.set(e,{onFocus:h,onBlur:g,onInput:k})}},this.unregister=function(e){var t=o.get(e);void 0!==t&&(e.removeEventListener("focus",t.onFocus),e.removeEventListener("blur",t.onBlur),e.removeEventListener("input",t.onInput),o.delete(e))}}));function P(e,t){var r,o=t.replacementChars,a=t.replacement,i=t.separate,l=o,c="",u=n(e);try{for(u.s();!(r=u.n()).done;){var p,s=r.value,f=!Object.prototype.hasOwnProperty.call(a,s)&&(null===(p=a[l[0]])||void 0===p?void 0:p.test(s));(i&&s===l[0]||f)&&(l=l.slice(1),c+=s)}}catch(e){u.e(e)}finally{u.f()}return c}function S(e,t){var r,o=t.mask,a=t.replacement,i=t.separate,l=t.showMask,c=0,u="",p=n(o);try{for(p.s();!(r=p.n()).done;){var s=r.value;if(!l&&void 0===e[c])break;Object.prototype.hasOwnProperty.call(a,s)&&void 0!==e[c]?u+=e[c++]:u+=s}}catch(e){p.e(e)}finally{p.f()}if(i&&!l){for(var f=o.length-1;f>=0&&u[f]===o[f];f--);u=u.slice(0,f+1)}return u}function E(e,t){for(var n=t.mask,r=t.replacement,o=[],a=0;a<n.length;a++){var i,l=null!==(i=e[a])&&void 0!==i?i:n[a],c=Object.prototype.hasOwnProperty.call(r,l)?"replacement":void 0!==e[a]&&e[a]!==n[a]?"input":"mask";o.push({type:c,value:l,index:a})}return o}function T(e){return e.length>0?r({},e,/./):{}}function I(e,t){for(var n=t.start,r=void 0===n?0:n,o=t.end,a=t.mask,i=t.replacement,l=t.separate,c=e.slice(r,o),u=a.slice(r,o),p="",s=0;s<u.length;s++){var f=Object.prototype.hasOwnProperty.call(i,u[s]);f&&void 0!==c[s]&&c[s]!==u[s]?p+=c[s]:f&&l&&(p+=u[s])}return p}function R(e,t){var n=t.mask,r=t.replacement,o="string"==typeof r?T(r):r,a=RegExp("[^".concat(Object.keys(o).join(""),"]"),"g");return S(P(e,{replacementChars:n.replace(a,""),replacement:o,separate:!1}),{mask:n,replacement:o,separate:!1,showMask:!1})}function M(e,t){var n=t.mask,r=t.replacement,o="string"==typeof r?T(r):r,a=I(e,{mask:n,replacement:o,separate:!1}),i=RegExp("[^".concat(Object.keys(o).join(""),"]"),"g");return P(a,{replacementChars:n.replace(i,""),replacement:o,separate:!1})}function _(e,t){var n=t.mask,r=t.replacement,o="string"==typeof r?T(r):r;return E(R(e,{mask:n,replacement:o}),{mask:n,replacement:o})}g=k,Object.defineProperty(g.prototype,Symbol.toStringTag,{writable:!1,enumerable:!1,configurable:!0,value:"Input"});var x=["[","]","\\","/","^","$",".","|","?","*","+","(",")","{","}"];function C(e){return x.includes(e)?"\\".concat(e):e}function D(e,t){for(var n=t.mask,r=t.replacement,o="string"==typeof r?T(r):r,a="partial"===e||"partial-inexact"===e,i="full"===e||"partial"===e,l="",c=0;c<n.length;c++){var u=n[c];0===c&&(l="^"),a&&(l+="("),l+=Object.prototype.hasOwnProperty.call(o,u)?"".concat(i?"(?!".concat(C(u),")"):"","(").concat(o[u].source,")"):C(u),c===n.length-1&&(a&&(l+=")?".repeat(n.length)),l+="$")}return l}var V,B=["track","modify"];function L(e){var t,n,r,o;return{mask:null!==(t=e.mask)&&void 0!==t?t:"",replacement:"string"==typeof e.replacement?T(e.replacement):null!==(n=e.replacement)&&void 0!==n?n:{},showMask:null!==(r=e.showMask)&&void 0!==r&&r,separate:null!==(o=e.separate)&&void 0!==o&&o,track:e.track,modify:e.modify}}var A=function(e){function n(){var e,r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,n),e=t(this,n,[{init:function(e){var t=e.initialValue,n=e.controlled,o=L(r),a=o.mask,i=o.replacement,l=o.separate,c=o.showMask;return{value:t=n||t?t:c?a:"",options:{mask:a,replacement:i,separate:l}}},tracking:function(e){var t=e.inputType,n=e.previousValue,o=e.previousOptions,a=e.addedValue,i=e.changeStart,c=e.changeEnd,u=L(r),p=u.track,s=u.modify,f=function(e,t){if(null==e)return{};var n,r,o=function(e,t){if(null==e)return{};var n={};for(var r in e)if({}.hasOwnProperty.call(e,r)){if(t.includes(r))continue;n[r]=e[r]}return n}(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)n=a[r],t.includes(n)||{}.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}(u,B),d=f.mask,v=f.replacement,y=f.showMask,b=f.separate,m=l(l({},"insert"===t?{inputType:t,data:a}:{inputType:t,data:null}),{},{value:n,selectionStart:i,selectionEnd:c}),h=null==p?void 0:p(m);if(!1===h)throw new O("Custom tracking stop.");null===h?a="":!0!==h&&void 0!==h&&(a=h);var g=null==s?void 0:s(m);void 0!==(null==g?void 0:g.mask)&&(d=g.mask),void 0!==(null==g?void 0:g.replacement)&&(v="string"==typeof(null==g?void 0:g.replacement)?T(null==g?void 0:g.replacement):g.replacement),void 0!==(null==g?void 0:g.showMask)&&(y=g.showMask),void 0!==(null==g?void 0:g.separate)&&(b=g.separate);var w=I(n,l({end:i},o)),j=I(n,l({start:c},o)),k=RegExp("[^".concat(Object.keys(v).join(""),"]"),"g"),R=d.replace(k,"");if(w&&(w=P(w,{replacementChars:R,replacement:v,separate:b}),R=R.slice(w.length)),a&&(a=P(a,{replacementChars:R,replacement:v,separate:!1}),R=R.slice(a.length)),"insert"===t&&""===a)throw new O("The character does not match the key value of the `replacement` object.");if(b){var M=d.slice(i,c).replace(k,""),_=M.length-a.length;_<0?j=j.slice(-_):_>0&&(j=M.slice(-_)+j)}j&&(j=P(j,{replacementChars:R,replacement:v,separate:b}));var x=S(w+a+j,{mask:d,replacement:v,separate:b,showMask:y}),C=function(e){var t,n,r,o=e.inputType,a=e.value,i=e.addedValue,l=e.beforeChangeValue,c=e.mask,u=e.replacement,p=e.separate,s=E(a,{mask:c,replacement:u}).filter((function(e){var t=e.type;return"input"===t||p&&"replacement"===t})),f=null===(t=s[l.length+i.length-1])||void 0===t?void 0:t.index,d=null===(n=s[l.length-1])||void 0===n?void 0:n.index,v=null===(r=s[l.length+i.length])||void 0===r?void 0:r.index;if("insert"===o){if(void 0!==f)return f+1;if(void 0!==v)return v;if(void 0!==d)return d+1}if("deleteForward"===o){if(void 0!==v)return v;if(void 0!==d)return d+1}if("deleteBackward"===o){if(void 0!==d)return d+1;if(void 0!==v)return v}var y=a.split("").findIndex((function(e){return Object.prototype.hasOwnProperty.call(u,e)}));return-1!==y?y:a.length}({inputType:t,value:x,addedValue:a,beforeChangeValue:w,mask:d,replacement:v,separate:b});return{value:x,selectionStart:C,selectionEnd:C,options:{mask:d,replacement:v,separate:b}}}}]),e.format=function(e){return R(e,L(r))},e.formatToParts=function(e){return _(e,L(r))},e.unformat=function(e){return M(e,L(r))},e.generatePattern=function(e){return D(e,L(r))},e}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&c(e,t)}(n,e),function(e,t,n){return Object.defineProperty(e,"prototype",{writable:!1}),e}(n)}(k);V=A,Object.defineProperty(V.prototype,Symbol.toStringTag,{writable:!1,enumerable:!1,configurable:!0,value:"Mask"});var F,$="undefined"!=typeof globalThis?globalThis:global||self;return $.ReactInput=null!==(F=$.ReactInput)&&void 0!==F?F:{},$.ReactInput.Mask=A,$.ReactInput.Mask.format=R,$.ReactInput.Mask.formatToParts=_,$.ReactInput.Mask.unformat=M,$.ReactInput.Mask.generatePattern=D,A}));