Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@patternfly/pfe-autocomplete

Package Overview
Dependencies
Maintainers
11
Versions
74
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@patternfly/pfe-autocomplete

Autocomplete element for PatternFly Elements

  • 1.0.0-prerelease.26
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
158
increased by295%
Maintainers
11
Weekly downloads
 
Created
Source

PFElements Autocomplete Element

is a Web Component that provides options in a dropdown list as user types in an input box by showing result from an api call.

Install

npm install @patternfly/pfe-autocomplete

Once installed, import it to your application:

import '@patternfly/pfe-autocomplete.umd.js';

Usage

<pfe-autocomplete debounce="500" init-value="uni">
    <input placeholder="Enter Your Search Term"/>
</pfe-autocomplete>

Setup

autocompleteRequest autocompleteRequest is a property that is assigned a function. When user types, component calls this function.

It is called inside component but we define it outside component. First param is the typed phrase by user and second param is a callback function to send api response back to the component.

In the function, we add loading attribute then send api call. When result is ready, we remove loading attribute and pass the result to web component by calling callback function. Here is an example:

// autocomplete call
searchAutocomplete.autocompleteRequest = function(params, callback) {
  var xhr = new XMLHttpRequest();

  searchAutocomplete.setAttribute('loading', '');
  xhr.onload = function() {
    searchAutocomplete.removeAttribute('loading');

    if(xhr.status === 404) {
      callback([]);
    } else {
      const response = JSON.parse(xhr.responseText);
      const regx = new RegExp("\^" + params.query, "i");

      var responseReady = response.reduce(function(acc, item) {
        if (regx.test(item.name)) acc.push(item.name);
        return acc;
      }, []);
      callback(responseReady);
    }
  };

  const url = 'https://restcountries.eu/rest/v2/name/' + params.query;

  xhr.open('GET', url, true);
  xhr.send();
};

debounce This attribute is optional. By default, It has been set tp 300ms. User may type very fast. We allow to input box value changes trigger autocomplete api call each 300ms.

loading loading is a boolean attribute. If you add this attribute on element a loading icon is added in input box. Add this attribute before autocomplete api call and remove this attribute form element when api call response is ready.

init-value Set this attribute when you want to set a value in input box when web component is added to page.

is-disabled is-disabled is a boolean attribute. Add this attribute to element when you want to make the element disabled. By adding this attribute input box and buttons become disabled.

Get selected item

User can select an item by clicking on search button, type press enter or select an item by using keyboard and press enter. The selected item can be captured by listening to an event(pfe-search-event) or observing attribute(selected-value) change.

pfe-search-event

When user performs search, pfe-search-event event is fired. By listening to this event you can get selected phrase by getting e.detail.searchValue.

searchAutocomplete.addEventListener('pfe-search-event', function(e) {
  console.log('do search= ' + e.detail.searchValue);
});

selected-value attribute

By observing selected-value attribute you can detect autocomplete selected value.

Dependencies

Make sure you have Polyserve and Web Component Tester installed.

npm install -g polyserve web-component-tester

Dev

npm start

Test

npm run test

Build

npm run build

Demo

Run npm start and Polyserve will start a server and open your default browser to the demo page of the element.

Code style

Autocomplete (and all PFElements) use Prettier to auto-format JS and JSON. The style rules get applied when you commit a change. If you choose to, you can integrate your editor with Prettier to have the style rules applied on every save.

Keywords

FAQs

Package last updated on 25 Oct 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc