aboutsummaryrefslogtreecommitdiff
path: root/src/components/autosuggest/autosuggest.js
diff options
context:
space:
mode:
authorshpuld <shp@cock.li>2019-04-22 17:24:35 +0300
committershpuld <shp@cock.li>2019-04-22 17:24:35 +0300
commitd4179454277c10a3e2fda9f62b61a18151693d17 (patch)
tree57adbec33bbe9e30e0463579807c097a18b2d56f /src/components/autosuggest/autosuggest.js
parent75a650aa6de13eaf1e0e17d5ec7bfffeee0db212 (diff)
parentc8f967d5c0424c7dd504ad4afeaefb7c745eed31 (diff)
Merge branch 'develop' into brendenbice1222/pleroma-fe-issues/pleroma-fe-202-show-boosted-users
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
+ }
+ }
+}