<script>importTypeAheadfrom'@dimitrilahaye/svelte-typeahead';constasyncProps={isAsync: true,value: 'alpha3Code',placeholder: 'Type a country',clearLabel: 'Clear countries list',loadingText: 'Hurry go...',noResultText: 'nope!',optionInput: 'name',searchData: async(str)=>{constresponse=awaitfetch(`https://restcountries.eu/rest/v2/name/${str}`);constcountries=awaitresponse.json();letselected;if(response.ok){selected=countries.map((c)=>{return{name: c.name,alpha3Code: c.alpha3Code};});}else{selected=[];}returnselected;}};constconfig={closeOnSelect: true,resetButton: false,multiple: false,};letselection=[];</script><TypeAhead{...asyncProps}{config}bind:selection/>
{#each selection as country (country.alpha3Code)}
<p>{country.name}</p>
{/each}
Above, bind:selection is a shortcut which will reflect all the changes on the selected items.
By this way, no need to use TypeAhead events.
Documentation
Props
name
description
isAsync(boolean)
determine if TypeAhead will be asynchronous. Default is false
items(array)
default itemsof the select field. Default is []
searchData(Function)
callback used for the items search. It requires 2 arguments for sync search (the item during iteration and the substring entered into the search field), and 1 argument for the async search (the substring entered into the search field). If undefined, default callback will be used: (item, v) => item.toLowerCase().includes(v.toLowerCase()).
value(string)
uniq identifier key for each item. If undefined, item itself will be used as uniq identifier.
optionFunction(Function)
function returning a string, used to display the text of each item. If undefined, optionInput will be used.
optionInput(string)
item key used to be displayed as text. If undefined, item itself will be displayed.
placeholder(string)
placeholder of the select field. Default is 'Search'
clearLabel(string)
text of the button to reset the select field. Default is 'Clear selection'
loadingText(string)
text displayed during the loading of async data. Default is 'Loading...'
noResultText(string)
text displayed when no data has been found. Default is 'No result...'
config(see TypeAhead configuration)
configuration of TypeAhead
selection(Array)
bind this prop in your parent component to track all the changes on the selected items. By this way, no need to use TypeAhead events.
TypeAhead configuration
config field
description
closeOnClickOutside
Close the dropdown panel when you click outside the select field. Default is 'true'.
closeOnSelect
Close the dropdown panel when you select an item. Default is 'false'.
resetButton
Display the reset button at the right of the select field. Default is 'true'.
multiple
Allow multiple items selection. Default is 'true'.
If multiple is setted at true, closeOnSelect is always considered as false.
Events
name
description
selectitem=> event.detail.items(Array)
event triggered when you select an item.
clearitem=> event.detail.item(Object)
event triggered when you clear one of the selected items.
reset
event triggered when you reset the select field.
Unit test
npm test# then coverage report in ./ut-coverage
Storybook
npm run storybook
# then visit http://localhost:6006/
The Tidelift Subscription provides access to a continuously curated stream of human-researched and maintainer-verified data on open source packages and their licenses, releases, vulnerabilities, and development practices.