feat(textarea): 根据内容自适应大小
Signed-off-by: forget skyrim <11513667+forget-skyrim@user.noreply.gitee.com>
This commit is contained in:
parent
38b2941990
commit
e0f8a4167a
@ -18,6 +18,8 @@
|
|||||||
padding: 6px 10px;
|
padding: 6px 10px;
|
||||||
resize: vertical;
|
resize: vertical;
|
||||||
position: relative;
|
position: relative;
|
||||||
|
transition: none;
|
||||||
|
-webkit-transition: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.layui-textarea-wrapper {
|
.layui-textarea-wrapper {
|
||||||
|
@ -6,7 +6,8 @@ export default {
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { LayIcon } from "@layui/icons-vue";
|
import { LayIcon } from "@layui/icons-vue";
|
||||||
import { computed, ref } from "vue";
|
import { computed, ref, watch } from "vue";
|
||||||
|
import {isObject } from "@vueuse/shared"
|
||||||
import "./index.less";
|
import "./index.less";
|
||||||
|
|
||||||
export interface TextareaProps {
|
export interface TextareaProps {
|
||||||
@ -17,6 +18,7 @@ export interface TextareaProps {
|
|||||||
showCount?: boolean;
|
showCount?: boolean;
|
||||||
allowClear?: boolean;
|
allowClear?: boolean;
|
||||||
maxlength?: number;
|
maxlength?: number;
|
||||||
|
autosize?: boolean | { minHeight: number, maxHeight: number };
|
||||||
}
|
}
|
||||||
|
|
||||||
const props = defineProps<TextareaProps>();
|
const props = defineProps<TextareaProps>();
|
||||||
@ -31,6 +33,7 @@ interface TextareaEmits {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const emit = defineEmits<TextareaEmits>();
|
const emit = defineEmits<TextareaEmits>();
|
||||||
|
const textareaRef = ref<HTMLTextAreaElement | null>(null);
|
||||||
const composing = ref(false);
|
const composing = ref(false);
|
||||||
|
|
||||||
const onInput = function (event: Event) {
|
const onInput = function (event: Event) {
|
||||||
@ -78,11 +81,25 @@ const wordCount = computed(() => {
|
|||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
watch([() => props.modelValue, textareaRef], () => {
|
||||||
|
if (!textareaRef.value || !props.autosize) return;
|
||||||
|
const height: number = textareaRef.value?.scrollHeight + 2; // 边框
|
||||||
|
if (isObject(props.autosize)) {
|
||||||
|
const { minHeight, maxHeight } = props.autosize;
|
||||||
|
if (height < minHeight || height > maxHeight) return;
|
||||||
|
}
|
||||||
|
textareaRef.value!.style.height = '1px'
|
||||||
|
textareaRef.value!.style.height = `${textareaRef.value?.scrollHeight + 2}px`
|
||||||
|
}, {
|
||||||
|
immediate: true,
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="layui-textarea-wrapper">
|
<div class="layui-textarea-wrapper">
|
||||||
<textarea
|
<textarea
|
||||||
|
ref="textareaRef"
|
||||||
class="layui-textarea"
|
class="layui-textarea"
|
||||||
:value="modelValue"
|
:value="modelValue"
|
||||||
:placeholder="placeholder"
|
:placeholder="placeholder"
|
||||||
|
Loading…
Reference in New Issue
Block a user