aboutsummaryrefslogtreecommitdiff
path: root/src/components/autosuggest/autosuggest.js
diff options
context:
space:
mode:
authortaehoon <th.dev91@gmail.com>2019-04-02 16:10:03 -0400
committertaehoon <th.dev91@gmail.com>2019-04-14 23:44:49 -0400
commit0bdb0e5a8124508edff12a265174bd51a09c37e6 (patch)
treed495e77866ffaa66bfcca940f789ff27c7b5d544 /src/components/autosuggest/autosuggest.js
parent1cf9780e05957c96b6d6b6b898b83ff1987b2703 (diff)
rename Autosuggest component
Diffstat (limited to 'src/components/autosuggest/autosuggest.js')
-rw-r--r--src/components/autosuggest/autosuggest.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/components/autosuggest/autosuggest.js b/src/components/autosuggest/autosuggest.js
new file mode 100644
index 00000000..d4efe912
--- /dev/null
+++ b/src/components/autosuggest/autosuggest.js
@@ -0,0 +1,52 @@
+const debounceMilliseconds = 500
+
+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
+ },
+ placeholder: {
+ type: String,
+ default: 'Search...'
+ }
+ },
+ data () {
+ return {
+ term: '',
+ timeout: null,
+ results: [],
+ resultsVisible: false
+ }
+ },
+ computed: {
+ filtered () {
+ return this.filter ? this.filter(this.results) : this.results
+ }
+ },
+ watch: {
+ term (val) {
+ this.fetchResults(val)
+ }
+ },
+ methods: {
+ fetchResults (term) {
+ clearTimeout(this.timeout)
+ this.timeout = setTimeout(() => {
+ this.results = []
+ if (term) {
+ this.query(term).then((results) => { this.results = results })
+ }
+ }, debounceMilliseconds)
+ },
+ onInputClick () {
+ this.resultsVisible = true
+ },
+ onClickOutside () {
+ this.resultsVisible = false
+ }
+ }
+}