Skip to content

Commit 40fafca

Browse files
authored
feat(Autocomplete): initial release
1 parent 8d61a59 commit 40fafca

18 files changed

+4846
-1
lines changed

docs/assets/js/partials/snippets.js

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,224 @@ export default () => {
167167
})
168168
}
169169

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+
280+
// js-docs-start autocomplete-custom-options
281+
const myAutocompleteCountries = document.getElementById('myAutocompleteCountries')
282+
const myAutocompleteCountriesAndCities = document.getElementById('myAutocompleteCountriesAndCities')
283+
284+
if (myAutocompleteCountries && myAutocompleteCountriesAndCities) {
285+
const countries = [
286+
{
287+
value: 'pl',
288+
label: 'Poland',
289+
flag: '🇵🇱'
290+
},
291+
{
292+
value: 'de',
293+
label: 'Germany',
294+
flag: '🇩🇪'
295+
},
296+
{
297+
value: 'us',
298+
label: 'United States',
299+
flag: '🇺🇸'
300+
},
301+
{
302+
value: 'es',
303+
label: 'Spain',
304+
flag: '🇪🇸'
305+
},
306+
{
307+
value: 'gb',
308+
label: 'United Kingdom',
309+
flag: '🇬🇧'
310+
}
311+
]
312+
313+
const cities = [
314+
{
315+
label: 'United States',
316+
code: 'us',
317+
flag: '🇺🇸',
318+
options: [
319+
{
320+
value: 'au',
321+
label: 'Austin'
322+
},
323+
{
324+
value: 'ch',
325+
label: 'Chicago'
326+
},
327+
{
328+
value: 'la',
329+
label: 'Los Angeles'
330+
},
331+
{
332+
value: 'ny',
333+
label: 'New York'
334+
},
335+
{
336+
value: 'sa',
337+
label: 'San Jose'
338+
}
339+
]
340+
},
341+
{
342+
label: 'United Kingdom',
343+
code: 'gb',
344+
flag: '🇬🇧',
345+
options: [
346+
{
347+
value: 'li',
348+
label: 'Liverpool'
349+
},
350+
{
351+
value: 'lo',
352+
label: 'London'
353+
},
354+
{
355+
value: 'ma',
356+
label: 'Manchester'
357+
}
358+
]
359+
}
360+
]
361+
362+
new coreui.Autocomplete(myAutocompleteCountries, {
363+
cleaner: true,
364+
indicator: true,
365+
options: countries,
366+
optionsTemplate(option) {
367+
return `<div class="d-flex align-items-center gap-2"><span class="fs-5">${option.flag}</span><span>${option.label}</span></div>`
368+
},
369+
placeholder: 'Select country',
370+
showHints: true,
371+
search: 'global'
372+
})
373+
374+
new coreui.Autocomplete(myAutocompleteCountriesAndCities, {
375+
cleaner: true,
376+
indicator: true,
377+
options: cities,
378+
optionsGroupsTemplate(optionGroup) {
379+
return `<div class="d-flex align-items-center gap-2"><span class="text-body fs-5">${optionGroup.flag}</span><span>${optionGroup.label}</span></div>`
380+
},
381+
placeholder: 'Select city',
382+
showHints: true,
383+
search: 'global'
384+
})
385+
}
386+
// js-docs-end autocomplete-custom-options
387+
170388
// -------------------------------
171389
// Multi Selects
172390
// -------------------------------

0 commit comments

Comments
 (0)