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

bootstrap-simple-autocomplete

v1.1.0

Published

A super simple and lightweight autocomplete component for Bootstrap 5 with remote data fetching.

Downloads

30

Readme

Bootstrap Simple Autocomplete

A super simple and lightweight autocomplete component for Bootstrap 5, designed for easy integration and remote data fetching. Created to replace the legacy Twitter's Typeahead.js when you just need a remote autocomplete.

Demo - https://osalabs.github.io/bootstrap-simple-autocomplete/

Features

  • Lightweight and easy to use
  • Supports remote data fetching via AJAX
  • Fully compatible with Bootstrap 5
  • Keyboard navigation (up/down arrows, enter, escape)
  • Replicates Typeahead.js UX behavior
  • Customizable rendering of dropdown items
  • Accessibility supported

Installation

Via npm

npm install bootstrap-simple-autocomplete

Via script tag

Include the script in your HTML file:

<script src="https://unpkg.com/bootstrap-simple-autocomplete/bootstrap-simple-autocomplete.js"></script>

Usage

Basic Usage

Add the data-autocomplete attribute to your input elements with the URL endpoint for fetching suggestions.

<input
  type="text"
  data-autocomplete="https://your-api-endpoint?q="
  class="form-control"
/>

Include the script and initialize the autocomplete (auto-initialization occurs if you include the script after the input element):

<script src="bootstrap-simple-autocomplete.js"></script>

ES6 Module Usage

Load .mjs version:

<script src="https://unpkg.com/bootstrap-simple-autocomplete/bootstrap-simple-autocomplete.mjs"></script>
import {
  BootstrapSimpleAutocomplete,
  initializeAutocomplete,
} from 'bootstrap-simple-autocomplete';

// Auto-initialize all elements with data-autocomplete attribute
initializeAutocomplete();

// Or initialize a specific element
const inputElement = document.querySelector('input[data-autocomplete]');
const autocomplete = new BootstrapSimpleAutocomplete(inputElement);

Custom Fetch Function

You can provide a custom function for fetching data, which allows integration with various data sources or additional processing.

const autocomplete = new BootstrapSimpleAutocomplete(inputElement, {
  fetchFunction: function (query) {
    // Custom data fetching logic
    return fetch(`https://your-api-endpoint?q=${encodeURIComponent(query)}`)
      .then((response) => response.json())
      .then((data) => {
        // Process data if needed
        return data;
      });
  },
});

Custom Rendering of Dropdown Items

Customize how each dropdown item is rendered by providing a renderItem function.

const autocomplete = new BootstrapSimpleAutocomplete(inputElement, {
  renderItem: function (option, query, index) {
    const item = document.createElement('a');
    item.className = 'dropdown-item';
    item.setAttribute('role', 'option');
    item.setAttribute('aria-selected', 'false');
    item.id = `autocomplete-item-${this.id}-${index}`;

    // Example: Highlight the query in the option text
    const regex = new RegExp(`(${query})`, 'gi');
    item.innerHTML = option.replace(regex, '<strong>$1</strong>');

    item.addEventListener('click', () => this.selectOption(option));
    return item;
  },
});

Event Handling

Listen for the autocomplete.select event to perform actions when a user selects an option.

inputElement.addEventListener('autocomplete.select', (event) => {
  console.log('Selected value:', event.detail.value);
  // Update your model or perform other actions
});

Options

Configure the component using data attributes or constructor options.

Data Attributes

  • data-debounce: Debounce delay in milliseconds (default: 300)
  • data-minlength: Minimum input length to trigger autocomplete (default: 1)

Constructor Options

  • debounceDelay: Number (milliseconds)
  • minQueryLength: Number
  • fetchFunction: Function for custom data fetching
  • renderItem: Function for custom rendering of dropdown items

Example:

const autocomplete = new BootstrapSimpleAutocomplete(inputElement, {
  debounceDelay: 200,
  minQueryLength: 2,
});

Accessibility

The component includes ARIA attributes for better accessibility:

  • aria-autocomplete="list"
  • aria-controls
  • aria-activedescendant

License

This project is licensed under the MIT License.