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