pleroma-fe/src/components/user_autosuggest/user_autosuggest.js

54 lines
1 KiB
JavaScript
Raw Normal View History

import BlockCard from '../block_card/block_card.vue'
2019-04-02 21:12:31 +11:00
const debounceMilliseconds = 500
2019-04-02 21:12:31 +11: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 21:12:31 +11:00
components: {
BlockCard
2019-04-02 21:12:31 +11:00
},
data () {
return {
term: '',
2019-04-03 04:18:36 +11:00
timeout: null,
results: [],
2019-04-03 04:18:36 +11:00
resultsVisible: false
}
},
computed: {
filtered () {
return this.filter ? this.filter(this.results) : this.results
}
},
watch: {
term (val) {
this.fetchResults(val)
2019-04-02 21:12:31 +11:00
}
},
methods: {
fetchResults (term) {
2019-04-02 21:12:31 +11:00
clearTimeout(this.timeout)
this.timeout = setTimeout(() => {
this.results = []
if (term) {
this.query(term).then((results) => { this.results = results })
}
}, debounceMilliseconds)
2019-04-03 04:18:36 +11:00
},
onInputClick () {
this.resultsVisible = true
},
onClickOutside () {
this.resultsVisible = false
2019-04-02 21:12:31 +11:00
}
}
}