UNPKG

@tarojs/components

Version:
1 lines 4.65 kB
import{r as t,c as i,h as s,g as h}from"./p-d0cabaaa.js";function e(t,i,s){if("search"===i&&(t="search"),s&&(t="password"),void 0===t)return"text";if(!t)throw new Error("unexpected type");return"digit"===t&&(t="number"),t}function n(t){return null!=t?t:""}const o=class{constructor(s){t(this,s),this.onInput=i(this,"input",7),this.onPaste=i(this,"paste",7),this.onFocus=i(this,"focus",7),this.onBlur=i(this,"blur",7),this.onConfirm=i(this,"confirm",7),this.onChange=i(this,"change",7),this.onKeyDown=i(this,"keydown",7),this.isOnComposition=!1,this.isOnPaste=!1,this.onInputExcuted=!1,this.handleInput=t=>{t.stopPropagation();const{type:i,maxlength:s,confirmType:h,password:n}=this;if(!this.isOnComposition&&!this.onInputExcuted){let o=t.target.value;const u=e(i,h,n);this.onInputExcuted=!0,"number"===u&&o&&s>-1&&s<=o.length&&(o=o.substring(0,s),t.target.value=o),this.value=o,this.onInput.emit({value:o,cursor:o.length}),this.onInputExcuted=!1}},this.handlePaste=t=>{t.stopPropagation(),this.isOnPaste=!0,this.onPaste.emit({value:t.target.value})},this.handleFocus=t=>{t.stopPropagation(),this.onInputExcuted=!1,this.onFocus.emit({value:t.target.value})},this.handleBlur=t=>{t.stopPropagation(),this.onBlur.emit({value:t.target.value})},this.handleChange=t=>{t.stopPropagation(),this.onChange.emit({value:t.target.value}),this.isOnPaste&&(this.isOnPaste=!1,this.value=t.target.value,this.onInput.emit({value:t.target.value,cursor:t.target.value.length}))},this.handleKeyDown=t=>{t.stopPropagation();const{value:i}=t.target,s=t.keyCode||t.code;this.onInputExcuted=!1,this.onKeyDown.emit({value:i,cursor:i.length,keyCode:s}),13===s&&this.onConfirm.emit({value:i})},this.handleComposition=t=>{t.stopPropagation(),t.target instanceof HTMLInputElement&&("compositionend"===t.type?(this.isOnComposition=!1,this.value=t.target.value,this.onInput.emit({value:t.target.value,cursor:t.target.value.length})):this.isOnComposition=!0)},this.handleBeforeInput=t=>{if(!t.data)return;const i=t.data&&/[0-9]/.test(t.data);"number"!==this.type||i||t.preventDefault(),"digit"!==this.type||i||("."!==t.data||"."===t.data&&t.target.value.indexOf(".")>-1)&&t.preventDefault()},this.value="",this.type=void 0,this.password=!1,this.placeholder=void 0,this.disabled=!1,this.maxlength=140,this.autoFocus=!1,this.confirmType="done",this.name=void 0,this.nativeProps={}}async focus(){this.inputRef.focus()}watchAutoFocus(t,i){var s;!i&&t&&(null===(s=this.inputRef)||void 0===s||s.focus())}watchValue(t){const i=n(t);this.inputRef.value!==i&&(this.inputRef.value=i)}componentDidLoad(){var t,i,s,h,e;"file"===this.type?(this.fileListener=()=>{this.onInput.emit()},null===(t=this.inputRef)||void 0===t||t.addEventListener("change",this.fileListener)):(null===(i=this.inputRef)||void 0===i||i.addEventListener("compositionstart",this.handleComposition),null===(s=this.inputRef)||void 0===s||s.addEventListener("compositionend",this.handleComposition),null===(h=this.inputRef)||void 0===h||h.addEventListener("beforeinput",this.handleBeforeInput),null===(e=this.inputRef)||void 0===e||e.addEventListener("textInput",this.handleBeforeInput))}disconnectedCallback(){var t,i,s,h,e;"file"===this.type?null===(t=this.inputRef)||void 0===t||t.removeEventListener("change",this.fileListener):(null===(i=this.inputRef)||void 0===i||i.removeEventListener("compositionstart",this.handleComposition),null===(s=this.inputRef)||void 0===s||s.removeEventListener("compositionend",this.handleComposition),null===(h=this.inputRef)||void 0===h||h.removeEventListener("beforeinput",this.handleBeforeInput),null===(e=this.inputRef)||void 0===e||e.removeEventListener("textInput",this.handleBeforeInput))}render(){const{value:t,type:i,password:h,placeholder:o,autoFocus:u,disabled:a,maxlength:r,confirmType:l,name:c,nativeProps:p}=this;return s("input",Object.assign({ref:t=>{this.inputRef=t,u&&t&&t.focus()},class:"weui-input",type:e(i,l,h),placeholder:o,autoFocus:u,disabled:a,maxlength:r,name:c,onInput:this.handleInput,onFocus:this.handleFocus,onBlur:this.handleBlur,onChange:this.handleChange,onKeyDown:this.handleKeyDown,onPaste:this.handlePaste,onCompositionStart:this.handleComposition,onCompositionEnd:this.handleComposition},p,{value:n(t)}))}get el(){return h(this)}static get watchers(){return{autoFocus:["watchAutoFocus"],value:["watchValue"]}}};o.style=".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}";export{o as taro_input_core}