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

@dimitrilahaye/svelte-typeahead

v1.0.0

Published

Svelte typeahead / auto-completion component for sync and async data search.

Downloads

12

Readme

Svelte typeahead / auto-completion component for sync and async data

install it

npm i --save @dimitrilahaye/svelte-typeahead

Use it

<scrip>
    import TypeAhead from '@dimitrilahaye/svelte-typeahead';
    
    //...
</script>

Table of contents

Examples

Example with an array of objects

<script>  
import TypeAhead from './TypeAhead.svelte';  

const users = [  
  {  
  	"id": 1,  
  	"first_name": "Sigismund",  
  	"last_name": "Pople",  
  	"email": "[email protected]",  
  	"gender": "Male",  
  },
  ...
];  
const props = {  
  items: users,  
  value: 'id',  
  searchData: (user, str) => user.first_name.includes(str),  
  optionFunction: (o) => `${o.first_name} ${o.last_name}`,  
};  
let selectedUsers = [];
</script>   
<TypeAhead {...props}  
    on:selectitem="{(e) => selectedUsers = [...e.detail.items]}"  
    on:reset="{() => selectedUsers = []}"  
    on:clearitem="{(e) => selectedUsers = selectedUsers.filter((u) => u.id !== e.detail.item.id)}"
    />  
{#each selectedUsers as user (user.id)}  
    <p>{user.first_name} {user.last_name}</p>  
{/each}

Example with an array of strings

<script>
import TypeAhead from './TypeAhead.svelte';   
const countries = [  
  'Russia',  
  'Colombia',  
  'Sweden',  
  'China',
  ...
];  
const props = {  
items: countries,  
};  
let selectedCountriesSync = [];
</script>  
<TypeAhead {...props}  
  on:selectitem="{(e) => selectedCountriesSync = [...e.detail.items]}"  
  on:reset="{() => selectedCountriesSync = []}"  
  on:clearitem="{(e) => selectedCountriesSync = selectedCountriesSync.filter((c) => c !== e.detail.item)}"
  />  
{#each selectedCountriesSync as country}  
    <p>{country}</p>  
{/each}

Example with fetched data from an API

<script>  
import TypeAhead from './TypeAhead.svelte';
const asyncProps = {  
    isAsync: true,  
    value: 'alpha3Code',  
    placeholder: 'Type a country',  
    clearLabel: 'Clear countries list',  
    loadingText: 'Hurry go...',  
    noResultText: 'nope!',
    optionInput: 'name',  
    searchData: async (str) => {  
  	const response = await fetch(`https://restcountries.eu/rest/v2/name/${str}`);  
  	const countries = await response.json();  
  	let selected;  
  	if (response.ok) {  
  		selected = countries.map((c) => {  
  			return {  
  				name: c.name,  
  				alpha3Code: c.alpha3Code  
  			};  
  		});  
  	} else {  
  		selected = [];  
  	}  
  	return selected;  
  }
};  
const config = {  
  closeOnSelect: true,  
  resetButton: false,  
  multiple: false,  
};  
let selection = [];
</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/