@tarojs/components
Version:
1 lines • 5.13 kB
JavaScript
import{r as t,c as i,h as s,g as e}from"./p-f72fcd8c.js";function o(t){return null!=t?t:""}const h=class{constructor(s){t(this,s),this.onInput=i(this,"input",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.onLineChange=i(this,"linechange",7),this.onKeyDown=i(this,"keydown",7),this.isComposing=!1,this.onInputExecuted=!1,this.handleInput=t=>{if(t.stopPropagation(),this.handleLineChange(),this.onInputExecuted)return void(this.onInputExecuted=!1);const i=t.target.value||"",s=this.getCursorFromTarget(t.target,i.length);this.isComposing?this.compositionValue=i:(this.onInputExecuted=!0,void 0!==this.compositionValue&&(this.compositionValue=void 0),this.value=i,this.onInput.emit({value:i,cursor:s}),this.onInputExecuted=!1)},this.handleComposition=t=>{if(t.stopPropagation(),t.target instanceof HTMLTextAreaElement)if("compositionend"===t.type){this.isComposing=!1;const i=t.target.value||"",s=this.getCursorFromTarget(t.target,i.length);this.compositionValue=void 0,this.handleLineChange(),this.value=i,this.onInput.emit({value:i,cursor:s})}else this.isComposing=!0},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.lastSelectionRange=void 0},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:i}=t.target,s=this.getCursorFromTarget(t.target,i.length),e=t.keyCode||t.code;this.onKeyDown.emit({value:i,cursor:s,keyCode:e}),13===e&&this.onConfirm.emit({value:i})},this.calculateContentHeight=(t,i)=>{const s=t.style.height;let e=t.offsetHeight;const o=t.scrollHeight,h=t.style.overflow,n=t.style.minHeight||null;if(!(e>=o))return o;if(t.style.minHeight=0,t.style.height=e+i+"px",t.style.overflow="hidden",o<t.scrollHeight){for(;t.offsetHeight>=t.scrollHeight;)t.style.height=(e-=i)+"px";for(;t.offsetHeight<t.scrollHeight;)t.style.height=e+++"px";return t.style.height=s,t.style.overflow=h,t.style.minHeight=n,e}},this.getNumberOfLines=()=>{const t=this.textareaRef,i=window.getComputedStyle?window.getComputedStyle(t):t.style,s=parseInt(i.lineHeight,10),e=this.calculateContentHeight(t,s);return Math.floor(e/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,this.compositionValue=void 0}watchAutoFocus(t,i){var s;!i&&t&&(null===(s=this.textareaRef)||void 0===s||s.focus())}watchValue(t){if(this.isComposing)return;const i=o(t);if(this.textareaRef.value!==i){const t="undefined"!=typeof document&&document.activeElement===this.textareaRef?this.lastSelectionRange||this.getSelectionSnapshot(this.textareaRef):null;this.textareaRef.value=i,t&&this.restoreSelection(this.textareaRef,t)}}async focus(){this.textareaRef.focus()}componentDidLoad(){var t,i;null===(t=this.textareaRef)||void 0===t||t.addEventListener("compositionstart",this.handleComposition),null===(i=this.textareaRef)||void 0===i||i.addEventListener("compositionend",this.handleComposition)}disconnectedCallback(){var t,i;null===(t=this.textareaRef)||void 0===t||t.removeEventListener("compositionstart",this.handleComposition),null===(i=this.textareaRef)||void 0===i||i.removeEventListener("compositionend",this.handleComposition)}getSelectionSnapshot(t){if(!t)return null;const{selectionStart:i,selectionEnd:s}=t;return null===i||null===s?null:(this.lastSelectionRange={selectionStart:i,selectionEnd:s},{selectionStart:i,selectionEnd:s})}getCursorFromTarget(t,i){var s;if(!t)return i;const{selectionEnd:e}=t;if("number"==typeof e){const i=null!==(s=t.selectionStart)&&void 0!==s?s:e;return this.lastSelectionRange={selectionStart:i,selectionEnd:e},e}return i}restoreSelection(t,i){if(!t)return;const s=i||this.lastSelectionRange||null;if(!s)return;const e=t.value.length,o=Math.min(s.selectionStart,e),h=Math.min(s.selectionEnd,e);"function"==typeof t.setSelectionRange&&t.setSelectionRange(o,h)}render(){const{value:t,placeholder:i,disabled:e,maxlength:h,autoFocus:n,autoHeight:a,name:r,nativeProps:l,handleInput:u,handleFocus:c,handleBlur:d,handleChange:v,compositionValue:p}=this,f={};return a&&(f.rows=this.line),s("textarea",Object.assign({ref:t=>{t&&(this.textareaRef=t,n&&t&&t.focus())},class:"taro-textarea "+(a?"auto-height":""),value:void 0!==p?p:o(t),placeholder:i,name:r,disabled:e,maxLength:h,autoFocus:n,onInput:u,onFocus:c,onBlur:d,onChange:v,onKeyDown:this.handleKeyDown},l,f))}get el(){return e(this)}static get watchers(){return{autoFocus:["watchAutoFocus"],value:["watchValue"]}}};h.style="taro-textarea-core{width:300px;display:block}taro-textarea-core .auto-height{height:auto}.taro-textarea{width:100%;height:inherit;appearance:none;cursor:auto;border:0;line-height:1.5;display:block;position:relative}.taro-textarea:focus{outline:none}";export{h as T}