Autocomplete
The Autocomplete component provides a searchable dropdown with filtering, option descriptions, and multi-select support.
Installation
npm install @duskmoon-dev/el-autocomplete
Usage
import '@duskmoon-dev/el-autocomplete/register';
<el-dm-autocomplete
placeholder="Select a fruit..."
options='[{"value":"apple","label":"Apple"},{"value":"banana","label":"Banana"}]'
></el-dm-autocomplete>
Live Demo
Basic Autocomplete
Properties
| Property | Type | Default | Description |
|---|---|---|---|
value | string | '' | Selected value(s) |
options | string | '[]' | JSON array of options |
multiple | boolean | false | Enable multi-select |
disabled | boolean | false | Disables the autocomplete |
clearable | boolean | false | Show clear button |
placeholder | string | '' | Placeholder text |
size | 'sm' | 'md' | 'lg' | 'md' | Size variant |
loading | boolean | false | Show loading state |
no-results-text | string | 'No results found' | Text for empty results |
Option Format
Options should be a JSON array with the following structure:
interface AutocompleteOption {
value: string; // Unique value
label: string; // Display text
description?: string; // Optional description
disabled?: boolean; // Disable this option
group?: string; // Group name for grouping
}
With Descriptions
Options with Descriptions
<el-dm-autocomplete
placeholder="Select a framework..."
options='[{"value":"react","label":"React","description":"A JavaScript library..."}]'
></el-dm-autocomplete>
Multiple Selection
Multiple Selection
<el-dm-autocomplete
multiple
placeholder="Select languages..."
options='[...]'
></el-dm-autocomplete>
Clearable
Clearable
<el-dm-autocomplete clearable placeholder="Select and clear..." options='[...]'></el-dm-autocomplete>
Sizes
Autocomplete Sizes
<el-dm-autocomplete size="sm" placeholder="Small..." options='[...]'></el-dm-autocomplete>
<el-dm-autocomplete size="md" placeholder="Medium..." options='[...]'></el-dm-autocomplete>
<el-dm-autocomplete size="lg" placeholder="Large..." options='[...]'></el-dm-autocomplete>
Disabled
Disabled
<el-dm-autocomplete disabled placeholder="Disabled..." options='[...]'></el-dm-autocomplete>
Events
| Event | Description |
|---|---|
change | Fired when selection changes |
input | Fired when search text changes |
clear | Fired when cleared |
Event Handling
const autocomplete = document.querySelector('el-dm-autocomplete');
autocomplete.addEventListener('change', (event) => {
console.log('Selected:', event.detail.value);
console.log('Selected values:', event.detail.selectedValues);
});
autocomplete.addEventListener('input', (event) => {
console.log('Search:', event.detail.searchValue);
});
Keyboard Navigation
- Arrow Down/Up: Navigate options
- Enter: Select highlighted option
- Escape: Close dropdown
- Backspace: Remove last tag (in multiple mode)
Accessibility
- Uses
role="combobox"with proper ARIA attributes aria-expandedindicates dropdown state- Options have
role="option"witharia-selected - Full keyboard navigation support