forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocomplete.vue
More file actions
154 lines (136 loc) · 5.11 KB
/
autocomplete.vue
File metadata and controls
154 lines (136 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<template>
<div>
<div class="layout-padding" style="max-width: 600px;">
<p class="caption" style="margin-bottom: 40px">
These examples feature countries autocomplete.<br>
On desktop, Escape key closes the suggestions popover and you can navigate with keyboard arrow keys. Selection is made with either mouse/finger tap or by Enter key.
</p>
<q-search @change="onChange" @input="onInput" v-model="terms" placeholder="Start typing a country name">
<q-autocomplete @search="search" @selected="selected" />
</q-search>
<q-search @change="onChange" @input="onInput" v-model="terms" hide-underline placeholder="Start typing a country name (hide underline)">
<q-autocomplete @search="search" @selected="selected" />
</q-search>
<q-search @change="val => { terms = val; onChange(val) }" @input="onInput" :value="terms" placeholder="Start typing a country name (onChange)">
<q-autocomplete @search="search" @selected="selected" />
</q-search>
<p class="caption">Number selected: {{ JSON.stringify(termsN) }}</p>
<q-search type="number" v-model="termsN" placeholder="Start typing a number">
<q-autocomplete :static-data="{field: 'value', list: numbers}" @selected="selected" />
</q-search>
<q-input @change="onChange" @input="onInput" v-model="terms" placeholder="Start typing a country name">
<q-autocomplete @search="search" @selected="selected" />
</q-input>
<q-input type="textarea" @change="onChange" @input="onInput" v-model="terms" placeholder="Start typing a country name" stack-label="Autocomplete in textarea">
<q-autocomplete @search="search" @selected="selected" />
</q-input>
<q-input @change="val => { terms = val; onChange(val) }" @input="onInput" :value="terms" placeholder="Start typing a country name (onChange)">
<q-autocomplete @search="search" @selected="selected" />
</q-input>
<q-search inverted v-model="terms" placeholder="Start typing a country name">
<q-autocomplete @search="search" @selected="selected" />
</q-search>
<p class="caption">Maximum of 2 results at a time</p>
<q-search inverted color="amber" v-model="terms">
<q-autocomplete
@search="search"
:max-results="2"
@selected="selected"
/>
</q-search>
<q-search inverted color="white" :dark="false" v-model="terms">
<q-autocomplete
@search="search"
:max-results="2"
@selected="selected"
/>
</q-search>
<p class="caption">Minimum 3 characters to trigger search</p>
<q-input color="amber" v-model="terms" placeholder="Type 'fre'">
<q-autocomplete
@search="search"
:min-characters="3"
@selected="selected"
/>
</q-input>
<p class="caption">Custom debounce before triggering search</p>
<q-input color="amber" v-model="terms" placeholder="One second debounce">
<q-autocomplete
@search="search"
:debounce="1000"
@selected="selected"
/>
</q-input>
<p class="caption">Static List</p>
<q-search inverted color="secondary" v-model="terms" placeholder="Featuring static data">
<q-autocomplete
:static-data="{field: 'value', list: countries}"
@selected="selected"
/>
</q-search>
<p class="caption">Separator between results</p>
<q-search v-model="terms">
<q-autocomplete
separator
@search="search"
@selected="selected"
/>
</q-search>
</div>
</div>
</template>
<script>
import { uid, filter } from 'quasar'
import countries from 'data/autocomplete.json'
const icons = ['alarm', 'email', 'search', 'build', 'card_giftcard', 'perm_identity', 'receipt', 'schedule', 'speaker_phone', 'archive', 'weekend', 'battery_charging_full']
function getRandomIcon () {
return icons[Math.floor(Math.random() * icons.length)]
}
function getRandomStamp () {
if (Math.floor(Math.random() * 50) % 3 === 0) {
return `${Math.floor(Math.random() * 10)} min`
}
}
function getRandomSecondLabel () {
if (Math.floor(Math.random() * 50) % 4 === 0) {
return `UID: ${uid().substring(0, 8)}`
}
}
function parseCountries () {
return countries.map(country => {
return {
label: country,
sublabel: getRandomSecondLabel(),
icon: getRandomIcon(),
stamp: getRandomStamp(),
value: country
}
})
}
export default {
data () {
return {
terms: '',
termsN: null,
countries: parseCountries(),
numbers: [1, 2, 3, 4, 5, 1111, 2222, 3333, 4444, 5555].map(v => ({ label: String(v), value: v }))
}
},
methods: {
search (terms, done) {
setTimeout(() => {
done(filter(terms, {field: 'value', list: parseCountries()}))
}, 1000)
},
selected (item) {
this.$q.notify(`Selected suggestion ${JSON.stringify(item.label)}`)
},
onChange (val) {
console.log('@change', JSON.stringify(val))
},
onInput (val) {
console.log('@input', JSON.stringify(val))
}
}
}
</script>