@@ -167,6 +167,116 @@ export default () => {
167
167
} )
168
168
}
169
169
170
+ // -------------------------------
171
+ // Aurocomplete
172
+ // -------------------------------
173
+ // 'Autocomplete components' example in docs only
174
+ // js-docs-start autocomplete-array-data
175
+ const myAutoComplete = document . getElementById ( 'myAutoComplete' )
176
+
177
+ if ( myAutoComplete ) {
178
+ new coreui . Autocomplete ( myAutoComplete , {
179
+ name : 'autocomplete' ,
180
+ options : [
181
+ 'Angular' ,
182
+ 'Bootstrap' ,
183
+ {
184
+ label : 'React.js' ,
185
+ disabled : true
186
+ } ,
187
+ 'Vue.js' ,
188
+ {
189
+ label : 'backend' ,
190
+ options : [ 'Django' , 'Laravel' , 'Node.js' ]
191
+ }
192
+ ] ,
193
+ value : 'Laravel'
194
+ } )
195
+ }
196
+ // js-docs-end autocomplete-array-data
197
+
198
+ // js-docs-start autocomplete-grouped-data
199
+ const myAutoCompleteGrouped = document . getElementById ( 'myAutoCompleteGrouped' )
200
+
201
+ if ( myAutoCompleteGrouped ) {
202
+ new coreui . Autocomplete ( myAutoCompleteGrouped , {
203
+ name : 'autocomplete-grouped' ,
204
+ options : [
205
+ 'Angular' ,
206
+ {
207
+ label : 'Bootstrap' ,
208
+ selected : true
209
+ } ,
210
+ {
211
+ label : 'React.js' ,
212
+ disabled : true
213
+ } ,
214
+ 'Vue.js' ,
215
+ {
216
+ label : 'backend' ,
217
+ options : [ 'Django' , 'Laravel' , 'Node.js' ]
218
+ }
219
+ ]
220
+ } )
221
+ }
222
+ // js-docs-end autocomplete-grouped-data
223
+
224
+ // js-docs-start autocomplete-external-data
225
+ const myAutoCompleteExternalData = document . getElementById ( 'myAutoCompleteExternalData' )
226
+
227
+ if ( myAutoCompleteExternalData ) {
228
+ const getUsers = async ( name = '' ) => {
229
+ try {
230
+ const response = await fetch ( `https://apitest.coreui.io/demos/users?first_name=${ name } &limit=10` )
231
+ const users = await response . json ( )
232
+
233
+ return users . records . map ( user => ( {
234
+ value : user . id ,
235
+ label : user . first_name
236
+ } ) )
237
+ } catch ( error ) {
238
+ // eslint-disable-next-line no-console
239
+ console . error ( 'Error fetching users:' , error )
240
+ }
241
+ }
242
+
243
+ const autocomplete = new coreui . Autocomplete ( myAutoCompleteExternalData , {
244
+ cleaner : true ,
245
+ highlightOptionsOnSearch : true ,
246
+ name : 'autocomplete-external' ,
247
+ options : [ ] ,
248
+ placeholder : 'Search names...' ,
249
+ search : [ 'external' , 'global' ] , // 🔴 'external' is required for external search
250
+ showHints : true
251
+ } )
252
+
253
+ let lastQuery = null
254
+ let debounceTimer = null
255
+
256
+ myAutoCompleteExternalData . addEventListener ( 'show.coreui.autocomplete' , async ( ) => {
257
+ const users = await getUsers ( )
258
+ autocomplete . update ( { options : users } )
259
+ } )
260
+
261
+ myAutoCompleteExternalData . addEventListener ( 'input.coreui.autocomplete' , event => {
262
+ const query = event . value
263
+
264
+ if ( query === lastQuery ) {
265
+ return
266
+ }
267
+
268
+ lastQuery = query
269
+
270
+ clearTimeout ( debounceTimer )
271
+
272
+ debounceTimer = setTimeout ( async ( ) => {
273
+ const users = await getUsers ( query )
274
+ autocomplete . update ( { options : users } )
275
+ } , 200 )
276
+ } )
277
+ }
278
+ // js-docs-end autocomplete-external-data
279
+
170
280
// -------------------------------
171
281
// Multi Selects
172
282
// -------------------------------
0 commit comments