tethys/resources/assets/js/components/MyAutocomplete.vue

177 lines
4.6 KiB
Vue
Raw Normal View History

2018-09-14 16:16:02 +00:00
<!-- https://pineco.de/instant-ajax-search-laravel-vue/
https://alligator.io/vuejs/vue-autocomplete-component/ -->
<template>
2018-09-17 16:04:26 +00:00
<div style="position:relative">
2018-11-29 10:58:00 +00:00
<input type="search" @input="searchChanged" v-model="search" v-bind:readonly="isLoading == true" v-bind:title="title" v-bind:placeholder="title"
2018-10-18 14:51:46 +00:00
class="pure-u-23-24" v-on:keydown.down="onArrowDown" v-on:keydown.up="onArrowUp" v-on:keydown.enter="onEnter">
2018-09-14 16:16:02 +00:00
<!-- <ul class="autocomplete-results" v-show="results.length > 0"> -->
2018-09-17 16:04:26 +00:00
<ul class="autocomplete-results pure-u-23-24" v-show="isOpen">
2018-09-14 16:16:02 +00:00
<li class="loading" v-if="isLoading" >Loading results...</li>
<li
v-else
2018-10-18 14:51:46 +00:00
v-for="(suggestion, i) in results"
:key="i"
2018-09-17 16:04:26 +00:00
@click="setResult(suggestion)"
2018-10-18 14:51:46 +00:00
class="autocomplete-result"
:class="{ 'is-active': i === arrowCounter }"
>
2018-09-17 16:04:26 +00:00
<strong>{{ suggestion.full_name }}</strong>
2018-09-14 16:16:02 +00:00
</li>
</ul>
</div>
</template>
<script>
2018-10-18 14:51:46 +00:00
import _ from 'lodash';
import axios from 'axios';
2018-09-14 16:16:02 +00:00
export default {
//data from parent component
props: {
title: String
// items: {
// type: Array,
// required: false,
// default: () => []
// },
// isAsync: {
// type: Boolean,
// required: false,
// default: false
// }
},
data() {
return {
search: null,
results: [],
isOpen: false,
isLoading: false,
isAsync: true,
2018-10-18 14:51:46 +00:00
items: [],
arrowCounter: -1
2018-09-14 16:16:02 +00:00
};
},
// watch: {
// search(after, before) {
// this.isOpen = true;
// this.filterResults();
// }
// },
watch: {
// Once the items content changes, it means the parent component
// provided the needed data
items: function(value, oldValue) {
// we want to make sure we only do this when it's an async request
if (this.isAsync) {
this.results = value;
this.isOpen = true;
2018-11-29 10:58:00 +00:00
this.isLoading = false;
2018-09-14 16:16:02 +00:00
} else {
2018-09-17 16:04:26 +00:00
if (value.length !== oldValue.length) {
this.results = value;
2018-09-14 16:16:02 +00:00
this.isLoading = false;
}
}
}
},
methods: {
setResult(person) {
// this.search = person.full_name;
2018-10-18 14:51:46 +00:00
this.reset();
2018-09-14 16:16:02 +00:00
this.$emit("person", person);
},
reset() {
this.search = "";
2018-09-17 16:04:26 +00:00
this.results = [];
this.isOpen = false;
2018-09-14 16:16:02 +00:00
},
2018-09-17 16:04:26 +00:00
searchChanged() {
2018-09-14 16:16:02 +00:00
// Let's warn the parent that a change was made
this.$emit("input", this.search);
2018-09-17 16:04:26 +00:00
if (this.search.length >= 2) {
// Is the data given by an outside ajax request?
if (this.isAsync) {
this.isLoading = true;
this.filterResults();
} else {
// Data is sync, we can search our flat array
this.results = this.items.filter(item => {
return item.toLowerCase().indexOf(this.search.toLowerCase()) > -1;
});
this.isOpen = true;
}
2018-10-18 14:51:46 +00:00
} else {
2018-09-17 16:04:26 +00:00
this.items = [];
2018-09-14 16:16:02 +00:00
}
},
2018-10-18 14:51:46 +00:00
filterResults: _.debounce(function() {
2018-09-14 16:16:02 +00:00
var self = this;
axios
2018-10-18 14:51:46 +00:00
.get("/api/persons", { params: { filter: this.search.toLowerCase() } })
2018-09-14 16:16:02 +00:00
.then(function(response) {
return (self.items = response.data.data);
})
.catch(function(error) {
alert(error);
});
// this.results = this.items.filter(item => {
// return item.toLowerCase().indexOf(this.search.toLowerCase()) > -1;
// });
2018-10-18 14:51:46 +00:00
}, 300),
onArrowDown() {
if (this.arrowCounter < this.results.length - 1) {
this.arrowCounter = this.arrowCounter + 1;
}
},
onArrowUp() {
if (this.arrowCounter > 0) {
this.arrowCounter = this.arrowCounter - 1;
}
},
onEnter() {
if(Array.isArray(this.results) && this.results.length && this.arrowCounter !== -1 && this.arrowCounter < this.results.length){
//this.search = this.results[this.arrowCounter];
var person = this.results[this.arrowCounter];
this.$emit("person", person);
//this.isOpen = false;
this.reset();
this.arrowCounter = -1;
}
2018-09-14 16:16:02 +00:00
}
2018-09-17 16:04:26 +00:00
},
computed: {
// isOpen() {
// return this.results.length > 0;
// }
2018-09-14 16:16:02 +00:00
}
};
</script>
<style>
.autocomplete-results {
padding: 0;
margin: 0;
border: 1px solid #eeeeee;
height: 120px;
overflow: auto;
}
.autocomplete-result {
list-style: none;
text-align: left;
padding: 4px 2px;
cursor: pointer;
}
2018-10-18 14:51:46 +00:00
.autocomplete-result.is-active,
2018-09-14 16:16:02 +00:00
.autocomplete-result:hover {
background-color: #4aae9b;
color: white;
}
</style>