UNPKG

@tarojs/components

Version:
1 lines 3.76 kB
import{__awaiter,__generator}from"tslib";import{r as registerInstance,c as createEvent,h,g as getElement}from"./index-ab3c86da.js";var indexCss="taro-textarea-core{width:300px;display:block}taro-textarea-core .auto-height{height:auto}.taro-textarea{height:inherit;-webkit-appearance:none;-moz-appearance:none;appearance:none;cursor:auto;border:0;width:100%;line-height:1.5;display:block;position:relative}.taro-textarea:focus{outline:none}";function fixControlledValue(e){return e!==null&&e!==void 0?e:""}var Textarea=function(){function e(e){var t=this;registerInstance(this,e);this.onInput=createEvent(this,"input",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.onLineChange=createEvent(this,"linechange",7);this.onKeyDown=createEvent(this,"keydown",7);this.handleInput=function(e){e.stopPropagation();t.handleLineChange();var n=e.target.value||"";t.value=n;t.onInput.emit({value:n,cursor:n.length})};this.handleFocus=function(e){e.stopPropagation();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})};this.handleLineChange=function(){var e=t.getNumberOfLines();if(e!==t.line){t.line=e;t.onLineChange.emit({height:t.textareaRef.clientHeight,lineCount:t.line})}};this.handleKeyDown=function(e){e.stopPropagation();var n=e.target.value;var a=e.keyCode||e.code;t.onKeyDown.emit({value:n,cursor:n.length,keyCode:a});a===13&&t.onConfirm.emit({value:n})};this.calculateContentHeight=function(e,t){var n=e.style.height,a=e.offsetHeight,i=e.scrollHeight,o=e.style.overflow,r=e.style.minHeight||null;if(a>=i){e.style.minHeight=0;e.style.height=a+t+"px";e.style.overflow="hidden";if(i<e.scrollHeight){while(e.offsetHeight>=e.scrollHeight){e.style.height=(a-=t)+"px"}while(e.offsetHeight<e.scrollHeight){e.style.height=a+++"px"}e.style.height=n;e.style.overflow=o;e.style.minHeight=r;return a}}else{return i}};this.getNumberOfLines=function(){var e=t.textareaRef,n=window.getComputedStyle?window.getComputedStyle(e):e.style,a=parseInt(n.lineHeight,10),i=t.calculateContentHeight(e,a),o=Math.floor(i/a);return o};this.value="";this.placeholder=undefined;this.disabled=false;this.maxlength=140;this.autoFocus=false;this.autoHeight=false;this.name=undefined;this.nativeProps={};this.line=1}e.prototype.watchAutoFocus=function(e,t){var n;if(!t&&e){(n=this.textareaRef)===null||n===void 0?void 0:n.focus()}};e.prototype.watchValue=function(e){var t=fixControlledValue(e);if(this.textareaRef.value!==t){this.textareaRef.value=t}};e.prototype.focus=function(){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(e){this.textareaRef.focus();return[2]}))}))};e.prototype.render=function(){var e=this;var t=this,n=t.value,a=t.placeholder,i=t.disabled,o=t.maxlength,r=t.autoFocus,l=t.autoHeight,s=t.name,u=t.nativeProps,c=t.handleInput,f=t.handleFocus,g=t.handleBlur,d=t.handleChange;var p={};if(l){p.rows=this.line}return h("textarea",Object.assign({ref:function(t){if(t){e.textareaRef=t;if(r&&t)t.focus()}},class:"taro-textarea ".concat(l?"auto-height":""),value:fixControlledValue(n),placeholder:a,name:s,disabled:i,maxlength:o,autofocus:r,onInput:c,onFocus:f,onBlur:g,onChange:d,onKeyDown:this.handleKeyDown},u,p))};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}();Textarea.style=indexCss;export{Textarea as taro_textarea_core};