A select/autocomplete component for Svelte apps. With support for grouping, filtering, async and more.
yarn add svelte-selectNote: Install as a dev dependency (yarn add svelte-select --dev) if using Sapper to avoid a SSR error.
<script>
import Select from 'svelte-select';
let items = [
{value: 'chocolate', label: 'Chocolate'},
{value: 'pizza', label: 'Pizza'},
{value: 'cake', label: 'Cake'},
{value: 'chips', label: 'Chips'},
{value: 'ice-cream', label: 'Ice Cream'},
];
let selectedValue = undefined;
</script>
<Select {items} bind:selectedValue></Select>-
items: ArrayDefault:[]. List of selectable items that appear in the dropdown. -
selectedValue: AnyDefault:undefined. Selected item or items -
filterText: StringDefault:''. Text to filteritemsby. -
placeholder: StringDefault:'Select...'. Placeholder text. -
noOptionsMessage: StringDefault:'No options'. Message to display in list when there are noitems. -
optionIdentifier: StringDefault:'value'. Override default identifier. -
listOpen: BooleanDefault:false. Open/close list. -
hideEmptyState: BooleanDefault:false. Hide list and don't shownoOptionsMessagewhen there are noitems. -
containerClasses: StringDefault:''. Add extra container classes, for example 'global-x local-y'. -
containerStyles: StringDefault:''. Add inline styles to container. -
isClearable: BooleanDefault:true. Enable clearing of selected items. -
isCreatable: BooleanDefault:false. Can create new item(s) to be added toselectedValue. -
isDisabled: BooleanDefault:false. Disable select. -
isMulti: BooleanDefault:false. Enable multi-select,selectedValuebecomes an array of selected items. -
isSearchable: BooleanDefault:true. Enable search/filtering ofitemsviafilterText. -
isGroupHeaderSelectable: BooleanDefault:false. Enable selectable group headers initems(see adv demo). -
listPlacement: StringDefault:'auto'. When'auto'displays either'top'or'bottom'depending on viewport. -
hasError: BooleanDefault:false. Show/hide error styles around select input (red border by default). -
listAutoWidth: BooleanDefault:true. List width will grow wider than the Select container (depending on list item content length). -
showIndicator: BooleanDefault:false. If true, the chevron indicator is always shown. -
inputAttributes: ObjectDefault:{}. Useful for passing in HTML attributes like'id'to the Select input. -
Item: ComponentDefault:Item. Item component. -
Selection: ComponentDefault:Selection. Selection component. -
MultiSelection: ComponentDefault:MultiSelection. Multi selection component. -
Icon: ComponentDefault:Icon. Icon component. -
iconProps: ObjectDefault:{}. Icon props. -
indicatorSvg: @htmlDefault:undefined. Override default SVG chevron indicator. -
isVirtualList: BooleanDefault:false. Uses svelte-virtual-list to render list (experimental).
If you really want to get your hands dirty these internal functions are exposed as props to override if needed. See the adv demo or look through the test file (test/src/index.js) for examples.
export let itemFilter = (label, filterText, option) => label.toLowerCase().includes(filterText.toLowerCase());export let groupBy = undefined; // see adv demo for exampleexport let groupFilter = groups => groups;export let createGroupHeaderItem = groupValue => {
return {
value: groupValue,
label: groupValue
};
};export let createItem = filterText => {
return {
value: filterText,
label: filterText
};
};export let getOptionLabel = (option, filterText) => {
return option.isCreator ? `Create \"${filterText}\"` : option.label;
};export let getSelectionLabel = option => {
if (option) return option.label;
};export let getGroupHeaderLabel = option => {
return option.label;
};export function handleClear() {
selectedValue = undefined;
listOpen = false;
dispatch("clear", selectedValue);
handleFocus();
}export function handleClear() {
selectedValue = undefined;
listOpen = false;
dispatch("clear", selectedValue);
handleFocus();
}export let loadOptions = undefined; // if used must return a Promise that updates 'items'You can style a component by overriding the available CSS variables.
<script>
import Select from 'svelte-select';
const items = ['One', 'Two', 'Three'];
</script>
<style>
.themed {
--border: 3px solid blue;
--borderRadius: 10px;
--placeholderColor: blue;
}
</style>
<div class="themed">
<h2>Theming</h2>
<Select {items}></Select>
</div>You can also use the inputStyles prop to write in any override styles needed for the input.
<script>
import Select from 'svelte-select';
const items = ['One', 'Two', 'Three'];
</script>
<Select {items} inputStyles="box-sizing: border-box;"></Select> | Event Name | Callback | Description |
|---|---|---|
| select | selectedValue | fires when selectedValue changes |
| clear | - | fires when clear all is invoked |
| error | { type, details } | fires when error is caught |
<script>
import Select from 'svelte-select';
let items = [...];
function handleSelect(selectedVal) {
...
}
function onClear() {
...
}
</script>
<Select {items} on:select={handleSelect} on:clear={handleClear}></Select>yarn global add serve@8
yarn
yarn dev
yarn test:browserIn your favourite browser go to http://localhost:3000 and open devtools and see the console for the test output. When developing its handy to see the component on the page; comment out the select.$destroy(); on the last test in /test/src/index.js or use the test.only() to target just one test.
For example:
test.only('when getSelectionLabel contains HTML then render the HTML', async (t) => {
const select = new Select({
target,
props: {
selectedValue: items[0],
getSelectionLabel: (option) => `<p>${option.label}</p>`,
}
});
t.ok(document.querySelector('.selection').innerHTML === '<p>Chocolate</p>');
//select.$destroy();
});If you're using webpack with svelte-loader, make sure that you add "svelte" to resolve.mainFields in your webpack config. This ensures that webpack imports the uncompiled component (src/index.html) rather than the compiled version (index.mjs) — this is more efficient.
If you're using Rollup with rollup-plugin-svelte, this will happen automatically.