1.0.15 • Published 10 months ago
@browser.style/auto-suggest v1.0.15
AutoSuggest
A customizable web component that provides search input with auto-suggest functionality, supporting both datalist and popover list modes.
Installation
npm install @browser.style/auto-suggestUsage
import '@browser.style/auto-suggest';<!-- Basic usage with datalist -->
<auto-suggest
api="https://api.example.com/search?q="
api-value-path="id"
api-display-path="name"
label="Search"
placeholder="Type to search">
</auto-suggest>
<!-- Using popover list mode -->
<auto-suggest
api="https://api.example.com/search?q="
api-value-path="id"
api-display-path="name"
label="Search"
list-mode="ul"
placeholder="Type to search">
</auto-suggest>Attributes
api(required): API endpoint for fetching suggestionsapi-value-path: Path to value in API responseapi-display-path: Path to display text in API responseapi-text-path: Path to additional text in API responseapi-array-path: Path to array in nested API responsecache: Enable result caching (true/false)debounce: Debounce time in ms (default: 300)display: Initial display valueinvalid: Custom validation messagelabel: Input labellist-mode: Display mode ('datalist' or 'ul')minlength: Min. characters before search (default: 3)noform: Exclude from form submissionnolimit: Allow free text inputvalue: Initial value
Events
autoSuggestSelect: Option selectedautoSuggestClear: Input clearedautoSuggestFetchStart: Fetch startedautoSuggestFetchEnd: Fetch completedautoSuggestFetchError: Fetch failedautoSuggestNoResults: No results foundautoSuggestNoSelection: Enter pressed (requires nolimit)
Form Integration
The component integrates with forms and provides both value and displayValue:
<form>
<auto-suggest
name="search"
api="https://api.example.com/search?q="
api-value-path="id"
api-display-path="name">
</auto-suggest>
</form>Access form values:
const form = document.querySelector('form');
const suggest = form.elements.search;
console.log(suggest.value); // Selected value
console.log(suggest.displayValue); // Display text