UNPKG

@react-input/core

Version:

The core of the packages included in the `@react-input` scope.

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