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