@react-input/mask
Version:
React input component for masked input.
2 lines (1 loc) • 5.28 kB
JavaScript
import{c as e,d as t,e as a,h as n,i as r,f as i,a as l,u as o,g as s,j as c,_ as u,k as p,l as d,m as h,n as v}from"./helpers-BtaZ0NTN.js";import{Input as m,SyntheticChangeError as f}from"@react-input/core";var k=function(e){return function(){for(var t=arguments.length,a=new Array(t),n=0;n<t;n++)a[n]=arguments[n];return new e("".concat(a.join("\n\n"),"\n"))}};var g,y=["track","modify"];function w(e){var t,a,n,r;return{mask:null!==(t=e.mask)&&void 0!==t?t:"",replacement:"string"==typeof e.replacement?c(e.replacement):null!==(a=e.replacement)&&void 0!==a?a:{},showMask:null!==(n=e.showMask)&&void 0!==n&&n,separate:null!==(r=e.separate)&&void 0!==r&&r,track:e.track,modify:e.modify}}var b=function(g){function b(){var t,a=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return n(this,b),(t=r(this,b,[{init:function(e){var t=e.initialValue,n=e.controlled,r=w(a),i=r.mask,l=r.replacement,o=r.separate,s=r.showMask;return t=n||t?t:s?i:"","production"!==process.env.NODE_ENV&&function(e){var t=e.initialValue,a=e.mask,n=e.replacement;t.length>a.length&&console.error(k(Error)("The initialized value of the `value` or `defaultValue` property is longer than the value specified in the `mask` property. Check the correctness of the initialized value in the specified property.",'Invalid value: "'.concat(t,'".'),"To initialize an unmasked value, use the `format` utility. More details https://github.com/GoncharukOrg/react-input/tree/main/packages/mask#initializing-the-value."));var r=Object.keys(n).filter((function(e){return e.length>1}));r.length>0&&console.error(k(Error)("Object keys in the `replacement` property are longer than one character. Replacement keys must be one character long. Check the correctness of the value in the specified property.","Invalid keys: ".concat(r.join(", "),"."),"To initialize an unmasked value, use the `format` utility. More details https://github.com/GoncharukOrg/react-input/tree/main/packages/mask#initializing-the-value."));for(var i=a.slice(0,t.length),l=-1,o=0;o<i.length;o++){var s=Object.prototype.hasOwnProperty.call(n,i[o]);if(!(i[o]===t[o]||s&&n[i[o]].test(t[o]))){l=o;break}}-1!==l&&console.error(k(Error)("An invalid character was found in the initialized property value `value` or `defaultValue` (index: ".concat(l,"). Check the correctness of the initialized value in the specified property."),'Invalid value: "'.concat(t,'".'),"To initialize an unmasked value, use the `format` utility. More details https://github.com/GoncharukOrg/react-input/tree/main/packages/mask#initializing-the-value."))}({initialValue:t,mask:i,replacement:l}),{value:t,options:{mask:i,replacement:l,separate:o}}},tracking:function(t){var n=t.inputType,r=t.previousValue,i=t.previousOptions,l=t.addedValue,o=t.changeStart,s=t.changeEnd,m=w(a),k=m.track,g=m.modify,b=u(m,y),O=b.mask,j=b.replacement,T=b.showMask,V=b.separate,M=p(p({},"insert"===n?{inputType:n,data:l}:{inputType:n,data:null}),{},{value:r,selectionStart:o,selectionEnd:s}),z=null==k?void 0:k(M);if(!1===z)throw new f("Custom tracking stop.");null===z?l="":!0!==z&&void 0!==z&&(l=z);var C=null==g?void 0:g(M);void 0!==(null==C?void 0:C.mask)&&(O=C.mask),void 0!==(null==C?void 0:C.replacement)&&(j="string"==typeof(null==C?void 0:C.replacement)?c(null==C?void 0:C.replacement):C.replacement),void 0!==(null==C?void 0:C.showMask)&&(T=C.showMask),void 0!==(null==C?void 0:C.separate)&&(V=C.separate);var E=d(r,p({end:o},i)),x=d(r,p({start:s},i)),P=RegExp("[^".concat(Object.keys(j).join(""),"]"),"g"),S=O.replace(P,"");if(E&&(E=h(E,{replacementChars:S,replacement:j,separate:V}),S=S.slice(E.length)),l&&(l=h(l,{replacementChars:S,replacement:j,separate:!1}),S=S.slice(l.length)),"insert"===n&&""===l)throw new f("The character does not match the key value of the `replacement` object.");if(V){var I=O.slice(o,s).replace(P,""),G=I.length-l.length;G<0?x=x.slice(-G):G>0&&(x=I.slice(-G)+x)}x&&(x=h(x,{replacementChars:S,replacement:j,separate:V}));var A=v(E+l+x,{mask:O,replacement:j,separate:V,showMask:T}),N=function(t){var a,n,r,i=t.inputType,l=t.value,o=t.addedValue,s=t.beforeChangeValue,c=t.mask,u=t.replacement,p=t.separate,d=e(l,{mask:c,replacement:u}).filter((function(e){var t=e.type;return"input"===t||p&&"replacement"===t})),h=null===(a=d[s.length+o.length-1])||void 0===a?void 0:a.index,v=null===(n=d[s.length-1])||void 0===n?void 0:n.index,m=null===(r=d[s.length+o.length])||void 0===r?void 0:r.index;if("insert"===i){if(void 0!==h)return h+1;if(void 0!==m)return m;if(void 0!==v)return v+1}if("deleteForward"===i){if(void 0!==m)return m;if(void 0!==v)return v+1}if("deleteBackward"===i){if(void 0!==v)return v+1;if(void 0!==m)return m}var f=l.split("").findIndex((function(e){return Object.prototype.hasOwnProperty.call(u,e)}));return-1!==f?f:l.length}({inputType:n,value:A,addedValue:l,beforeChangeValue:E,mask:O,replacement:j,separate:V});return{value:A,selectionStart:N,selectionEnd:N,options:{mask:O,replacement:j,separate:V}}}}])).format=function(e){return i(e,w(a))},t.formatToParts=function(e){return l(e,w(a))},t.unformat=function(e){return o(e,w(a))},t.generatePattern=function(e){return s(e,w(a))},t}return t(b,m),a(b)}();g=b,Object.defineProperty(g.prototype,Symbol.toStringTag,{writable:!1,enumerable:!1,configurable:!0,value:"Mask"});export{b as default};