yandere_fe/src/components/user_autosuggest/user_autosuggest.js

54 lines
1.0 KiB
JavaScript
Raw Normal View History

import BlockCard from '../block_card/block_card.vue'
2019-04-02 03:12:31 -07:00
const debounceMilliseconds = 500
2019-04-02 03:12:31 -07:00
export default {
props: {
query: { // function to query results and return a promise
type: Function,
required: true
},
filter: { // function to filter results in real time
type: Function
}
},
2019-04-02 03:12:31 -07:00
components: {
BlockCard
2019-04-02 03:12:31 -07:00
},
data () {
return {
term: '',
2019-04-02 10:18:36 -07:00
timeout: null,
results: [],
2019-04-02 10:18:36 -07:00
resultsVisible: false
}
},
computed: {
filtered () {
return this.filter ? this.filter(this.results) : this.results
}
},
watch: {
term (val) {
this.fetchResults(val)
2019-04-02 03:12:31 -07:00
}
},
methods: {
fetchResults (term) {
2019-04-02 03:12:31 -07:00
clearTimeout(this.timeout)
this.timeout = setTimeout(() => {
this.results = []
if (term) {
this.query(term).then((results) => { this.results = results })
}
}, 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
}
}
}