2019-04-02 16:31:45 +00:00
|
|
|
const debounceMilliseconds = 500
|
|
|
|
|
2019-04-02 10:12:31 +00:00
|
|
|
export default {
|
2019-04-02 19:41:26 +00:00
|
|
|
props: {
|
2019-07-05 07:02:14 +00:00
|
|
|
query: { // function to query results and return a promise
|
2019-04-02 19:41:26 +00:00
|
|
|
type: Function,
|
|
|
|
required: true
|
|
|
|
},
|
2019-07-05 07:02:14 +00:00
|
|
|
filter: { // function to filter results in real time
|
2019-04-02 19:41:26 +00:00
|
|
|
type: Function
|
2019-04-02 19:43:41 +00:00
|
|
|
},
|
|
|
|
placeholder: {
|
|
|
|
type: String,
|
|
|
|
default: 'Search...'
|
2019-04-02 19:41:26 +00:00
|
|
|
}
|
|
|
|
},
|
2019-04-02 10:12:31 +00:00
|
|
|
data () {
|
|
|
|
return {
|
2019-04-02 19:41:26 +00:00
|
|
|
term: '',
|
2019-04-02 17:18:36 +00:00
|
|
|
timeout: null,
|
2019-04-02 19:41:26 +00:00
|
|
|
results: [],
|
2019-04-02 17:18:36 +00:00
|
|
|
resultsVisible: false
|
2019-04-02 16:31:45 +00:00
|
|
|
}
|
|
|
|
},
|
2019-04-02 18:27:22 +00:00
|
|
|
computed: {
|
|
|
|
filtered () {
|
2019-04-02 19:41:26 +00:00
|
|
|
return this.filter ? this.filter(this.results) : this.results
|
2019-04-02 18:27:22 +00:00
|
|
|
}
|
|
|
|
},
|
2019-04-02 16:31:45 +00:00
|
|
|
watch: {
|
2019-04-02 19:41:26 +00:00
|
|
|
term (val) {
|
2019-04-02 16:31:45 +00:00
|
|
|
this.fetchResults(val)
|
2019-04-02 10:12:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2019-04-02 19:41:26 +00:00
|
|
|
fetchResults (term) {
|
2019-04-02 10:12:31 +00:00
|
|
|
clearTimeout(this.timeout)
|
|
|
|
this.timeout = setTimeout(() => {
|
2019-04-02 16:31:45 +00:00
|
|
|
this.results = []
|
2019-04-02 19:41:26 +00:00
|
|
|
if (term) {
|
|
|
|
this.query(term).then((results) => { this.results = results })
|
2019-04-02 16:31:45 +00:00
|
|
|
}
|
|
|
|
}, debounceMilliseconds)
|
2019-04-02 17:18:36 +00:00
|
|
|
},
|
|
|
|
onInputClick () {
|
|
|
|
this.resultsVisible = true
|
|
|
|
},
|
|
|
|
onClickOutside () {
|
|
|
|
this.resultsVisible = false
|
2019-04-02 10:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|