Can someone review this code snippet?
This is a peculiar example of a custom autocomplete VUE component.
If you enter a value in one of the fields in Section 1 (like 'Apple'), then click on the Next button, you'll notice that the same value appears incorrectly in some of the fields in Section 2 (still 'Apple'). However, if you click on Display vals, it will correctly display all values from both sections.
Many thanks in advance!
HTML:
<div id="app">
<div v-if="section == 1">
<p>Section {{section}}</p>
<br>
<div>
<label>Test1</label>
<autocomplete v-model="test1"
:items="theItems">
</autocomplete>
</div>
<br>
<div>
<label>Test2</label>
<autocomplete v-model="test2"
:items="theItems">
</autocomplete>
</div>
</div>
<div v-if="section == 2">
<p>Section {{section}}</p>
<br>
<div>
<label>Test3</label>
<autocomplete v-model="test3"
:items="theItems">
</autocomplete>
</div>
<br>
<div>
<label>Test4</label>
<autocomplete v-model="test4"
:items="theItems">
</autocomplete>
</div>
</div>
<br>
<button v-if="section == 2" type="button" v-on:click="section=1">Prev</button>
<button type="button" v-on:click="displayVals()">Display vals</button>
<button v-if="section == 1" type="button" v-on:click="section=2">Next</button>
</div>
<script type="text/x-template" id="autocomplete">
<div class="autocomplete">
<input type="text" @input="onChange" v-model="search" @keyup.down="onArrowDown" @keyup.up="onArrowUp" @keyup.enter="onEnter" />
<ul id="autocomplete-results" v-show="isOpen" class="autocomplete-results">
<li class="loading" v-if="isLoading">
Loading results...
</li>
<li v-else v-for="(result, i) in results" :key="i" @click="setResult(result)" class="autocomplete-result" :class="{ 'is-active': i === arrowCounter }">
{{ result }}
</li>
</ul>
</div>
</script>
VUE:
const Autocomplete = {
name: "autocomplete",
template: "#autocomplete",
props: {
items: {
type: Array,
required: false,
default: () => []
},
isAsync: {
type: Boolean,
required: false,
default: false
}
},
data() {
return {
isOpen: false,
results: [],
search: "",
isLoading: false,
arrowCounter: 0
};
},
methods: {
onChange() {
// Notify parent of change
this.$emit("input", this.search);
// Handle async vs. local data
if (this.isAsync) {
this.isLoading = true;
} else {
// Search local array
this.filterResults();
this.isOpen = true;
}
},
filterResults() {
// Filter results based on input
this.results = this.items.filter(item => {
return item.toLowerCase().indexOf(this.search.toLowerCase()) > -1;
});
},
setResult(result) {
this.search = result;
this.$emit("input", this.search);
this.isOpen = false;
},
onArrowDown(evt) {
if (this.arrowCounter < this.results.length) {
this.arrowCounter = this.arrowCounter + 1;
}
},
onArrowUp() {
if (this.arrowCounter > 0) {
this.arrowCounter = this.arrowCounter - 1;
}
},
onEnter() {
this.search = this.results[this.arrowCounter];
this.isOpen = false;
this.arrowCounter = -1;
},
handleClickOutside(evt) {
if (!this.$el.contains(evt.target)) {
this.isOpen = false;
this.arrowCounter = -1;
}
}
},
watch: {
items: function(val, oldValue) {
// Check for changes in items
if (val.length !== oldValue.length) {
this.results = val;
this.isLoading = false;
}
}
},
mounted() {
document.addEventListener("click", this.handleClickOutside);
},
destroyed() {
document.removeEventListener("click", this.handleClickOutside);
}
};
new Vue({
el: "#app",
name: "app",
components: {
autocomplete: Autocomplete
},
methods: {
displayVals() {
alert("test1=" + this.test1 + ", test2=" + this.test2 + ", test3=" + this.test3 + ", test4=" + this.test4);
},
},
data: {
test1: '',
test2: '',
test3: '',
test4: '',
section: 1,
theItems: [ 'Apple', 'Banana', 'Orange', 'Mango', 'Pear', 'Peach', 'Grape', 'Tangerine', 'Pineapple']
}
});
CSS:
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
margin-top: 60px;
}
.autocomplete {
position: relative;
width: 130px;
}
.autocomplete-results {
padding: 0;
margin: 0;
border: 1px solid #eeeeee;
height: 120px;
overflow: auto;
width: 100%;
}
.autocomplete-result {
list-style: none;
text-align: left;
padding: 4px 2px;
cursor: pointer;
}
.autocomplete-result.is-active,
.autocomplete-result:hover {
background-color: #4aae9b;
color: white;
}
Thank you once more!