UNPKG

@tarojs/components

Version:
1 lines 2.99 kB
import{r as t,c as s,h as i,g as h}from"./p-d0cabaaa.js";function e(t){return null!=t?t:""}const a=class{constructor(i){t(this,i),this.onInput=s(this,"input",7),this.onFocus=s(this,"focus",7),this.onBlur=s(this,"blur",7),this.onConfirm=s(this,"confirm",7),this.onChange=s(this,"change",7),this.onLineChange=s(this,"linechange",7),this.onKeyDown=s(this,"keydown",7),this.handleInput=t=>{t.stopPropagation(),this.handleLineChange();const s=t.target.value||"";this.value=s,this.onInput.emit({value:s,cursor:s.length})},this.handleFocus=t=>{t.stopPropagation(),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.handleLineChange=()=>{const t=this.getNumberOfLines();t!==this.line&&(this.line=t,this.onLineChange.emit({height:this.textareaRef.clientHeight,lineCount:this.line}))},this.handleKeyDown=t=>{t.stopPropagation();const{value:s}=t.target,i=t.keyCode||t.code;this.onKeyDown.emit({value:s,cursor:s.length,keyCode:i}),13===i&&this.onConfirm.emit({value:s})},this.calculateContentHeight=(t,s)=>{let i=t.style.height,h=t.offsetHeight,e=t.scrollHeight,a=t.style.overflow,o=t.style.minHeight||null;if(!(h>=e))return e;if(t.style.minHeight=0,t.style.height=h+s+"px",t.style.overflow="hidden",e<t.scrollHeight){for(;t.offsetHeight>=t.scrollHeight;)t.style.height=(h-=s)+"px";for(;t.offsetHeight<t.scrollHeight;)t.style.height=h+++"px";return t.style.height=i,t.style.overflow=a,t.style.minHeight=o,h}},this.getNumberOfLines=()=>{const t=this.textareaRef,s=window.getComputedStyle?window.getComputedStyle(t):t.style,i=parseInt(s.lineHeight,10),h=this.calculateContentHeight(t,i);return Math.floor(h/i)},this.value="",this.placeholder=void 0,this.disabled=!1,this.maxlength=140,this.autoFocus=!1,this.autoHeight=!1,this.name=void 0,this.nativeProps={},this.line=1}watchAutoFocus(t,s){var i;!s&&t&&(null===(i=this.textareaRef)||void 0===i||i.focus())}watchValue(t){const s=e(t);this.textareaRef.value!==s&&(this.textareaRef.value=s)}async focus(){this.textareaRef.focus()}render(){const{value:t,placeholder:s,disabled:h,maxlength:a,autoFocus:o,autoHeight:n,name:r,nativeProps:u,handleInput:c,handleFocus:l,handleBlur:d,handleChange:p}=this,g={};return n&&(g.rows=this.line),i("textarea",Object.assign({ref:t=>{t&&(this.textareaRef=t,o&&t&&t.focus())},class:"taro-textarea "+(n?"auto-height":""),value:e(t),placeholder:s,name:r,disabled:h,maxlength:a,autofocus:o,onInput:c,onFocus:l,onBlur:d,onChange:p,onKeyDown:this.handleKeyDown},u,g))}get el(){return h(this)}static get watchers(){return{autoFocus:["watchAutoFocus"],value:["watchValue"]}}};a.style="taro-textarea-core{width:300px;display:block}taro-textarea-core .auto-height{height:auto}.taro-textarea{height:inherit;appearance:none;cursor:auto;border:0;width:100%;line-height:1.5;display:block;position:relative}.taro-textarea:focus{outline:none}";export{a as taro_textarea_core}