@tarojs/components
Version:
1 lines • 6.84 kB
JavaScript
var __awaiter=this&&this.__awaiter||function(e,t,n,i){function o(e){return e instanceof n?e:new n((function(t){t(e)}))}return new(n||(n=Promise))((function(n,a){function r(e){try{u(i.next(e))}catch(e){a(e)}}function s(e){try{u(i["throw"](e))}catch(e){a(e)}}function u(e){e.done?n(e.value):o(e.value).then(r,s)}u((i=i.apply(e,t||[])).next())}))};var __generator=this&&this.__generator||function(e,t){var n={label:0,sent:function(){if(a[0]&1)throw a[1];return a[1]},trys:[],ops:[]},i,o,a,r;return r={next:s(0),throw:s(1),return:s(2)},typeof Symbol==="function"&&(r[Symbol.iterator]=function(){return this}),r;function s(e){return function(t){return u([e,t])}}function u(s){if(i)throw new TypeError("Generator is already executing.");while(r&&(r=0,s[0]&&(n=0)),n)try{if(i=1,o&&(a=s[0]&2?o["return"]:s[0]?o["throw"]||((a=o["return"])&&a.call(o),0):o.next)&&!(a=a.call(o,s[1])).done)return a;if(o=0,a)s=[s[0]&2,a.value];switch(s[0]){case 0:case 1:a=s;break;case 4:n.label++;return{value:s[1],done:false};case 5:n.label++;o=s[1];s=[0];continue;case 7:s=n.ops.pop();n.trys.pop();continue;default:if(!(a=n.trys,a=a.length>0&&a[a.length-1])&&(s[0]===6||s[0]===2)){n=0;continue}if(s[0]===3&&(!a||s[1]>a[0]&&s[1]<a[3])){n.label=s[1];break}if(s[0]===6&&n.label<a[1]){n.label=a[1];a=s;break}if(a&&n.label<a[2]){n.label=a[2];n.ops.push(s);break}if(a[2])n.ops.pop();n.trys.pop();continue}s=t.call(e,n)}catch(e){s=[6,e];o=0}finally{i=a=0}if(s[0]&5)throw s[1];return{value:s[0]?s[1]:void 0,done:true}}};import{r as registerInstance,c as createEvent,h,g as getElement}from"./index-980f930f.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.handleInput=function(e){e.stopPropagation();var n=t,i=n.type,o=n.maxlength,a=n.confirmType,r=n.password;if(!t.isOnComposition&&!t.onInputExcuted){var s=e.target.value;var u=getTrueType(i,a,r);t.onInputExcuted=true;if(u==="number"&&s&&o>-1&&o<=s.length){s=s.substring(0,o);e.target.value=s}t.value=s;t.onInput.emit({value:s,cursor:s.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,r=t.autoFocus,s=t.disabled,u=t.maxlength,l=t.confirmType,p=t.name,f=t.nativeProps;return h("input",Object.assign({ref:function(t){e.inputRef=t;if(r&&t)t.focus()},class:"weui-input",type:getTrueType(i,l,o),placeholder:a,autoFocus:r,disabled:s,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},f,{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};