Code Monkey home page Code Monkey logo

svelte-autocomplete's Introduction

svelte-autocomplete

Lightweight typeahead / autocomplete component made with Svelte.js

  • no dependencies
  • can handle asynchronous data
  • use plain lists or key / value pairs

Install

npm install svelte-autocomplete

Usage

Import the component directly in your Svelte project.

import AutoComplete from 'svelte-autocomplete'

export default {
  components: {
    AutoComplete,
	...
  }
}

And then use it like so:

<AutoComplete class="input" name="fruits" items="{fruits}" minChar="1" />

<AutoComplete name="countries" items="{countries}" isAsync on:input="loadApiData(event)">
  <div class="notification">Loading data from API...</div>
</AutoComplete>

Options

Prop Type Default Description
name String - Form input name
class String - Additional styles for input element
items Array - Array with items, can be a plain list or key, value pairs
isAsync Boolean false If retrieving API data asynchronously
minChar Number 2 Min. characters to type before popup shows
maxItems Number 10 Max. items to show in popup
fromStart Boolean true Match from the start or anywhere in the string

Slots

Slot Prop dependencies Description
default isAsync custom loading indication

Made with Svelte

It's made with Svelte, which means you don't need any JS framework. Just use the dist/index.js file in any Javascript project:

include the bundle:

<script src="svelte-autocomplete/dist/index.js"></script>

create component like so:

const myComponent = new AutoComplete({
  target: document.querySelector('#app'),
  data: { 
    name: 'fruits',
    itemStart: 1,
    items: [...]
  }
})

svelte-autocomplete's People

Contributors

elcobvg avatar germanftorres avatar kkoehler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

svelte-autocomplete's Issues

compilation error

i have added this library to my project but ever since its been failing to load ever since. Here is the error message .

[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
node_modules/svelte-autocomplete/src/index.html (1:0)
1: <svelte:window on:click="close()" />
^
2:


3: <input
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
at error (/home/kinsley/WebstormProjects/node_modules/rollup/dist/shared/rollup.js:5265:30)
at Module.error (/home/kinsley/WebstormProjects/node_modules/rollup/dist/shared/rollup.js:9835:16)
at tryParse (/home/kinsley/WebstormProjects/node_modules/rollup/dist/shared/rollup.js:9716:23)
at Module.setSource (/home/kinsley/WebstormProjects/node_modules/rollup/dist/shared/rollup.js:10142:19)
at ModuleLoader.addModuleSource (/home/kinsley/WebstormProjects/node_modules/rollup/dist/shared/rollup.js:18312:20)
at ModuleLoader.fetchModule (/home/kinsley/WebstormProjects/node_modules/rollup/dist/shared/rollup.js:18368:9)
at async Promise.all (index 2)
at ModuleLoader.fetchStaticDependencies (/home/kinsley/WebstormProjects/node_modules/rollup/dist/shared/rollup.js:18393:34)
at async Promise.all (index 0)
at ModuleLoader.fetchModule (/home/kinsley/WebstormProjects/node_modules/rollup/dist/shared/rollup.js:18370:9)

Example with API endpoint

If you want to fetch results based on the input, where do you set the API endpoint? The demo page has this functionality but the source of the demo page is bundled and I don't see that site in a repo anywhere.

Thanks!

well done, works great (my changes included)

This is just what I needed - way better than trying to add some other solution loading as external js on the front end side.

I'm using the Async option, and got rid of the "data loading" as it loads from my site and the filesizes are pretty small.

Here is my version for anybody that may be able to glean something from it!
Autocomplete.txt

Failed to execute 'setAttribute' on 'Element': '{name}' is not a valid attribute name.

Hi, I tried to use your component just with the minimum requirement:

<AutoComplete class="input" name="fruits" items="{fruits}" minChar="1" />

But it fails at runtime:

h:function hydrate() {
			addListener(input, "input", input_input_handler);
			addListener(input, "input", input_handler);
			addListener(input, "focus", focus_handler);
			addListener(input, "blur", blur_handler);
			addListener(input, "keydown", keydown_handler);
			setAttribute(input, "type", "text");
			input.className = "{class} svelte-1yo2r3";
			setAttribute(input, "{name}", true);
			setAttribute(input, "{placeholder}", true);
			setAttribute(input, "{required}", true);
			setAttribute(input, "{disabled}", true);
			input.value = "{value || ''}";
			input.autocomplete = "{name}";
			addListener(li, "click", click_handler);
			li.className = "autocomplete-result{ i === arrowCounter ? ' is-active' : '' } svelte-1yo2r3";
			ul.className = "autocomplete-results{!isOpen ? ' hide-results' : ''} svelte-1yo2r3";
			if (!slot_content_default) {
				p.className = "fallback svelte-1yo2r3";
			}
			addListener(div, "click", click_handler_1);
			div.className = "autocomplete svelte-1yo2r3";
		},

Happens on the setAttribute(input, "{name}", true); line

I am using svelte 2.11 and parcel 1.9.7 with the svelte plugin 1.2.2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.