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

@topmarksdevelopment/autocomplete

v1.0.1

Published

Autocomplete user input - similar to JQuery UI Autocomplete

Downloads

3

Readme

Autocomplete (A JavaScript package)

A small package to provide an autocomplete list as a user is typing.

Links

Usage

Basic usage

To add autocomplete to an input with the class "autocomplete", use the sample code below - which will query the specified source URL and expect a JSON response.

Without specifying a type, autocomplete defaults to the generic type:
{ label: string; value: string }

// Using the default options (source is always required)
new Autocomplete(
    '.autocomplete',
    {
        source: './relative-folder/query.html'
    }
)
    // Don't forget to start it
    .start();

You can also specify the source type for some strong types!

In this example I've provided an array of inputs (that will always be returned) - this is also strongly typed!

type MyType = {
    myCustomName: string;
    myCustomValue: string;
}

// Using the custom MyType (which is now tightly-bound)
new Autocomplete<MyType>(
    '.autocomplete',
    {
        source: [
            {
                myCustomName: "One - Name",
                myCustomValue: "One - Value"
            },
            {
                myCustomName: "Two - Name",
                myCustomValue: "Two - Value"
            },
            {
                myCustomName: "Three - Name",
                myCustomValue: "Three - Value"
            },
        ]
    },
    renderers: {
        itemRenderer: ({ item }) =&gt; {
            const li = document.createElement('li');

            li.dataset.value = item.myCustomName;

            //li.innerText = item.detail; <-- This isn't in `MyType`

            li.innerText = item.myCustomValue;

            return li;
        },
    }
)
    // Don't forget to start it
    .start();

Options

source: SourceTypes<T>

The source of the autocompelte data

SourceTypes<T>

  • string
    a URL we will GET with a term querystring parameter (expects a JSON response)
  • Record set
    an object with string keys and values, treated as label, value respectively
  • string[]
    an array of strings with each item treated as both the value and label
  • T[]
    an array of the generic type (if specified)
  • function(string) => _ONE OF THE ABOVE
    a function that receives { term } and returns one of the above sets

autoFocus?: boolean

Automatically focus on the first element when the menu opens
Default: false

delay?: number

The delay (in milliseconds) between a keystroke and a search. This acts like a throttle between source calls
Default: 300

minLength?: number

Minimum term entered by the user before we query the source
Default: 2

recordMapper?: (input: Record<string, string>) => T[]

Define how an object should be mapped to the type T
Default: An array where the property name becomes label and it's value becomes value

position?: IPositionData

How shold the autocomplete box positioned
Default: { my: "top center", at: "botom center" }

renderers?: IRenderers;

Specify how we render the elements

appendTo?: HTMLElement

Append the menu to this element, instead of the body

logger?: JavascriptLogger

Log stages of the autocomplete processes

Option Methods

onClose

Called after the menu has been closed
Call:

onClose: (ev: Event, data: { ul: HTMLUListElement }) => {}

onCreated

Called after the menu has been created, but before being opened
Call:

onCreated: (ev: Event, data: { ul: HTMLUListElement }) => {}

onItemFocus

Called as soon as an item is focused, but before changing its state
Call:

onItemFocus: (
    ev: Event,
    data: {
        ul: HTMLUListElement
        item: <T>, // Your generic type, if specified
        input: HTMLInputElement
    }
) => {}

onItemSelect

Called as soon as an item is selecetd, but before changing any state
Call:

onItemSelect: (
    ev: Event,
    data: {
        ul: HTMLUListElement
        item: <T>, // Your generic type, if specified
        input: HTMLInputElement
    }
) => {}

onOpen

Called before the menu has been opened and any position data is calculated
Call:

onOpen: (ev: Event, data: { ul: HTMLUListElement }) => {}

onResponse

Called after processing the response and before creating the menu
Call:

// T is your generic type, if specified
onResponse: (ev: Event, data: { items: T[] }) => {}

onSearch

Called before processing the search
Call:

// T is your generic type, if specified
onSearch: (ev: Event, data: { term: string }) => {}

onStart

A method called after events have been added
Call:

// T is your generic type, if specified
onStart: () => {}

onStop

A method called after events have been removed
Call:

// T is your generic type, if specified
onStop: () => {}