From 306a6417d1896b3b04473d094caed7ff804123c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=98=89=E6=B6=B5?= Date: Mon, 21 Aug 2017 21:53:52 +0800 Subject: [PATCH] fix: field textarea height calc wrong --- packages/field/src/field.vue | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/field/src/field.vue b/packages/field/src/field.vue index 1a10aafbf..011fdae8a 100644 --- a/packages/field/src/field.vue +++ b/packages/field/src/field.vue @@ -95,6 +95,14 @@ export default { }; }, + mounted() { + if (this.autosize && this.type === 'textarea') { + const el = this.$refs.textareaElement; + el.style.height = el.scrollHeight + 'px'; + el.style.overflowY = 'hidden'; + } + }, + watch: { value(val) { this.currentValue = val; @@ -102,7 +110,7 @@ export default { currentValue(val) { if (this.autosize && this.type === 'textarea') { - this.$nextTick(() => this.sizeAdjust()); + this.$nextTick(this.sizeAdjust); } this.$emit('input', val); } @@ -125,12 +133,9 @@ export default { }, sizeAdjust() { - const textareaElement = this.$refs.textareaElement; - const textAreaDiff = (parseInt(textareaElement.style.paddingBottom, 10) + - parseInt(textareaElement.style.paddingTop, 10)) || 0; - // 需要先设为0, 才可以让scrollHeight正确计算。 - textareaElement.style.height = 0 + 'px'; - textareaElement.style.height = (textareaElement.scrollHeight - textAreaDiff) + 'px'; + const el = this.$refs.textareaElement; + el.style.height = 'auto'; + el.style.height = el.scrollHeight + 'px'; }, handleInputFocus() {