UNPKG

@tarojs/components

Version:
1 lines 4.14 kB
import{r as registerInstance,c as createEvent,h,g as getElement}from"./index-5bd7cbab.js";var indexCss="taro-input-core{display:block}input{display:block;overflow:hidden;height:1.4rem;text-overflow:clip;text-align:inherit;white-space:nowrap}";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.password=false;this.disabled=false;this.maxlength=140;this.autoFocus=false;this.confirmType="done";this.nativeProps={};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 r=e.target.value;var u=getTrueType(i,a,s);t.onInputExcuted=true;if(u==="number"&&r&&o<=r.length){r=r.substring(0,o);e.target.value=r}t.value=r;t.onInput.emit({value:r,cursor:r.length})}};this.handlePaste=function(e){t.isOnPaste=true;t.onPaste.emit({value:e.target.value})};this.handleFocus=function(e){t.onInputExcuted=false;t.onFocus.emit({value:e.target.value})};this.handleBlur=function(e){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){var n=e.target.value;var i=e.keyCode||e.code;t.onInputExcuted=false;e.stopPropagation();t.onKeyDown.emit({value:n,cursor:n.length,keyCode:i});i===13&&t.onConfirm.emit({value:n})};this.handleComposition=function(e){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}}}e.prototype.watchFocus=function(e,t){var n;if(!t&&e){(n=this.inputRef)===null||n===void 0?void 0:n.focus()}};e.prototype.componentDidLoad=function(){var e=this;var t,n,i;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)}Object.defineProperty(this.el,"value",{get:function(){var t;return(t=e.inputRef)===null||t===void 0?void 0:t.value},set:function(t){return e.value=t},configurable:true})};e.prototype.disconnectedCallback=function(){var e;if(this.type==="file"){(e=this.inputRef)===null||e===void 0?void 0:e.removeEventListener("change",this.fileListener)}};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,r=t.disabled,u=t.maxlength,l=t.confirmType,p=t.name,c=t.nativeProps;return h("input",Object.assign({ref:function(t){e.inputRef=t},class:"weui-input",value:fixControlledValue(n),type:getTrueType(i,l,o),placeholder:a,autoFocus:s,disabled:r,maxlength:u,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},c))};Object.defineProperty(e.prototype,"el",{get:function(){return getElement(this)},enumerable:false,configurable:true});Object.defineProperty(e,"watchers",{get:function(){return{autoFocus:["watchFocus"]}},enumerable:false,configurable:true});return e}();Input.style=indexCss;export{Input as taro_input_core};