UNPKG

otp-validator-react

Version:

A customizable OTP input validator for React

3 lines (2 loc) 15.3 kB
import e,{useState as t,useEffect as r}from"react";function n(e){var t={exports:{}};return e(t,t.exports),t.exports}var o="function"==typeof Symbol&&Symbol.for,a=o?Symbol.for("react.element"):60103,i=o?Symbol.for("react.portal"):60106,c=o?Symbol.for("react.fragment"):60107,u=o?Symbol.for("react.strict_mode"):60108,s=o?Symbol.for("react.profiler"):60114,f=o?Symbol.for("react.provider"):60109,l=o?Symbol.for("react.context"):60110,p=o?Symbol.for("react.async_mode"):60111,y=o?Symbol.for("react.concurrent_mode"):60111,d=o?Symbol.for("react.forward_ref"):60112,m=o?Symbol.for("react.suspense"):60113,b=o?Symbol.for("react.suspense_list"):60120,v=o?Symbol.for("react.memo"):60115,g=o?Symbol.for("react.lazy"):60116,h=o?Symbol.for("react.block"):60121,S=o?Symbol.for("react.fundamental"):60117,O=o?Symbol.for("react.responder"):60118,x=o?Symbol.for("react.scope"):60119;function E(e){if("object"==typeof e&&null!==e){var t=e.$$typeof;switch(t){case a:switch(e=e.type){case p:case y:case c:case s:case u:case m:return e;default:switch(e=e&&e.$$typeof){case l:case d:case g:case v:case f:return e;default:return t}}case i:return t}}}function w(e){return E(e)===y}var $={AsyncMode:p,ConcurrentMode:y,ContextConsumer:l,ContextProvider:f,Element:a,ForwardRef:d,Fragment:c,Lazy:g,Memo:v,Portal:i,Profiler:s,StrictMode:u,Suspense:m,isAsyncMode:function(e){return w(e)||E(e)===p},isConcurrentMode:w,isContextConsumer:function(e){return E(e)===l},isContextProvider:function(e){return E(e)===f},isElement:function(e){return"object"==typeof e&&null!==e&&e.$$typeof===a},isForwardRef:function(e){return E(e)===d},isFragment:function(e){return E(e)===c},isLazy:function(e){return E(e)===g},isMemo:function(e){return E(e)===v},isPortal:function(e){return E(e)===i},isProfiler:function(e){return E(e)===s},isStrictMode:function(e){return E(e)===u},isSuspense:function(e){return E(e)===m},isValidElementType:function(e){return"string"==typeof e||"function"==typeof e||e===c||e===y||e===s||e===u||e===m||e===b||"object"==typeof e&&null!==e&&(e.$$typeof===g||e.$$typeof===v||e.$$typeof===f||e.$$typeof===l||e.$$typeof===d||e.$$typeof===S||e.$$typeof===O||e.$$typeof===x||e.$$typeof===h)},typeOf:E},j=n(function(e,t){"production"!==process.env.NODE_ENV&&function(){var e="function"==typeof Symbol&&Symbol.for,r=e?Symbol.for("react.element"):60103,n=e?Symbol.for("react.portal"):60106,o=e?Symbol.for("react.fragment"):60107,a=e?Symbol.for("react.strict_mode"):60108,i=e?Symbol.for("react.profiler"):60114,c=e?Symbol.for("react.provider"):60109,u=e?Symbol.for("react.context"):60110,s=e?Symbol.for("react.async_mode"):60111,f=e?Symbol.for("react.concurrent_mode"):60111,l=e?Symbol.for("react.forward_ref"):60112,p=e?Symbol.for("react.suspense"):60113,y=e?Symbol.for("react.suspense_list"):60120,d=e?Symbol.for("react.memo"):60115,m=e?Symbol.for("react.lazy"):60116,b=e?Symbol.for("react.block"):60121,v=e?Symbol.for("react.fundamental"):60117,g=e?Symbol.for("react.responder"):60118,h=e?Symbol.for("react.scope"):60119;function S(e){if("object"==typeof e&&null!==e){var t=e.$$typeof;switch(t){case r:var y=e.type;switch(y){case s:case f:case o:case i:case a:case p:return y;default:var b=y&&y.$$typeof;switch(b){case u:case l:case m:case d:case c:return b;default:return t}}case n:return t}}}var O=f,x=u,E=c,w=r,$=l,j=o,T=m,P=d,C=n,N=i,I=a,_=p,k=!1;function R(e){return S(e)===f}t.AsyncMode=s,t.ConcurrentMode=O,t.ContextConsumer=x,t.ContextProvider=E,t.Element=w,t.ForwardRef=$,t.Fragment=j,t.Lazy=T,t.Memo=P,t.Portal=C,t.Profiler=N,t.StrictMode=I,t.Suspense=_,t.isAsyncMode=function(e){return k||(k=!0,console.warn("The ReactIs.isAsyncMode() alias has been deprecated, and will be removed in React 17+. Update your code to use ReactIs.isConcurrentMode() instead. It has the exact same API.")),R(e)||S(e)===s},t.isConcurrentMode=R,t.isContextConsumer=function(e){return S(e)===u},t.isContextProvider=function(e){return S(e)===c},t.isElement=function(e){return"object"==typeof e&&null!==e&&e.$$typeof===r},t.isForwardRef=function(e){return S(e)===l},t.isFragment=function(e){return S(e)===o},t.isLazy=function(e){return S(e)===m},t.isMemo=function(e){return S(e)===d},t.isPortal=function(e){return S(e)===n},t.isProfiler=function(e){return S(e)===i},t.isStrictMode=function(e){return S(e)===a},t.isSuspense=function(e){return S(e)===p},t.isValidElementType=function(e){return"string"==typeof e||"function"==typeof e||e===o||e===f||e===i||e===a||e===p||e===y||"object"==typeof e&&null!==e&&(e.$$typeof===m||e.$$typeof===d||e.$$typeof===c||e.$$typeof===u||e.$$typeof===l||e.$$typeof===v||e.$$typeof===g||e.$$typeof===h||e.$$typeof===b)},t.typeOf=S}()}),T=n(function(e){e.exports="production"===process.env.NODE_ENV?$:j}),P=Object.getOwnPropertySymbols,C=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable,I=function(){try{if(!Object.assign)return!1;var e=new String("abc");if(e[5]="de","5"===Object.getOwnPropertyNames(e)[0])return!1;for(var t={},r=0;r<10;r++)t["_"+String.fromCharCode(r)]=r;var n=Object.getOwnPropertyNames(t).map(function(e){return t[e]});if("0123456789"!==n.join(""))return!1;var o={};return"abcdefghijklmnopqrst".split("").forEach(function(e){o[e]=e}),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},o)).join("")}catch(e){return!1}}()?Object.assign:function(e,t){for(var r,n,o=function(e){if(null==e)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(e)}(e),a=1;a<arguments.length;a++){for(var i in r=Object(arguments[a]))C.call(r,i)&&(o[i]=r[i]);if(P){n=P(r);for(var c=0;c<n.length;c++)N.call(r,n[c])&&(o[n[c]]=r[n[c]])}}return o},_=Function.call.bind(Object.prototype.hasOwnProperty),k="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED",R=_,A=function(){};if("production"!==process.env.NODE_ENV){var M=k,V={},D=R;A=function(e){var t="Warning: "+e;"undefined"!=typeof console&&console.error(t);try{throw new Error(t)}catch(e){}}}function F(e,t,r,n,o){if("production"!==process.env.NODE_ENV)for(var a in e)if(D(e,a)){var i;try{if("function"!=typeof e[a]){var c=Error((n||"React class")+": "+r+" type `"+a+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+typeof e[a]+"`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.");throw c.name="Invariant Violation",c}i=e[a](t,a,n,r,null,M)}catch(e){i=e}if(!i||i instanceof Error||A((n||"React class")+": type specification of "+r+" `"+a+"` is invalid; the type checker function must return `null` or an `Error` but returned a "+typeof i+". You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument)."),i instanceof Error&&!(i.message in V)){V[i.message]=!0;var u=o?o():"";A("Failed "+r+" type: "+i.message+(null!=u?u:""))}}}F.resetWarningCache=function(){"production"!==process.env.NODE_ENV&&(V={})};var z=F,q=function(){};function W(){return null}function L(){}function B(){}"production"!==process.env.NODE_ENV&&(q=function(e){var t="Warning: "+e;"undefined"!=typeof console&&console.error(t);try{throw new Error(t)}catch(e){}}),B.resetWarningCache=L;var U=n(function(e){e.exports="production"!==process.env.NODE_ENV?function(e){var t="function"==typeof Symbol&&Symbol.iterator,r="<<anonymous>>",n={array:c("array"),bigint:c("bigint"),bool:c("boolean"),func:c("function"),number:c("number"),object:c("object"),string:c("string"),symbol:c("symbol"),any:i(W),arrayOf:function(e){return i(function(t,r,n,o,i){if("function"!=typeof e)return new a("Property `"+i+"` of component `"+n+"` has invalid PropType notation inside arrayOf.");var c=t[r];if(!Array.isArray(c))return new a("Invalid "+o+" `"+i+"` of type `"+f(c)+"` supplied to `"+n+"`, expected an array.");for(var u=0;u<c.length;u++){var s=e(c,u,n,o,i+"["+u+"]",k);if(s instanceof Error)return s}return null})},element:i(function(t,r,n,o,i){var c=t[r];return e(c)?null:new a("Invalid "+o+" `"+i+"` of type `"+f(c)+"` supplied to `"+n+"`, expected a single ReactElement.")}),elementType:i(function(e,t,r,n,o){var i=e[t];return T.isValidElementType(i)?null:new a("Invalid "+n+" `"+o+"` of type `"+f(i)+"` supplied to `"+r+"`, expected a single ReactElement type.")}),instanceOf:function(e){return i(function(t,n,o,i,c){return t[n]instanceof e?null:new a("Invalid "+i+" `"+c+"` of type `"+((u=t[n]).constructor&&u.constructor.name?u.constructor.name:r)+"` supplied to `"+o+"`, expected instance of `"+(e.name||r)+"`.");var u})},node:i(function(e,t,r,n,o){return s(e[t])?null:new a("Invalid "+n+" `"+o+"` supplied to `"+r+"`, expected a ReactNode.")}),objectOf:function(e){return i(function(t,r,n,o,i){if("function"!=typeof e)return new a("Property `"+i+"` of component `"+n+"` has invalid PropType notation inside objectOf.");var c=t[r],u=f(c);if("object"!==u)return new a("Invalid "+o+" `"+i+"` of type `"+u+"` supplied to `"+n+"`, expected an object.");for(var s in c)if(R(c,s)){var l=e(c,s,n,o,i+"."+s,k);if(l instanceof Error)return l}return null})},oneOf:function(e){return Array.isArray(e)?i(function(t,r,n,i,c){for(var u=t[r],s=0;s<e.length;s++)if(o(u,e[s]))return null;var f=JSON.stringify(e,function(e,t){return"symbol"===l(t)?String(t):t});return new a("Invalid "+i+" `"+c+"` of value `"+String(u)+"` supplied to `"+n+"`, expected one of "+f+".")}):("production"!==process.env.NODE_ENV&&q(arguments.length>1?"Invalid arguments supplied to oneOf, expected an array, got "+arguments.length+" arguments. A common mistake is to write oneOf(x, y, z) instead of oneOf([x, y, z]).":"Invalid argument supplied to oneOf, expected an array."),W)},oneOfType:function(e){if(!Array.isArray(e))return"production"!==process.env.NODE_ENV&&q("Invalid argument supplied to oneOfType, expected an instance of array."),W;for(var t=0;t<e.length;t++){var r=e[t];if("function"!=typeof r)return q("Invalid argument supplied to oneOfType. Expected an array of check functions, but received "+p(r)+" at index "+t+"."),W}return i(function(t,r,n,o,i){for(var c=[],u=0;u<e.length;u++){var s=(0,e[u])(t,r,n,o,i,k);if(null==s)return null;s.data&&R(s.data,"expectedType")&&c.push(s.data.expectedType)}return new a("Invalid "+o+" `"+i+"` supplied to `"+n+"`"+(c.length>0?", expected one of type ["+c.join(", ")+"]":"")+".")})},shape:function(e){return i(function(t,r,n,o,i){var c=t[r],s=f(c);if("object"!==s)return new a("Invalid "+o+" `"+i+"` of type `"+s+"` supplied to `"+n+"`, expected `object`.");for(var p in e){var y=e[p];if("function"!=typeof y)return u(n,o,i,p,l(y));var d=y(c,p,n,o,i+"."+p,k);if(d)return d}return null})},exact:function(e){return i(function(t,r,n,o,i){var c=t[r],s=f(c);if("object"!==s)return new a("Invalid "+o+" `"+i+"` of type `"+s+"` supplied to `"+n+"`, expected `object`.");var p=I({},t[r],e);for(var y in p){var d=e[y];if(R(e,y)&&"function"!=typeof d)return u(n,o,i,y,l(d));if(!d)return new a("Invalid "+o+" `"+i+"` key `"+y+"` supplied to `"+n+"`.\nBad object: "+JSON.stringify(t[r],null," ")+"\nValid keys: "+JSON.stringify(Object.keys(e),null," "));var m=d(c,y,n,o,i+"."+y,k);if(m)return m}return null})}};function o(e,t){return e===t?0!==e||1/e==1/t:e!=e&&t!=t}function a(e,t){this.message=e,this.data=t&&"object"==typeof t?t:{},this.stack=""}function i(e){function t(t,n,o,i,c,u,s){if(i=i||r,u=u||o,s!==k){var f=new Error("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");throw f.name="Invariant Violation",f}return null==n[o]?t?new a(null===n[o]?"The "+c+" `"+u+"` is marked as required in `"+i+"`, but its value is `null`.":"The "+c+" `"+u+"` is marked as required in `"+i+"`, but its value is `undefined`."):null:e(n,o,i,c,u)}process;var n=t.bind(null,!1);return n.isRequired=t.bind(null,!0),n}function c(e){return i(function(t,r,n,o,i,c){var u=t[r];return f(u)!==e?new a("Invalid "+o+" `"+i+"` of type `"+l(u)+"` supplied to `"+n+"`, expected `"+e+"`.",{expectedType:e}):null})}function u(e,t,r,n,o){return new a((e||"React class")+": "+t+" type `"+r+"."+n+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+o+"`.")}function s(r){switch(typeof r){case"number":case"string":case"undefined":return!0;case"boolean":return!r;case"object":if(Array.isArray(r))return r.every(s);if(null===r||e(r))return!0;var n=function(e){var r=e&&(t&&e[t]||e["@@iterator"]);if("function"==typeof r)return r}(r);if(!n)return!1;var o,a=n.call(r);if(n!==r.entries){for(;!(o=a.next()).done;)if(!s(o.value))return!1}else for(;!(o=a.next()).done;){var i=o.value;if(i&&!s(i[1]))return!1}return!0;default:return!1}}function f(e){var t=typeof e;return Array.isArray(e)?"array":e instanceof RegExp?"object":function(e,t){return"symbol"===e||!!t&&("Symbol"===t["@@toStringTag"]||"function"==typeof Symbol&&t instanceof Symbol)}(t,e)?"symbol":t}function l(e){if(null==e)return""+e;var t=f(e);if("object"===t){if(e instanceof Date)return"date";if(e instanceof RegExp)return"regexp"}return t}function p(e){var t=l(e);switch(t){case"array":case"object":return"an "+t;case"boolean":case"date":case"regexp":return"a "+t;default:return t}}return a.prototype=Error.prototype,n.checkPropTypes=z,n.resetWarningCache=z.resetWarningCache,n.PropTypes=n,n}(T.isElement):function(){function e(e,t,r,n,o,a){if(a!==k){var i=new Error("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");throw i.name="Invariant Violation",i}}function t(){return e}e.isRequired=e;var r={array:e,bigint:e,bool:e,func:e,number:e,object:e,string:e,symbol:e,any:e,arrayOf:t,element:e,elementType:e,instanceOf:t,node:e,objectOf:t,oneOf:t,oneOfType:t,shape:t,exact:t,checkPropTypes:B,resetWarningCache:L};return r.PropTypes=r,r}()});const J=({length:n=6,autoSubmit:o=!0,onComplete:a,inputClassName:i="otp-input",containerClassName:c="otp-container",errorClassName:u="otp-error",errorMessage:s="Invalid OTP",isValid:f=!0})=>{const[l,p]=t(Array(n).fill("")),[y,d]=t(0),m=e=>{e.preventDefault();const t=e.clipboardData.getData("text/plain").slice(0,n);if(/^[0-9]+$/.test(t)){const e=[...l];for(let r=0;r<t.length;r++)r<n&&(e[r]=t[r]);p(e),t.length===n&&o&&b(e.join(""))}},b=e=>{a&&a(e)};return r(()=>{const e=document.getElementById(`otp-input-${y}`);e&&e.focus()},[y]),/*#__PURE__*/e.createElement("div",{className:c},/*#__PURE__*/e.createElement("div",{style:{display:"flex",gap:"10px"}},l.map((t,r)=>/*#__PURE__*/e.createElement("input",{key:r,id:`otp-input-${r}`,type:"text",maxLength:"1",value:t,onChange:e=>((e,t)=>{const r=e.target.value;if(/^[0-9]$/.test(r)){const e=[...l];e[t]=r,p(e),t<n-1?d(t+1):o&&b(e.join(""))}else if(""===r){const e=[...l];e[t]="",p(e),t>0&&d(t-1)}})(e,r),onKeyDown:e=>((e,t)=>{"Backspace"===e.key&&!l[t]&&t>0&&d(t-1)})(e,r),onPaste:m,className:i,style:{width:"40px",height:"40px",textAlign:"center",fontSize:"18px",border:"1px solid "+(f?"#ccc":"red")},onFocus:()=>d(r)}))),!f&&/*#__PURE__*/e.createElement("div",{className:u},s))};J.propTypes={length:U.number,autoSubmit:U.bool,onComplete:U.func.isRequired,inputClassName:U.string,containerClassName:U.string,errorClassName:U.string,errorMessage:U.string,isValid:U.bool};export{J as default}; //# sourceMappingURL=index.modern.mjs.map