@tarojs/components
Version:
1 lines • 3.04 kB
JavaScript
import{r as t,c as e,h as s,g as i}from"./p-dd7a128d.js";function h(t){return null!=t?t:""}const a=class{constructor(s){t(this,s),this.onInput=e(this,"input",7),this.onFocus=e(this,"focus",7),this.onBlur=e(this,"blur",7),this.onConfirm=e(this,"confirm",7),this.onChange=e(this,"change",7),this.onLineChange=e(this,"linechange",7),this.onKeyDown=e(this,"keydown",7),this.handleInput=t=>{t.stopPropagation(),this.handleLineChange();const e=t.target.value||"";this.value=e,this.onInput.emit({value:e,cursor:e.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:e}=t.target,s=t.keyCode||t.code;this.onKeyDown.emit({value:e,cursor:e.length,keyCode:s}),13===s&&this.onConfirm.emit({value:e})},this.calculateContentHeight=(t,e)=>{let s=t.style.height,i=t.offsetHeight,h=t.scrollHeight,a=t.style.overflow,o=t.style.minHeight||null;if(!(i>=h))return h;if(t.style.minHeight=0,t.style.height=i+e+"px",t.style.overflow="hidden",h<t.scrollHeight){for(;t.offsetHeight>=t.scrollHeight;)t.style.height=(i-=e)+"px";for(;t.offsetHeight<t.scrollHeight;)t.style.height=i+++"px";return t.style.height=s,t.style.overflow=a,t.style.minHeight=o,i}},this.getNumberOfLines=()=>{const t=this.textareaRef,e=window.getComputedStyle?window.getComputedStyle(t):t.style,s=parseInt(e.lineHeight,10),i=this.calculateContentHeight(t,s);return Math.floor(i/s)},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,e){var s;!e&&t&&(null===(s=this.textareaRef)||void 0===s||s.focus())}watchValue(t){const e=h(t);this.textareaRef.value!==e&&(this.textareaRef.value=e)}async focus(){this.textareaRef.focus()}render(){const{value:t,placeholder:e,disabled:i,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),s("textarea",Object.assign({ref:t=>{t&&(this.textareaRef=t,o&&t&&t.focus())},class:"taro-textarea "+(n?"auto-height":""),value:h(t),placeholder:e,name:r,disabled:i,maxlength:a,autofocus:o,onInput:c,onFocus:l,onBlur:d,onChange:p,onKeyDown:this.handleKeyDown},u,g))}get el(){return i(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;-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}";export{a as taro_textarea_core}