Skip to content

Commit 1dd5b9a

Browse files
committed
docs(Autocomplete): update examples
1 parent 637fa21 commit 1dd5b9a

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

docs/assets/js/partials/snippets.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,43 @@ export default () => {
385385
}
386386
// js-docs-end autocomplete-custom-options
387387

388+
// js-docs-start autocomplete-option-values
389+
const myAutoCompleteValues = document.getElementById('myAutoCompleteValues')
390+
391+
if (myAutoCompleteValues) {
392+
new coreui.Autocomplete(myAutoCompleteValues, {
393+
cleaner: true,
394+
indicator: true,
395+
name: 'autocomplete-option-values',
396+
options: [
397+
{ label: 'Product A', value: 1 },
398+
{ label: 'Product B', value: 2 },
399+
{ label: 'Product C', value: 3.5 },
400+
{ label: 'Product D', value: 42 },
401+
{
402+
label: 'Categories',
403+
options: [
404+
{ label: 'Electronics', value: 100 },
405+
{ label: 'Books', value: 200 },
406+
{ label: 'Clothing', value: 300 }
407+
]
408+
}
409+
],
410+
placeholder: 'Select product by ID...',
411+
search: 'global',
412+
value: 100
413+
})
414+
415+
// Log the selected value to demonstrate that numbers are converted to strings
416+
myAutoCompleteValues.addEventListener('changed.coreui.autocomplete', event => {
417+
// eslint-disable-next-line no-console
418+
console.log('Selected value:', event.value.value)
419+
// eslint-disable-next-line no-console
420+
console.log('Selected label:', event.value.label)
421+
})
422+
}
423+
// js-docs-end autocomplete-option-values
424+
388425
// -------------------------------
389426
// Multi Selects
390427
// -------------------------------

docs/content/forms/autocomplete.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ You can organize suggestions into groups using optgroups for better categorizati
5858

5959
{{< js-docs name="autocomplete-grouped-data" file="docs/assets/js/partials/snippets.js" >}}
6060

61+
### Options with values
62+
63+
You can use values in your options array. This is particularly useful when working with database IDs, product codes, or any numeric identifiers. Note that the component internally converts number values to strings for consistency and DOM compatibility.
64+
65+
{{< example stackblitz_pro="true" stackblitz_add_js="true">}}
66+
<div id="myAutoCompleteValues"></div>
67+
{{< /example >}}
68+
69+
{{< js-docs name="autocomplete-option-values" file="docs/assets/js/partials/snippets.js" >}}
70+
71+
{{< callout info >}}
72+
**Important:** While you can pass number values in your options, the component internally converts all values to strings using `String(value)`. When handling selection events, remember that `event.value.value` will always be a string representation of your original number.
73+
74+
For example:
75+
- Input: `{ label: 'Product A', value: 42 }`
76+
- Internal storage: `{ label: 'Product A', value: '42' }`
77+
- Event output: `event.value.value === '42'` (string)
78+
{{< /callout >}}
79+
6180
### External data
6281

6382
You can configure CoreUI's AutoComplete component to fetch and display options dynamically from an external API. This is useful when you need to autocomplete data that changes frequently or is too large to preload.

js/dist/autocomplete.js

Lines changed: 22 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)