UNPKG

@tarojs/components

Version:
1 lines 5.57 kB
import{__awaiter,__generator}from"tslib";import{r as registerInstance,c as createEvent,h,g as getElement}from"./index-5e431bb8.js";var indexCss=".weui-input{-webkit-appearance:none;font-size:inherit;color:inherit;background-color:transparent;border:0;outline:0;width:100%;height:1.47059em;line-height:1.47059}.weui-input::-webkit-outer-spin-button,.weui-input::-webkit-inner-spin-button{-webkit-appearance:none;margin:0}taro-input-core{display:block}";function getTrueType(e,t,n){if(t==="search")e="search";if(n)e="password";if(typeof e==="undefined"){return"text"}if(!e){throw new Error("unexpected type")}if(e==="digit")e="number";return e}function fixControlledValue(e){return e!==null&&e!==void 0?e:""}var Input=function(){function e(e){var t=this;registerInstance(this,e);this.onInput=createEvent(this,"input",7);this.onPaste=createEvent(this,"paste",7);this.onFocus=createEvent(this,"focus",7);this.onBlur=createEvent(this,"blur",7);this.onConfirm=createEvent(this,"confirm",7);this.onChange=createEvent(this,"change",7);this.onKeyDown=createEvent(this,"keydown",7);this.isOnComposition=false;this.isOnPaste=false;this.onInputExcuted=false;this.handleInput=function(e){e.stopPropagation();var n=t,i=n.type,o=n.maxlength,a=n.confirmType,s=n.password;if(!t.isOnComposition&&!t.onInputExcuted){var u=e.target.value;var r=getTrueType(i,a,s);t.onInputExcuted=true;if(r==="number"&&u&&o>-1&&o<=u.length){u=u.substring(0,o);e.target.value=u}t.value=u;t.onInput.emit({value:u,cursor:u.length});t.onInputExcuted=false}};this.handlePaste=function(e){e.stopPropagation();t.isOnPaste=true;t.onPaste.emit({value:e.target.value})};this.handleFocus=function(e){e.stopPropagation();t.onInputExcuted=false;t.onFocus.emit({value:e.target.value})};this.handleBlur=function(e){e.stopPropagation();t.onBlur.emit({value:e.target.value})};this.handleChange=function(e){e.stopPropagation();t.onChange.emit({value:e.target.value});if(t.isOnPaste){t.isOnPaste=false;t.value=e.target.value;t.onInput.emit({value:e.target.value,cursor:e.target.value.length})}};this.handleKeyDown=function(e){e.stopPropagation();var n=e.target.value;var i=e.keyCode||e.code;t.onInputExcuted=false;t.onKeyDown.emit({value:n,cursor:n.length,keyCode:i});i===13&&t.onConfirm.emit({value:n})};this.handleComposition=function(e){e.stopPropagation();if(!(e.target instanceof HTMLInputElement))return;if(e.type==="compositionend"){t.isOnComposition=false;t.value=e.target.value;t.onInput.emit({value:e.target.value,cursor:e.target.value.length})}else{t.isOnComposition=true}};this.handleBeforeInput=function(e){if(!e.data)return;var n=e.data&&/[0-9]/.test(e.data);if(t.type==="number"&&!n){e.preventDefault()}if(t.type==="digit"&&!n){if(e.data!=="."||e.data==="."&&e.target.value.indexOf(".")>-1){e.preventDefault()}}};this.value="";this.type=undefined;this.password=false;this.placeholder=undefined;this.disabled=false;this.maxlength=140;this.autoFocus=false;this.confirmType="done";this.name=undefined;this.nativeProps={}}e.prototype.focus=function(){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(e){this.inputRef.focus();return[2]}))}))};e.prototype.watchAutoFocus=function(e,t){var n;if(!t&&e){(n=this.inputRef)===null||n===void 0?void 0:n.focus()}};e.prototype.watchValue=function(e){var t=fixControlledValue(e);if(this.inputRef.value!==t){this.inputRef.value=t}};e.prototype.componentDidLoad=function(){var e=this;var t,n,i,o,a;if(this.type==="file"){this.fileListener=function(){e.onInput.emit()};(t=this.inputRef)===null||t===void 0?void 0:t.addEventListener("change",this.fileListener)}else{(n=this.inputRef)===null||n===void 0?void 0:n.addEventListener("compositionstart",this.handleComposition);(i=this.inputRef)===null||i===void 0?void 0:i.addEventListener("compositionend",this.handleComposition);(o=this.inputRef)===null||o===void 0?void 0:o.addEventListener("beforeinput",this.handleBeforeInput);(a=this.inputRef)===null||a===void 0?void 0:a.addEventListener("textInput",this.handleBeforeInput)}};e.prototype.disconnectedCallback=function(){var e,t,n,i,o;if(this.type==="file"){(e=this.inputRef)===null||e===void 0?void 0:e.removeEventListener("change",this.fileListener)}else{(t=this.inputRef)===null||t===void 0?void 0:t.removeEventListener("compositionstart",this.handleComposition);(n=this.inputRef)===null||n===void 0?void 0:n.removeEventListener("compositionend",this.handleComposition);(i=this.inputRef)===null||i===void 0?void 0:i.removeEventListener("beforeinput",this.handleBeforeInput);(o=this.inputRef)===null||o===void 0?void 0:o.removeEventListener("textInput",this.handleBeforeInput)}};e.prototype.render=function(){var e=this;var t=this,n=t.value,i=t.type,o=t.password,a=t.placeholder,s=t.autoFocus,u=t.disabled,r=t.maxlength,l=t.confirmType,p=t.name,d=t.nativeProps;return h("input",Object.assign({ref:function(t){e.inputRef=t;if(s&&t)t.focus()},class:"weui-input",type:getTrueType(i,l,o),placeholder:a,autoFocus:s,disabled:u,maxlength:r,name:p,onInput:this.handleInput,onFocus:this.handleFocus,onBlur:this.handleBlur,onChange:this.handleChange,onKeyDown:this.handleKeyDown,onPaste:this.handlePaste,onCompositionStart:this.handleComposition,onCompositionEnd:this.handleComposition},d,{value:fixControlledValue(n)}))};Object.defineProperty(e.prototype,"el",{get:function(){return getElement(this)},enumerable:false,configurable:true});Object.defineProperty(e,"watchers",{get:function(){return{autoFocus:["watchAutoFocus"],value:["watchValue"]}},enumerable:false,configurable:true});return e}();Input.style=indexCss;export{Input as taro_input_core};