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