Generalized AutoComplete Component code

This commit is contained in:
Andrew Bastin
2019-12-11 17:28:32 -05:00
parent 870521c004
commit f446c9acc1

View File

@@ -3,7 +3,7 @@
<input <input
type="text" type="text"
:placeholder="placeholder" :placeholder="placeholder"
v-model="value" v-model="text"
@input="updateSuggestions" @input="updateSuggestions"
@keyup="updateSuggestions" @keyup="updateSuggestions"
@click="updateSuggestions" @click="updateSuggestions"
@@ -97,18 +97,24 @@ export default {
source: { source: {
type: Array, type: Array,
required: true required: true
},
value: {
type: String,
default: "",
required: false
} }
}, },
watch: { watch: {
value() { text() {
this.$emit("input", this.value); this.$emit("input", this.text);
} }
}, },
data() { data() {
return { return {
value: "application/json", text: this.value,
selectionStart: 0, selectionStart: 0,
suggestionsOffsetLeft: 0, suggestionsOffsetLeft: 0,
currentSuggestionIndex: -1, currentSuggestionIndex: -1,
@@ -134,10 +140,10 @@ export default {
}, },
forceSuggestion(text) { forceSuggestion(text) {
let input = this.value.substring(0, this.selectionStart); let input = this.text.substring(0, this.selectionStart);
this.value = input + text; this.text = input + text;
this.selectionStart = this.value.length; this.selectionStart = this.text.length;
this.suggestionsVisible = true; this.suggestionsVisible = true;
this.currentSuggestionIndex = -1; this.currentSuggestionIndex = -1;
}, },
@@ -166,8 +172,8 @@ export default {
this.currentSuggestionIndex >= 0 ? this.currentSuggestionIndex : 0 this.currentSuggestionIndex >= 0 ? this.currentSuggestionIndex : 0
]; ];
if (activeSuggestion) { if (activeSuggestion) {
let input = this.value.substring(0, this.selectionStart); let input = this.text.substring(0, this.selectionStart);
this.value = input + activeSuggestion; this.text = input + activeSuggestion;
} }
break; break;
@@ -184,7 +190,7 @@ export default {
* @returns {default.props.source|{type, required}} * @returns {default.props.source|{type, required}}
*/ */
suggestions() { suggestions() {
let input = this.value.substring(0, this.selectionStart); let input = this.text.substring(0, this.selectionStart);
return ( return (
this.source this.source