npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-suggestbox

v1.1.1

Published

Autocomplete / Multiselect / Typeahead component made with Svelte

Downloads

5

Readme

Svelte SuggestBox

Svelte SuggestBox is a dropdown list component built with Svelte.

The Svelte SuggestBox can be customised in a lot of ways that should cover most of the possible use cases.

Features

  • Combobox behaviour.
  • Multiple item selection and allow duplicates.
  • Custom behaviour for item suggestions.
  • Custom display field, and search field.
  • A custom layout for:
    • dropdown items.
    • selected items.
    • trigger button.
    • result count and "not found" messages.

Installation

yarn add svelte-suggestbox

or

npm install svelte-suggestbox --save

Get started

<script>
import SuggestBox from 'svelte-suggestbox';
</script>

<SuggestBox
placeholder={"Select an item"}
items={[{name: "Hello"}, {name: "World"}, {name: "It's me"}]}
/>

Demo

Examples available at

Props

| Prop | Type | Description| |---|---|---| | placeholder | string | Placeholder for input. | | items | array | A fixed set for dropdown menu elements. All array items must be of a JSON type. | displayField | function(item) | Returns a field value for supplied item. This value is used to lookup user input value in. Also to show in the dropdown and as a selected item(s). By default it will return name field value. default value: displayField = function(item) { return item.name; } If you use a different field than name then you should replace this function by defining this prop. | indexOfValue | function(item, value) | Returns the first index at which user input value can be found in the item.default value: indexOfValue = function(item, value) { return displayField(item).toLowerCase().indexOf(value.toLowerCase()); } if you use different logic to lookup user input value in items then you should define this prop. For instance, by default this function is Case Insensitive, if you want to make it Case Sensitive then you can change to: indexOfValue = {function(item, value) { return displayField(item).indexOf(value); } }| | isSuggested | function(item, value) | Returns true or false indicating weather an item should be shown in the dropdown list. default value: isSuggested = function(item, value) { return indexOfValue(item, value) > -1; } | | getSuggestedItems | function(value) | Returns an array of suggested dropdown list items. If user input value is empty returns all items. If there is a user input value then it filters out items using functions shown above. Also items get sorted using functions shown below.default value:getSuggestedItems = function(value) { return items.map((item, index) => ({ ...item, [INDEX_FIELD]: index })).filter(item => hideSelected && selectionMap[item[INDEX_FIELD]] ? false : value && value.length && enableInput ? isSuggested(item, value) : true ).sort(sortComparator);} This function can be replaced to change suggestions behaviour. But if you do so note that all the functions described above and the items array will be ignored. This prop can be used for XHR requests, so filtering and sorting will be handled on a server side.| | sortField | function(item) | Returns a field value for supplied item. This value is used to sort items before showing in the dropdown.default value:sortField = function(item) { return item.name; } Same as the displayField this function returns name field. If you want to sort by a different field value then, you should replace this function. For instance, if you want to preserve initial items array order, you can use $index$ field that holds reference to items array index, so:sortField = function(item) { return item['$index$']; } will show dropdown items in the same order they are in items array.| | sortComparator | function | sorting logic for suggested items default value: sortComparator = function(a, b) { return sortField(a) > sortField(b) ? 1 : sortField(a) < sortField(b) ? -1 : 0; }| | callDelay | integer | A number of milliseconds to wait before calling getSuggestedItems. Can be useful with XHR requests. Default value is 0. The dropdown will show waiting message for callDelay number of milliseconds. | enableInput | boolean | default true. Enables user to input a lookup value. Set to false for combobox behaviour. | typeAhead | boolean | default true. When there is only one suggested item left, it will fill the rest of the value into the input.| | multiSelect | boolean | default false. | | allowDuplicates | boolean | default false. | | closeOnSelect | boolean | default true. | | hideSelected | boolean | default true. | | selectedItems | array | an array of items that were selected. | | cls | string | a CSS class that will be appended (if defined) to the parent DOM element|