|
|
@@ -1,4 +1,4 @@
|
|
|
-onMounted, nextTick, , watch, ref<template>
|
|
|
+<template>
|
|
|
<view :class="classes">
|
|
|
<view v-if="readonly" class="nut-textarea__textarea">
|
|
|
{{ modelValue }}
|
|
|
@@ -25,7 +25,7 @@ onMounted, nextTick, , watch, ref<template>
|
|
|
<script lang="ts">
|
|
|
import { computed, nextTick, onMounted, ref, watch } from 'vue';
|
|
|
import { createComponent } from '@/packages/utils/create';
|
|
|
-
|
|
|
+import Taro, { eventCenter, getCurrentInstance as getCurrentInstanceTaro } from '@tarojs/taro';
|
|
|
const { componentName, create, translate } = createComponent('textarea');
|
|
|
|
|
|
export default create({
|
|
|
@@ -63,7 +63,7 @@ export default create({
|
|
|
default: false
|
|
|
},
|
|
|
autosize: {
|
|
|
- type: Boolean,
|
|
|
+ type: [Boolean, Object],
|
|
|
default: false
|
|
|
},
|
|
|
autofocus: {
|
|
|
@@ -85,7 +85,8 @@ export default create({
|
|
|
|
|
|
const styles: any = computed(() => {
|
|
|
return {
|
|
|
- textAlign: props.textAlign
|
|
|
+ textAlign: props.textAlign,
|
|
|
+ height: props.autosize ? heightSet.value : 'null'
|
|
|
// resize: props.autosize ? 'vertical' : 'none'
|
|
|
};
|
|
|
});
|
|
|
@@ -120,12 +121,15 @@ export default create({
|
|
|
emit('blur', event);
|
|
|
};
|
|
|
|
|
|
- const textareaRef = ref();
|
|
|
-
|
|
|
+ const textareaRef = ref<any>(null);
|
|
|
+ const textareaHeight = ref();
|
|
|
+ const heightSet = ref('auto');
|
|
|
const getContentHeight = () => {
|
|
|
- let textarea = textareaRef.value;
|
|
|
- textarea.style.height = 'auto';
|
|
|
- let height = textarea.scrollHeight;
|
|
|
+ heightSet.value = 'auto';
|
|
|
+ let height = textareaHeight.value;
|
|
|
+ // let textarea = textareaRef.value;
|
|
|
+ // textarea.style.height = 'auto';
|
|
|
+ // let height = textarea.scrollHeight;
|
|
|
if (typeof props.autosize === 'object') {
|
|
|
const { maxHeight, minHeight } = props.autosize;
|
|
|
if (maxHeight !== undefined) {
|
|
|
@@ -136,7 +140,8 @@ export default create({
|
|
|
}
|
|
|
}
|
|
|
if (height) {
|
|
|
- textarea.style.height = height + 'px';
|
|
|
+ // textarea.style.height = height + 'px';
|
|
|
+ heightSet.value = height + 'px';
|
|
|
}
|
|
|
};
|
|
|
watch(
|
|
|
@@ -148,9 +153,27 @@ export default create({
|
|
|
}
|
|
|
);
|
|
|
|
|
|
+ const getRefHeight = () => {
|
|
|
+ const query = Taro.createSelectorQuery();
|
|
|
+ // const query = Taro.createSelectorQuery();
|
|
|
+ query.selectAll('.nut-textarea__textarea').boundingClientRect();
|
|
|
+ let uid = textareaRef.value ? textareaRef.value.uid : '0';
|
|
|
+ query.exec((res) => {
|
|
|
+ if (res[0] && textareaRef.value) {
|
|
|
+ let _item: any = Array.from(res[0]).filter((item: any) => item.id == uid);
|
|
|
+ textareaHeight.value = _item[0]['height'] || 30;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
onMounted(() => {
|
|
|
if (props.autosize) {
|
|
|
- nextTick(getContentHeight);
|
|
|
+ eventCenter.once((getCurrentInstanceTaro() as any).router.onReady, () => {
|
|
|
+ getRefHeight();
|
|
|
+ });
|
|
|
+ setTimeout(() => {
|
|
|
+ nextTick(getContentHeight);
|
|
|
+ }, 300);
|
|
|
}
|
|
|
});
|
|
|
|