@react-input/core
Version:
The core of the packages included in the `@react-input` scope.
2 lines (1 loc) • 3.12 kB
JavaScript
import{a as e,b as t,c as n,d as o}from"./helpers-C8k3UfPS.js";import i from"./SyntheticChangeError.js";var l,a=["options"],r=["text","email","tel","search","url"],s=e((function e(l){var s=l.init,c=l.tracking;t(this,e);var u=new WeakMap;this.register=function(e){var t;if(r.includes(e.type)){var l=null!==(t=e._wrapperState)&&void 0!==t?t:{},d=l.initialValue,v=void 0===d?"":d,p=l.controlled,h=void 0!==p&&p,f=s({initialValue:e.value||v,controlled:h}),E=f.value,g=f.options,w={value:E,options:g,fallbackOptions:g},S={id:-1,cachedId:-1},m={value:"",selectionStart:0,selectionEnd:0},b=Object.getOwnPropertyDescriptor("_valueTracker"in e?e:HTMLInputElement.prototype,"value");Object.defineProperty(e,"value",n(n({},b),{},{set:function(t){var n;m.value=t,null==b||null===(n=b.set)||void 0===n||n.call(e,t)}})),e.value=E;var y=function(){var t=function(){var n,o;m.selectionStart=null!==(n=e.selectionStart)&&void 0!==n?n:0,m.selectionEnd=null!==(o=e.selectionEnd)&&void 0!==o?o:0,S.id=window.setTimeout(t)};S.id=window.setTimeout(t)},T=function(){window.clearTimeout(S.id),S.id=-1,S.cachedId=-1},k=function(t){try{var n,l;if(S.cachedId===S.id)throw new i("The input selection has not been updated.");S.cachedId=S.id;var r=e.value,s=e.selectionStart,u=e.selectionEnd;if(null===s||null===u)throw new i("The selection attributes have not been initialized.");var d,v=m.value;if(void 0===t.inputType&&(m.selectionStart=0,m.selectionEnd=v.length),s>m.selectionStart?d="insert":s<=m.selectionStart&&s<m.selectionEnd?d="deleteBackward":s===m.selectionEnd&&r.length<v.length&&(d="deleteForward"),void 0===d||("deleteBackward"===d||"deleteForward"===d)&&r.length>v.length)throw new i("Input type detection error.");var p="",h=m.selectionStart,f=m.selectionEnd;if("insert"===d)p=r.slice(m.selectionStart,s);else{var E=v.length-r.length;h=s,f=s+E}w.value!==v?w.options=w.fallbackOptions:w.fallbackOptions=w.options;var g=w.options,b=c({inputType:d,previousValue:v,previousOptions:g,value:r,addedValue:p,changeStart:h,changeEnd:f,selectionStart:s,selectionEnd:u}),y=b.options,T=o(b,a);e.value=T.value,e.setSelectionRange(T.selectionStart,T.selectionEnd),w.value=T.value,w.options=y,m.selectionStart=T.selectionStart,m.selectionEnd=T.selectionEnd,null===(n=e._valueTracker)||void 0===n||null===(l=n.setValue)||void 0===l||l.call(n,v)}catch(n){if(e.value=m.value,e.setSelectionRange(m.selectionStart,m.selectionEnd),t.preventDefault(),t.stopPropagation(),"SyntheticChangeError"!==n.name)throw n}};document.activeElement===e&&y(),e.addEventListener("focus",y),e.addEventListener("blur",T),e.addEventListener("input",k),u.set(e,{onFocus:y,onBlur:T,onInput:k})}else"production"!==process.env.NODE_ENV&&console.warn("Warn: The input element type does not match one of the types: ".concat(r.join(", "),"."))},this.unregister=function(e){var t=u.get(e);void 0!==t&&(e.removeEventListener("focus",t.onFocus),e.removeEventListener("blur",t.onBlur),e.removeEventListener("input",t.onInput),u.delete(e))}}));l=s,Object.defineProperty(l.prototype,Symbol.toStringTag,{writable:!1,enumerable:!1,configurable:!0,value:"Input"});export{s as default};