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

etto

v1.6.14

Published

small, agnostic autocomplete & select component

Downloads

13

Readme

etto

A small, customizable autocomplete and select component. Some highlights:

  • Lightweight (~3.3kb min+gzip)
  • Zero dependencies
  • View-library agnostic
  • Select mode included
  • Support for user-defined callbacks
  • Use the included filtering mechanism or bring your own!

Etto only supports modern browsers. It is not intended to work with IE11.

This a replacement for my earlier component, otto. I wanted to get rid of the Hyperapp dependency, but also simplify the codebase and features, hence the new name and project. Etto is also named after a friend of mine. I advise you use etto over otto.

Etto may or may not be the tool you're looking for. See the other tools section.

Install

npm install etto

Node

import Etto from 'etto';

A CommonJS export is available under /dist/cjs/.

Browser

<script src="https://unpkg.com/etto/dist/etto.min.js"></script>

Styles

<link rel="stylesheet" href="https://unpkg.com/etto/dist/etto.css">

Usage

See Live Examples.

Basic usage

<div id="my-input"></div>
const choices = [
    { label: 'Alabama', value: 'AL', country: 'USA' },
    { label: 'Alaska', value: 'AK', isCold: true },
    { label: 'Wyoming', value: 'WY', population: 578759 },
    { label: 'Minnesota', value: 'MN', capital: 'Saint Paul' }
];

const etto = new Etto(document.getElementById('my-input'), {}, choices);
  • choice.label (Required)
    • what is shown for each item in the dropdown.
  • choice.value (Optional, defaults to value of choice.label)
    • what your input element will be populated with upon selection.
  • choice.country, choice.isCold, choice.population, choice.capital
    • these are examples of custom properties. Add/use whatever you'd like!

By default, Etto matches against choice.label only. You can provide a config.filterFn function for a custom filtering mechanism. You can also configure etto to use functions such as config.onSelect and config.createItemFn which are great for taking advantage of your custom properties (read more below & see live examples).

Select mode

etto can imitate a dropdown selection by enabling the built-in select mode service.

const etto = new Etto(document.getElementById('my-input'), { selectMode: true }, choices);

QoL methods & properties

Use etto.value to get/set current input value.

const oldValue = etto.value;
console.log(oldValue); // 'old value'

etto.value = 'new value';
console.log(etto.value); // 'new value'

Use etto.selected to get current selected choice when using select mode.

const currentChoice = etto.selected;
console.log(currentChoice); // { label: 'Wyoming', value: 'WY', population: 578759 }

Warning: The etto.selected getter returns a reference to etto.service.state.selected. Mutating this object can result in unexpected behavior, so be cautious!

Use etto.clear to clear current input value (it also clears etto.selected when in select mode).

etto.clear();
console.log(etto.value); // ''
console.log(etto.selected); // null

Use etto.destroy to remove all event listeners and delete the service of the Etto instance.

etto.destroy();

Several more lower-level methods are available under etto.service. Proceed with caution.

Configuration

etto provides several options to configure its behavior. Below are default values:

const etto = new Etto(document.getElementById('my-input'), {
    // selectMode: boolean
    // whether to enable select mode
    selectMode: false,

    // classList: array
    // additional classes to add to the etto-input element
    classList: undefined,

    // placeholder: string,
    // custom placeholder. defaults to 'Select...' in select mode
    placeholder: undefined,

    // inputAttributes: object
    // any additional attributes you'd like to apply to the etto-input element. be aware that overwriting the `style` attribute may break Etto functionality
    inputAttributes: undefined,

    // source(query: string, done: function): void
    // for use with async choice sources; see live examples page
    source: undefined,

    // minChars: number
    minChars: 3,

    // maxResults: number
    maxResults: 7,

    // requestDelay: number
    // used in conjunction with config.source; delay before making each request in milliseconds
    requestDelay: 350,

    // matchFullWord: boolean
    // whether to match full words
    matchFullWord: false,

    // showEmptyMsg: boolean
    // whether to display empty message upon no match
    showEmptyMsg: true,

    // emptyHtml: string
    // custom html markup for empty message
    emptyHtml: undefined,

    // initialCache: object
    // used in conjunction with config.source;
    // in case you already have some cached data you'd like to initialize with Etto
    initialCache: {},

    // createItemFn(choice: object, index: number, inputVal: string, isHighlighted: boolean, isSelected: boolean): string
    // customize how each list item is rendered
    // Note: The rendered item *must* be an `li` element with a `data-index` attribute; see live examples page for a detailed explanation (https://kevinfiol.com/etto/#createItemFn)
    createItemFn: undefined,

    // filterFn(inputVal: string, choices: array, matchFullWord: boolean, maxResults: number): array
    // customize how etto filters results; see live examples page (https://kevinfiol.com/etto/#filterFn)
    // defaults to using the included filter function, /src/util.js > filterChoices
    filterFn: filterChoices,

    // onSelect(choice: object): void
    // a custom callback you can use to do something upon selecting an item from the list
    // takes in object of selected choice
    onSelect: undefined,

    // onClear(): void
    // a custom callback you can use to do something upon programmatically clearing the user input
    onClear: undefined,

    // onValue(value: string): void
    // a custom callback you can use to when the input value has changed
    // primarily for use in input mode; might behave unexpectedly when using select mode
    onValue: undefined
});

Other Tools

Etto aims to be small in size and scope, while remaining very customizable. That being said, here are some other tools that I can recommend:

  • Awesomeplete - A truly awesome autocomplete component that's even smaller than Etto (2kb). Zero dependencies.
  • select2 - Tried & battle-tested. Has a jQuery dependency.
  • Choices.js - A select2 alternative without the jQuery dependency. 19kb gzipped.
  • react-select - As the name implies, a select component with a hard React dependency.