From c154292d1b639e93247b91b84c5ffc4ae328e588 Mon Sep 17 00:00:00 2001 From: Zhi Cun Date: Mon, 21 Jan 2019 14:42:00 +0800 Subject: [PATCH] input: fix textarea ref (#13803) --- packages/autocomplete/src/autocomplete.vue | 13 ++++++++++--- packages/input/src/input.vue | 14 ++++++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/autocomplete/src/autocomplete.vue b/packages/autocomplete/src/autocomplete.vue index 4fa3e4501..8cc6172b5 100644 --- a/packages/autocomplete/src/autocomplete.vue +++ b/packages/autocomplete/src/autocomplete.vue @@ -149,7 +149,10 @@ }, watch: { suggestionVisible(val) { - this.broadcast('ElAutocompleteSuggestions', 'visible', [val, this.$refs.input.$refs.input.offsetWidth]); + let $input = this.getInput(); + if ($input) { + this.broadcast('ElAutocompleteSuggestions', 'visible', [val, $input.offsetWidth]); + } } }, methods: { @@ -248,7 +251,11 @@ suggestion.scrollTop -= highlightItem.scrollHeight; } this.highlightedIndex = index; - this.$el.querySelector('.el-input__inner').setAttribute('aria-activedescendant', `${this.id}-item-${this.highlightedIndex}`); + let $input = this.getInput(); + $input.setAttribute('aria-activedescendant', `${this.id}-item-${this.highlightedIndex}`); + }, + getInput() { + return this.$refs.input.getInput(); } }, mounted() { @@ -256,7 +263,7 @@ this.$on('item-click', item => { this.select(item); }); - let $input = this.$el.querySelector('.el-input__inner'); + let $input = this.getInput(); $input.setAttribute('role', 'textbox'); $input.setAttribute('aria-autocomplete', 'list'); $input.setAttribute('aria-controls', 'id'); diff --git a/packages/input/src/input.vue b/packages/input/src/input.vue index e7798dab4..0afba042f 100644 --- a/packages/input/src/input.vue +++ b/packages/input/src/input.vue @@ -221,10 +221,10 @@ methods: { focus() { - (this.$refs.input || this.$refs.textarea).focus(); + this.getInput().focus(); }, blur() { - (this.$refs.input || this.$refs.textarea).blur(); + this.getInput().blur(); }, getMigratingConfig() { return { @@ -245,7 +245,7 @@ } }, select() { - (this.$refs.input || this.$refs.textarea).select(); + this.getInput().select(); }, resizeTextarea() { if (this.$isServer) return; @@ -286,7 +286,10 @@ // set input's value, in case parent refuses the change // see: https://github.com/ElemeFE/element/issues/12850 - this.$nextTick(() => { this.$refs.input.value = this.value; }); + this.$nextTick(() => { + let input = this.getInput(); + input.value = this.value; + }); }, handleChange(event) { this.$emit('change', event.target.value); @@ -322,6 +325,9 @@ this.$emit('input', ''); this.$emit('change', ''); this.$emit('clear'); + }, + getInput() { + return this.$refs.input || this.$refs.textarea; } },