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

autocomplete-tools

v1.0.4

Published

A React Typescript module that provides autocomplete components. It requires Bootstrap > 4.x

Downloads

13

Readme

autocomplete-tools

A React Typescript module that provides autocomplete components. It requires Bootstrap > 1.0

How to install:

npm install autocomplete-tools

How to include

Code is available in javascript and TypeScript versions

Step 1: Import the code

import { AutoComplete, AutoCompleteAjax } from "autocomplete-tools";

Step 2: Import the styles:

Make sure you include the styles in your project. Versions in css and scss are available

  • css: autocomplete-tools/css/styles.css
  • scss: autocomplete-tools/scss/styles.scss

NOTE: If you use the scss version, then make sure you include bootstrap.scss first to make sure the color variables are defined.

To use

Add the component to a Form as you would do with any other React-Boostrap form control

  • AutoComplete: Component to autocomplete from elements from an in memory array
  • AutoCompleteAjax: Component to autocomplete from elements received from an external API via AJAX

Javascript example:

<Form>
    <Form.Group controlId="test2-id">
        <Form.Label>Array example</Form.Label>
        <AutoComplete data={CountriesList} onSelect={doSomething} initialText={initialText} keyString={"name"} customRender={customRender}/>
    </Form.Group>
    <Form.Group controlId="test1-id">
        <Form.Label>Select Airport</Form.Label>
        <AutoCompleteAjax query={ajaxQuery} onSelect={(x: AirportTable) => setX(x)} initialText={""} keyString={"name"} customRender={renderAirport} placeholder="Type to search"/>
    </Form.Group>
</Form>

Typescript Example: You can inforce type by using the operators <>

Step 1: Define the type for your own data:

interface Employee {
    id: number;
    name: string;
    lastname: string;
}

Step 2: Use the type when creating the component

<Form>
    <Form.Group controlId="test2-id">
        <Form.Label>Array example</Form.Label>
        <AutoComplete<Employee> data={CountriesList} onSelect={(country: Employee) => setcountry(country)} initialText={initialText} keyString={"name"} customRender={customRender}/>
    </Form.Group>
    <Form.Group controlId="test1-id">
        <Form.Label>Select Airport</Form.Label>
        <AutoCompleteAjax<Employee> query={ajaxQuery} onSelect={(x: Employee) => setX(x)} initialText={""} keyString={"name"} customRender={renderAirport} placeholder="Type to search"/>
    </Form.Group>
</Form>

Props

  • data: An array containing the autocompare available values [{id:123, name: "some text", other:"""}, ...]
  • onSelect: A call back function that will be called when an element is selected: (item) => dosomething(item)
  • initialText: The text that will be show when you first show the element
  • keyString: The name of the key that provides the string to search
  • customRender: Optional parameter. By default, the value of string is rendered as text. You can create a custom render for the items in the list
  • placehoder: The text to display when the input is empty
  • query: a function that returns the paramteters required to perform the ajax query.

Call back Examples

Example of AjaxQuery:

    const ajaxQuery = (q) => {
      const queryString = "https://demo.luciad.com/airports/search/?limit=10&q="+q;
      const headers = {};
      return {
          queryString,
          headers
      }
    }
  • Note: Internally AutoCompleteAjax uses 'fetch', so the parameters passed in the return are compatibles with it.

Example for custom render:

const customRender = (item) => (
      <>
          <p className="mb-0 font-weight-bold line-height-1">
              {item.name}{" "}
              <img src={item.flag} alt="flag" style={{ width: "30px" }} />
          </p>
          <Badge variant="primary">{item.region}</Badge>
          <Badge variant="secondary">{item.capital}</Badge>
      </>
  )

Code sandbox

A small code sandbox can be found here: https://codesandbox.io/s/autocomplete-tools-08l5h