Socket
Book a DemoInstallSign in
Socket

plete

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

plete

A vanilla js auto complete component

Source
npmnpm
Version
0.2.0
Version published
Weekly downloads
5
-28.57%
Maintainers
1
Weekly downloads
 
Created
Source

plete

CircleCI codecov npm

A vanilla js autocomplete component that supports remote filtering.

npm install plete --save

Objectives

  • Good WAI-ARIA support
  • Support multiple input types: keyboard, mouse, touch
  • Support local and remove filtering (async function as datasource)
  • Custom rendering of elements
  • Excellent test coverage

Compatibility

This project is aiming for wide compatibility in modern and supported browsers.

This means that no versions of IE, or legacy versions of Safari (before 10) are supported.

If you have to support legacy browsers in your project, you likely already have a transpiling setup and know how to use polyfills.

eslint-plugin-compat is used to ensure contributors are aware when they introduce compatibility issues.

Examples

Simple values

The very basic example allows the user to choose from simple string values.

<!-- this is the input the user will type in -->
<input type="text" name="plete-country" />

<!-- this is where the user's choice will end up -->
<input type="hidden" name="country" />
import Plete from "./dist/main-esm.js";

const input = document.querySelector("#input[name='plete-country'])";
const hidden = document.querySelector("#input[name='country'])";

const plete = new Plete({
  input: input,
  dataSrc: ["Denmark", "Germany", "Spain", "Sweden", "United Kingdom"],
  select: function(value) {
    hidden.textContent = value;
  }
});

Complex values

The most likely use case, is that you will need to display more complex values as suggestions.

The schema for complex values is as follows: an Object with id and label properties (strings). Other properties are allowed but not used (unless you use them in a custom renderer).

// example data item
const DNK = {
  // mandatory properties
  id: "DNK",
  label: "Denmark",

  // extra properties
  icon: "http://example.com/path/to/flag/icon"
};
<!-- this is the input the user will type in -->
<input type="text" name="plete-country" />

<!-- this is where the user's choice will end up -->
<input type="hidden" name="country" />
import Plete from "./dist/main-esm.js";

const input = document.querySelector("#input[name='plete-country'])";
const hidden = document.querySelector("#input[name='country'])";

const plete = new Plete({
  input: input,
  dataSrc: [
      { id: "BEL", label: "Belgium" },
      { id: "DNK", label: "Denmark" },
      { id: "GER", label: "Germany" },
      { id: "MCO", label: "Monaco" },
      { id: "SRB", label: "Serbia" },
      { id: "ESP", label: "Spain" },
      { id: "SWE", label: "Sweden" },
      { id: "GBR", label: "United Kingdom" },
      { id: "USA", label: "United States of America" }
  ],
  select: function(value) {
    hidden.textContent = value;
  }
});

Remote data and filtering

You can use remote data and filtering by providing an async function as the dataSrc option.

<!-- this is the input the user will type in -->
<input type="text" name="plete-country" />

<!-- this is where the user's choice will end up -->
<input type="hidden" name="country" />
import Plete from "./dist/main-esm.js";

const input = document.querySelector("#input[name='plete-country'])";
const hidden = document.querySelector("#input[name='country'])";

const plete = new Plete({
  input: input,
  dataSrc: async function filterCountries(query) {
    const response = await fetch(`https://restcountries.eu/rest/v2/name/${query}`);
    const result = await response.json();

    // make sure we always return an Array
    if (!Array.isArray(result)) {
      return [];
    }

    // map the result into the supported format
    return result.map(function(v) {
      return {
        id: v.alpha3Code,
        label: v.name
      }
    });
  },
  select: function(value) {
    hidden.textContent = value;
  }
});

Custom rendering of options

When you need custom rendering of options, you can supply a function as the render option.

render is expected to produce a string, which will be used inside the plete-item element (added using .innerHTML).

<!-- this is the input the user will type in -->
<input type="text" name="plete-country" />

<!-- this is where the user's choice will end up -->
<input type="hidden" name="country" />
import Plete from "./dist/main-esm.js";

const input = document.querySelector("#input[name='plete-country'])";
const hidden = document.querySelector("#input[name='country'])";

const plete = new Plete({
  input: input,
  dataSrc: [
      { id: "BEL", label: "Belgium" },
      { id: "DNK", label: "Denmark" },
      { id: "GER", label: "Germany" },
      { id: "MCO", label: "Monaco" },
      { id: "SRB", label: "Serbia" },
      { id: "ESP", label: "Spain" },
      { id: "SWE", label: "Sweden" },
      { id: "GBR", label: "United Kingdom" },
      { id: "USA", label: "United States of America" }
  ],
  // this function will be called for each value from `dataSrc` option
  render: function(item) {
    return `<b>${item.id}</b> ${item.label}`;
  },
  select: function(value) {
    hidden.textContent = value;
  }
});

Options

OptionDefault valueDescription
dataSrcundefinedAn array or async function with values
selectundefinedA callback to call, when a value is selected
autoFirsttrue(optional) When set, the first suggestion will be automatically highlighted
busyundefined(optional) A callback to call before filtering
cssClassundefined(optional) One or more CSS classes to add to the containing plete-list element
maxItems5(optional) The number of suggestions to display
readyundefined(optional) A callback to call after filtering
minChars3(optional) The number of characters required before searching (when using remote)
renderundefined(optional) A callback to render individual values

FAQs

Package last updated on 17 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts