svelte-select
A <Select> component for Svelte apps
Last updated 5 years ago by rob-balfre .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ npm install svelte-select 
SYNC missed versions from official npm registry.
Svelte Select

Svelte Select

version downloads
A select/autocomplete/typeahead Svelte component.
Lightweight, feature-rich and modular by design.

Demos

🌱 Simple demo

🌻 Advanced demo

Installation

npm install svelte-select

Migrating for v4 to v5

v5 is a major release that that includes some ⚠️ BREAKING CHANGES ⚠️

Removed isVirtualList instead npm i svelte-tiny-virtual-list -D and

<script>
  import VirtualList from 'svelte-tiny-virtual-list';
</script>

<Select VirtualList />

CSS classes and custom properties converted from camel to kebab case. For example selectedItem → selected-item and --borderRadius → --border-radius

Other CSS class name changes:

selectContainer → svelte-select
listContainer → list
indicator → chevron
virtual-list removed

Prop changes:

containerClasses → class
MultiSelection → Multi
indicatorSvg → ChevronIcon
selectedValue removed (was already deprecated in v4 in favour of value)
loadOptionsInterval → debounceWait

Props

Prop Type Default Description
items array [] Array items available to display / filter
value any null Selected value(s)
justValue any null Selected value(s) excluding container object
optionIdentifier string value Override default identifier
labelIdentifier string label Override default identifier
id string null Add an id to the filter input field
filterText string '' Text to filter items by
placeholder string Please select Placeholder text
noOptionsMessage string No options Message displayed when no items
hideEmptyState boolean false When no items hide list and noOptionsMessage
listOpen boolean false Open/close list
class string '' container classes
containerStyles string '' Add inline styles to container
isClearable boolean true Enable clearing of value(s)
isCreatable boolean false Can create new item(s) to be added to value
isDisabled boolean false Disable select
isMulti boolean false Enable multi-select
isSearchable boolean true If false search/filtering is disabled
isGroupHeaderSelectable boolean false Enable selectable group headers
listPlacement string auto Display list 'auto', 'top' or 'bottom'
hasError boolean false Show error styles around select input
listAutoWidth boolean true If false will ignore width of select
showChevron boolean false Show chevron at all times
inputAttributes object {} Pass in HTML attributes to Select's input
iconProps object {} Icon props
filteredItems array [] List of items after filtering (read only)
placeholderAlwaysShow boolean false When isMulti placeholder text will always show
isWaiting boolean false Show LoadingIcon. loadOptions will override this
listOffset number 5 px space between select and list
debounceWait number 300 milliseconds debounce wait
suggestions boolean false Show search suggestions before user input

Replaceable components

Import Type Description
Item component Item component
Selection component Selection component
Multi component Multi select support
ChevronIcon component Chevron Icon
ClearIcon component Clear Icon
LoadingIcon component Spinning Loading Icon

Optional component imports

Import Type Description
VirtualList component Virtual list support (uses svelte-tiny-virtual-list)
Icon component Icon component

Items

items can be simple arrays or collections.

<script>
  import Select from 'svelte-select';

  let simple = ['one', 'two', 'three'];

  let collection = [
    { value: 1, label: 'one' },
    { value: 2, label: 'two' },
    { value: 3, label: 'three' },
  ];
</script>

<Select items={simple} />

<Select items={collection} />

They can also be grouped and include non-selectable items.

<script>
  import Select from 'svelte-select';

  const items = [
    {value: 'chocolate', label: 'Chocolate', group: 'Sweet'},
    {value: 'pizza', label: 'Pizza', group: 'Savory'},
    {value: 'cake', label: 'Cake', group: 'Sweet', selectable: false},
    {value: 'chips', label: 'Chips', group: 'Savory'},
    {value: 'ice-cream', label: 'Ice Cream', group: 'Sweet'}
  ];

  const groupBy = (item) => item.group;
</script>

<Select {items} {groupBy} />

You can also use custom collections.

<script>
  import Select from 'svelte-select';

  const optionIdentifier = 'id';
  const labelIdentifier = 'title';

  const items = [
    {id: 0, title: 'Foo'},
    {id: 1, title: 'Bar'},
  ];
</script>

<Select {optionIdentifier} {labelIdentifier} {items} />

Async Items

To load items asynchronously then loadOptions is the simplest solution. Supply a function that returns a Promise that resolves with a list of items. loadOptions has debounce baked in and fires each time filterText is updated.

<script>
  import Select from 'svelte-select';

  import { someApiCall } from './services';

  async function examplePromise(filterText) {
    // Put your async code here...
    // For example call an API using filterText as your search params
    // When your API responds resolve your Promise
    let res = await someApiCall(filterText);
    return res;
  }
</script>

<Select loadOptions={examplePromise} />

Exposed methods

These internal functions are exposed 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;
export 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() {
  value = undefined;
  listOpen = false;
  dispatch("clear", value);
  handleFocus();
}
export let loadOptions = undefined; // if used must return a Promise that updates 'items'
/* Return an object with { cancelled: true } to keep the loading state as active. */
export const getFilteredItems = () => {
  return filteredItems;
};
export function debounce(fn, wait = 1) {
    let timeout;
    return function (...args) {
        clearTimeout(timeout);
        timeout = setTimeout(() => fn.apply(this, ...args), wait);
    };
}

| debounce | function | Debounce function | | filter | function | Filter options function | | loadOptions | function | Return a Promise that resolves with items | | getItems | function | Take full control of async and loadOptions defaults |

A11y (Accessibility)

Override these methods to change the aria-context and aria-selection text.

export let ariaValues = (values) => {
  return `Option ${values}, selected.`;
}

export let ariaListOpen = (label, count) => {
  return `You are currently focused on option ${label}. There are ${count} results available.`;
}

export let ariaFocused = () => {
  return `Select is focused, type to refine list, press down to open the menu.`;
}

Styling

You can style a component by overriding the available CSS custom properties.

<script>
  import Select from 'svelte-select';
</script>

<Select --border-radius= "10px" --placeholder-color="blue" />

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>

Replace styles (Tailwind, Bootstrap, Bulma etc)

If you'd like to supply your own styles use: import Select from 'svelte-select/no-styles/Select.svelte'. Then add you own somewhere in your code or build pipeline. There is a tailwind stylesheet via import 'tailwind.css'. It uses @extend so PostCSS is required (experimental - looking for PRs to improve).

Events

Event Name Callback Description
select { detail } fires when value changes
focus { detail } fires when select > input on:focus
blur { detail } fires when select > input on:blur
clear { detail } fires when clear all is invoked or item is removed (by user) from multi select
loaded { options } fires when loadOptions resolves
error { type, details } fires when error is caught

Development

npm i
npm run dev-tests
npm test:browser

Open http://localhost:3000 and see devtools console output. When developing it's useful to see the component on the page; comment out the select.$destroy(); on test your debugging in /test/src/index.js and use 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: {
      value: items[0],
      getSelectionLabel: (option) => `<p>${option.label}</p>`,
    }
  });

  t.ok(document.querySelector('.selection').innerHTML === '<p>Chocolate</p>');

  //select.$destroy();
});

License

LIL

Current Tags

  • 5.0.0-alpha.10                                ...           alpha (5 years ago)
  • 5.3.0-beta.7                                ...           beta (4 years ago)
  • 6.0.0                                ...           latest (4 months ago)

196 Versions

  • 6.0.0                                ...           4 months ago
  • 5.8.3                                ...           3 years ago
  • 5.8.2                                ...           3 years ago
  • 5.8.1                                ...           3 years ago
  • 5.8.0                                ...           3 years ago
  • 5.7.0                                ...           3 years ago
  • 5.6.1                                ...           4 years ago
  • 5.6.0                                ...           4 years ago
  • 5.5.3                                ...           4 years ago
  • 5.5.2                                ...           4 years ago
  • 5.5.1                                ...           4 years ago
  • 5.5.0                                ...           4 years ago
  • 5.4.0                                ...           4 years ago
  • 5.3.1                                ...           4 years ago
  • 5.3.0                                ...           4 years ago
  • 5.3.0-beta.7                                ...           4 years ago
  • 5.3.0-beta.6                                ...           4 years ago
  • 5.3.0-beta.5                                ...           4 years ago
  • 5.3.0-beta.4                                ...           4 years ago
  • 5.3.0-beta.3                                ...           4 years ago
  • 5.3.0-beta.2                                ...           4 years ago
  • 5.3.0-beta.1                                ...           4 years ago
  • 5.3.0-beta.0                                ...           4 years ago
  • 5.2.1                                ...           4 years ago
  • 5.2.0                                ...           4 years ago
  • 5.1.4                                ...           4 years ago
  • 5.1.3                                ...           4 years ago
  • 5.1.2                                ...           4 years ago
  • 5.1.1                                ...           4 years ago
  • 5.1.0                                ...           4 years ago
  • 5.0.2                                ...           4 years ago
  • 5.0.1                                ...           4 years ago
  • 5.0.0                                ...           4 years ago
  • 5.0.0-beta.40                                ...           4 years ago
  • 5.0.0-beta.39                                ...           4 years ago
  • 5.0.0-beta.38                                ...           4 years ago
  • 5.0.0-beta.37                                ...           4 years ago
  • 5.0.0-beta.36                                ...           4 years ago
  • 5.0.0-beta.35                                ...           4 years ago
  • 5.0.0-beta.34                                ...           4 years ago
  • 5.0.0-beta.33                                ...           4 years ago
  • 5.0.0-beta.32                                ...           4 years ago
  • 5.0.0-beta.31                                ...           4 years ago
  • 5.0.0-beta.30                                ...           4 years ago
  • 5.0.0-beta.29                                ...           4 years ago
  • 5.0.0-beta.28                                ...           4 years ago
  • 5.0.0-beta.27                                ...           4 years ago
  • 5.0.0-beta.26                                ...           4 years ago
  • 5.0.0-beta.25                                ...           4 years ago
  • 5.0.0-beta.24                                ...           4 years ago
  • 5.0.0-beta.23                                ...           4 years ago
  • 5.0.0-beta.22                                ...           4 years ago
  • 5.0.0-beta.21                                ...           4 years ago
  • 5.0.0-beta.20                                ...           4 years ago
  • 5.0.0-beta.19                                ...           4 years ago
  • 5.0.0-beta.18                                ...           4 years ago
  • 5.0.0-beta.17                                ...           4 years ago
  • 5.0.0-beta.16                                ...           4 years ago
  • 5.0.0-beta.15                                ...           4 years ago
  • 5.0.0-beta.14                                ...           4 years ago
  • 5.0.0-beta.13                                ...           4 years ago
  • 5.0.0-beta.12                                ...           4 years ago
  • 5.0.0-beta.11                                ...           5 years ago
  • 5.0.0-beta.10                                ...           5 years ago
  • 5.0.0-beta.9                                ...           5 years ago
  • 5.0.0-beta.8                                ...           5 years ago
  • 5.0.0-beta.7                                ...           5 years ago
  • 5.0.0-beta.6                                ...           5 years ago
  • 5.0.0-beta.5                                ...           5 years ago
  • 5.0.0-beta.4                                ...           5 years ago
  • 5.0.0-beta.3                                ...           5 years ago
  • 5.0.0-beta.2                                ...           5 years ago
  • 5.0.0-beta.1                                ...           5 years ago
  • 5.0.0-beta.0                                ...           5 years ago
  • 4.4.7                                ...           5 years ago
  • 5.0.0-alpha.10                                ...           5 years ago
  • 5.0.0-alpha.9                                ...           5 years ago
  • 5.0.0-alpha.8                                ...           5 years ago
  • 5.0.0-alpha.7                                ...           5 years ago
  • 5.0.0-alpha.6                                ...           5 years ago
  • 5.0.0-alpha.5                                ...           5 years ago
  • 4.4.6                                ...           5 years ago
  • 4.4.5                                ...           5 years ago
  • 5.0.0-alpha.4                                ...           5 years ago
  • 5.0.0-alpha.3 [deprecated]           ...           5 years ago
  • 5.0.0-alpha.2                                ...           5 years ago
  • 4.4.4                                ...           5 years ago
  • 4.4.4-beta.1                                ...           5 years ago
  • 4.4.4-beta.0                                ...           5 years ago
  • 5.0.0-alpha.1                                ...           5 years ago
  • 4.4.3                                ...           5 years ago
  • 4.4.2                                ...           5 years ago
  • 5.0.0-alpha.0                                ...           5 years ago
  • 4.4.1                                ...           5 years ago
  • 4.4.0                                ...           5 years ago
  • 4.3.1                                ...           5 years ago
  • 4.3.1-beta.0                                ...           5 years ago
  • 4.3.0                                ...           5 years ago
  • 4.2.8-beta.2                                ...           5 years ago
  • 4.2.8-beta.1                                ...           5 years ago
  • 4.2.8-beta.0                                ...           5 years ago
  • 4.2.7                                ...           5 years ago
  • 4.2.7-beta.2                                ...           5 years ago
  • 4.2.7-beta.1                                ...           5 years ago
  • 4.2.7-beta.0                                ...           5 years ago
  • 4.2.6                                ...           5 years ago
  • 4.2.5                                ...           5 years ago
  • 4.2.4                                ...           5 years ago
  • 4.2.3                                ...           5 years ago
  • 4.2.3-beta.2                                ...           5 years ago
  • 4.2.3-beta.1                                ...           5 years ago
  • 4.2.3-beta.0                                ...           5 years ago
  • 4.2.2                                ...           5 years ago
  • 4.2.1                                ...           5 years ago
  • 4.2.0                                ...           5 years ago
  • 4.1.0                                ...           5 years ago
  • 4.0.0                                ...           5 years ago
  • 4.0.0-beta.13                                ...           5 years ago
  • 4.0.0-beta.12                                ...           6 years ago
  • 4.0.0-beta.11                                ...           6 years ago
  • 4.0.0-beta.10                                ...           6 years ago
  • 4.0.0-beta.9                                ...           6 years ago
  • 4.0.0-beta.8                                ...           6 years ago
  • 4.0.0-beta.7                                ...           6 years ago
  • 4.0.0-beta.6                                ...           6 years ago
  • 4.0.0-beta.5                                ...           6 years ago
  • 4.0.0-beta.4                                ...           6 years ago
  • 4.0.0-beta.3                                ...           6 years ago
  • 4.0.0-beta.2                                ...           6 years ago
  • 4.0.0-beta.1                                ...           6 years ago
  • 3.17.0                                ...           6 years ago
  • 3.16.1                                ...           6 years ago
  • 3.16.0                                ...           6 years ago
  • 3.15.0                                ...           6 years ago
  • 3.14.3                                ...           6 years ago
  • 3.14.2                                ...           6 years ago
  • 3.14.1                                ...           6 years ago
  • 3.14.0                                ...           6 years ago
  • 3.13.0                                ...           6 years ago
  • 3.12.0                                ...           6 years ago
  • 3.11.1                                ...           6 years ago
  • 3.11.0                                ...           6 years ago
  • 3.10.1                                ...           6 years ago
  • 3.10.0                                ...           6 years ago
  • 3.9.0                                ...           6 years ago
  • 3.8.1                                ...           6 years ago
  • 3.8.0                                ...           7 years ago
  • 3.7.2                                ...           7 years ago
  • 3.7.1                                ...           7 years ago
  • 3.7.0                                ...           7 years ago
  • 3.6.2                                ...           7 years ago
  • 3.6.1                                ...           7 years ago
  • 3.6.0                                ...           7 years ago
  • 3.5.0                                ...           7 years ago
  • 3.4.0                                ...           7 years ago
  • 3.3.7                                ...           7 years ago
  • 3.3.6                                ...           7 years ago
  • 3.3.5                                ...           7 years ago
  • 3.3.4                                ...           7 years ago
  • 3.3.3                                ...           7 years ago
  • 3.3.2                                ...           7 years ago
  • 3.3.1                                ...           7 years ago
  • 3.3.0                                ...           7 years ago
  • 3.2.0                                ...           7 years ago
  • 3.1.2                                ...           7 years ago
  • 3.1.1                                ...           7 years ago
  • 3.1.0                                ...           7 years ago
  • 3.0.2                                ...           7 years ago
  • 3.0.1                                ...           7 years ago
  • 3.0.0                                ...           7 years ago
  • 2.1.0                                ...           7 years ago
  • 2.1.0-beta.0                                ...           7 years ago
  • 2.0.3                                ...           7 years ago
  • 2.0.2                                ...           7 years ago
  • 2.0.1                                ...           7 years ago
  • 1.7.6                                ...           7 years ago
  • 1.7.5                                ...           8 years ago
  • 1.7.4                                ...           8 years ago
  • 1.7.3                                ...           8 years ago
  • 1.7.2                                ...           8 years ago
  • 1.7.1                                ...           8 years ago
  • 1.7.0                                ...           8 years ago
  • 1.6.0                                ...           8 years ago
  • 1.5.5                                ...           8 years ago
  • 1.5.4                                ...           8 years ago
  • 1.5.3                                ...           8 years ago
  • 1.5.2                                ...           8 years ago
  • 1.5.1                                ...           8 years ago
  • 1.5.0                                ...           8 years ago
  • 1.4.0                                ...           8 years ago
  • 1.3.0                                ...           8 years ago
  • 1.2.0                                ...           8 years ago
  • 1.1.0                                ...           8 years ago
  • 1.0.2                                ...           8 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
Maintainers (1)
Downloads
Total 864
Today 6
This Week 61
This Month 308
Last Day 3
Last Week 193
Last Month 171
Dependencies (0)
None
Dev Dependencies (21)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE