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

@algolia/autocomplete-js

Package Overview
Dependencies
Maintainers
82
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@algolia/autocomplete-js

Fast and fully-featured autocomplete JavaScript library.

  • 1.17.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
124K
decreased by-3.62%
Maintainers
82
Weekly downloads
 
Created

What is @algolia/autocomplete-js?

@algolia/autocomplete-js is a JavaScript library that provides a highly customizable autocomplete experience. It is designed to be flexible and easy to integrate with various data sources, including Algolia's search API. The package allows developers to create powerful and responsive autocomplete interfaces with minimal effort.

What are @algolia/autocomplete-js's main functionalities?

Basic Autocomplete Setup

This code sets up a basic autocomplete interface with a static list of items. The `autocomplete` function initializes the autocomplete widget, and the `getSources` method defines the data sources for the autocomplete suggestions.

```javascript
import { autocomplete } from '@algolia/autocomplete-js';

autocomplete({
  container: '#autocomplete',
  getSources() {
    return [
      {
        sourceId: 'example-source',
        getItems() {
          return [
            { label: 'Item 1' },
            { label: 'Item 2' },
            { label: 'Item 3' }
          ];
        },
        templates: {
          item({ item }) {
            return item.label;
          }
        }
      }
    ];
  }
});
```

Using Algolia Search API

This example demonstrates how to integrate the autocomplete widget with Algolia's search API. The `getItems` method performs a search query using Algolia's client and returns the search results as autocomplete suggestions.

```javascript
import { autocomplete } from '@algolia/autocomplete-js';
import algoliasearch from 'algoliasearch/lite';

const searchClient = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');

autocomplete({
  container: '#autocomplete',
  getSources() {
    return [
      {
        sourceId: 'products',
        getItems({ query }) {
          return searchClient
            .initIndex('products')
            .search(query)
            .then(({ hits }) => hits);
        },
        templates: {
          item({ item }) {
            return item.name;
          }
        }
      }
    ];
  }
});
```

Customizing Autocomplete Templates

This code sample shows how to customize the templates used for rendering autocomplete suggestions. The `item` template is modified to include both a label and a description for each suggestion.

```javascript
import { autocomplete } from '@algolia/autocomplete-js';

autocomplete({
  container: '#autocomplete',
  getSources() {
    return [
      {
        sourceId: 'example-source',
        getItems() {
          return [
            { label: 'Item 1', description: 'Description 1' },
            { label: 'Item 2', description: 'Description 2' },
            { label: 'Item 3', description: 'Description 3' }
          ];
        },
        templates: {
          item({ item }) {
            return `<div>
                      <strong>${item.label}</strong>
                      <p>${item.description}</p>
                    </div>`;
          }
        }
      }
    ];
  }
});
```

Other packages similar to @algolia/autocomplete-js

FAQs

Package last updated on 05 Nov 2024

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