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

@trevoreyre/autocomplete

v3.0.2

Published

Simple autocomplete component in vanilla JS and Vue

Downloads

4,206

Readme

@trevoreyre/autocomplete

The core package provides the base functionality of the autocomplete component. It provides an instance to track the current state, and event handlers to respond to user input. It doesn't actually render anything to the DOM, so that it can remain flexible and be used in multiple platforms.

The best way to learn how to use the core package is to take a look at the source code of other components that provide a rendering layer on top of it, like the JavaScript component and the Vue component

Demo

Take a look at the documentation page, and the Codepen examples.

Installation

Install the component from npm.

npm install --save @trevoreyre/autocomplete

Or using yarn.

yarn add @trevoreyre/autocomplete

You can also use the browser bundle in a script tag.

<script src="https://unpkg.com/@trevoreyre/autocomplete"></script>

Usage

The core package provides a class instance to handle the internal state of the autocomplete component. The component can be tweaked using a number of constructor arguments.

const autocomplete = new Autocomplete(options)

Arguments

| Argument | Type | Description | | :-------------- | :------------------ | :------------------------------------------------------------------------------------------------------------- | | search | Function (required) | The search function to be executed on user input. Can be a synchronous function or a Promise. | | autoSelect | Boolean | Controls whether first result should be highlighted after input | | setValue | Function | Sets the value of the calling component's input element | | setAttribute | Function | Sets attributes on the calling component's input element | | onUpdate | Function | Fired when the results list is updated. Receives results (Array), and selectedIndex (Number) as arguments. | | onSubmit | Function | Fired when user submits result. Receives result as argument. | | onShow | Function | Fired when the results list is shown | | onHide | Function | Fired when the results list is hidden | | onLoading | Function | Fired if search is a Promise and hasn't resolved yet | | onLoaded | Function | Fired after asynchronous search function resolves | | submitOnEnter | Boolean | If true, immediately call onSubmit on result when pressing Enter. Default: false |

Event handlers

The core package provides a number of event handlers that can be wired up in the calling component.

| Name | Description | | :---------------------- | :-------------------------------------------- | | handleInput | Handles the input element's input event | | handleKeyDown | Handles the input element's keydown event | | handleBlur | Handles the input element's blur event | | handleResultMouseDown | Handles the ul element's mousedown event | | handleResultClick | Handles the ul element's click event |

Methods

| Name | Description | |:----------|:---------------------------------------------------------------------------------------| | destroy | Removes all event listeners and object references that were set during initialization. |