
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
simple-svelte-autocomplete
Advanced tools
Autocomplete / Select / Typeahead component made with Svelte 3
Autocomplete / Select / Typeahead component made with Svelte
npm i -D simple-svelte-autocomplete
Import the component and define items:
import AutoComplete from "simple-svelte-autocomplete";
const colors = ["White", "Red", "Yellow", "Green", "Blue", "Black"];
let selectedColor;
And use it like this:
<AutoComplete items={colors} bind:selectedItem={selectedColor} />
You can also use it with array of objects:
const colorList = [
{ id: 1, name: "White", code: "#FFFFFF" },
{ id: 2, name: "Red", code: "#FF0000" },
{ id: 3, name: "Yellow", code: "#FF00FF" },
{ id: 4, name: "Green", code: "#00FF00" },
{ id: 5, name: "Blue", code: "#0000FF" },
{ id: 6, name: "Black", code: "#000000" }
];
let selectedColorObject;
Just define which field should be used as label:
<AutoComplete
items={colorList}
bind:selectedItem={selectedColorObject}
labelFieldName="name" />
Specifying function for label instead of field name is also supported:
<AutoComplete
items={colorList}
bind:selectedItem={selectedColorObject}
labelFunction={color => color.id + '. ' + color.name} />
By default the component searches by the item label, but it can also search by custom fields by specifying keywords
function. For example to enable searching by color name and color HEX code:
<AutoComplete
items={colorList}
bind:selectedItem={selectedColorObject}
labelFieldName="name"
keywordsFunction={color => color.name + ' ' + color.code} />
Define a searchFunction
which will be called with keyword
parameter.
If you have searchFunction
defined you don't need to specify items
since the function will be used for loading.
<AutoComplete searchFunction={getItems} labelFieldName="name" valueFieldName="id" bind:selectedItem={myValue} />
async function getItems(keyword) {
const url = '/api/my-items/?format=json&name=' + encodeURIComponent(keyword);
const response = await fetch(url);
const json = await response.json();
return json.results;
}
{
"results": [
{
"id": 1,
"name": "Sample One",
"date": "2020-09-25",
},
{
"id": 2,
"name": "Sample Two",
"date": "2020-09-26",
}
]
}
Props you may want to specify include:
items
- array of items the user can select from (optional, use searchFunction
for async loading of items)searchFunction
- optional function to load items asynchroniously from HTTP call for exampleselectedItem
- the current item that is selected (object if the array of items contains objects)labelFieldName
- the name of the field to be used for showing the items as text in the droprownkeywordsFieldName
- the name of the filed to search byvalue
- derived value from the selectedItem
, equals to selectedItem
if valueFieldName
is not specifiedvalueFieldName
- field to use to derive the value from the selected itemlabelFunction
- optional function that creates label from the item. If used labelFieldName
is ignoredkeywordsFunction
- optional function that creates text to search from the item. If used keywordsFieldName
is ignoredvalueFunction
- optional function that derives the value from the selected item. If used valueFieldName
is ignoredkeywordsCleanFunction
- optional function to additionally process the derived keywords from the itemtextCleanFunction
- optional function to additionally process the user entered textselectFirstIfEmpty
- set to true to select the first item if the user clears the text and closes the dropdown. Defaults to false.minCharactersToSearch
- minimum length of search text to perform search, defaults to 1maxItemsToShowInList
- maximum number of items to show in the dropdown list, defaults 0 (no limit)disabled
- disable the controlbeforeChange
- function called before a new value is selectedonChange
- function called after new value is selectedplaceholder
- change the text displayed when no option is selectednoResultsText
- text to show in the dropdown when the search text does not match any item. Defaults to "No results found". Can be set to "" to not show anything.hideArrow
- set to true to not show the blue dropdown arrowshowClear
- set to true to show X button that can be used to deselect the selected itemclassName
- apply a className to the controlinputClassName
- apply a className to the input controlinputId
- apply an id attribute to the the input controldropdownClassName
- apply a className to the dropdown div showing the list of itemsname
- generate an HTML input with this name, containing the current valuedebug
- flag to enable detailed log statements from the componentautocomplete
the class applied to the main controlautocomplete-input
the class applied to the input listautocomplete-list
the class applied to the dropdown listautocomplete-list-item
the class applied to items in the dropdown listThe component is inteded to use with Bulma but it can be adapted to use Boostrap or anything else.
FAQs
Autocomplete / Select / Typeahead component made with Svelte 3
The npm package simple-svelte-autocomplete receives a total of 2,139 weekly downloads. As such, simple-svelte-autocomplete popularity was classified as popular.
We found that simple-svelte-autocomplete demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.