From 88b7a741d9a171ff473a9660b194d3b63ba8e162 Mon Sep 17 00:00:00 2001 From: chenjiajian <770230504@qq.com> Date: Fri, 14 Apr 2023 11:53:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96textarea=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/form/textarea.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/form/textarea.js b/src/form/textarea.js index 91fc30d..82fcbbb 100644 --- a/src/form/textarea.js +++ b/src/form/textarea.js @@ -18,6 +18,8 @@ class TextArea extends Component { readOnly: false, disabled: false, autosize: false, + maxheight: null, + minheight: null, maxlength: null, minlength: null, 'show-limit': false, @@ -157,11 +159,24 @@ class TextArea extends Component { onInput(e) { this.value = e.target.value if (this.autosize) { - nextTick(() => { - this.$refs.textarea.style.height = 'auto' - let height = this.$refs.textarea.scrollHeight + 'px' - this.$refs.textarea.style.height = height - }) + nextTick(() => this.adjustTextareaSize()) + } + } + adjustTextareaSize() { + let { maxheight, minheight } = this + const input = this.$refs.textarea + input.style.height = 'auto' + let height = input.scrollHeight + + if (minheight && !Number.isNaN(+minheight)) { + height = Math.max(minheight, height) + } + if (maxheight && !Number.isNaN(+maxheight)) { + input.style.overflow = height > maxheight ? 'auto' : 'hidden' + height = Math.min(maxheight, height) + } + if (input.style.height !== height) { + input.style.height = height + 'px' } } onKeydown(ev) { @@ -223,6 +238,10 @@ class TextArea extends Component { // 火狐浏览器需要手动focus()才能聚焦成功 nextTick(_ => this.$refs.textarea.focus()) } + if (this.autosize) { + this.minheight = this.minheight || this.offsetHeight - 2 + this.adjustTextareaSize() + } } }