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

@appbaseio/autocomplete-suggestions-plugin

v1.1.1-alpha

Published

A suggestions plugin for autocomplete-js, backed by appbase client.

Downloads

76

Readme

Version MIT License

@appbaseio/autocomplete-suggestions-plugin

The @appbaseio/autocomplete-suggestions-plugin is a Suggestions plugin that adds Query Suggestions powered by appbase-js client, to your autocomplete.

Installation

yarn add @appbaseio/autocomplete-suggestions-plugin
# or
npm install @appbaseio/autocomplete-suggestions-plugin

Usage

To get started, you need a container for your autocomplete to go in. If you don't have one already, you can insert one into your markup:

<div id="autocomplete"></div>

Then, insert your autocomplete into it by calling the autocomplete function and providing the container. It can be a CSS selector or an Element.

Import the @appbaseio/autocomplete-suggestions-plugin plugin utility and create a suggestions plugin by passing appbase-js client config and the query config to fetch suggestions. Additionally, you can pass the third argument for UI customisation.

As a final step, pass this plugin to the plugins property while calling autocomplete function.

Make sure to provide a container (e.g., a div), not an input. Autocomplete generates a fully accessible search box for you.

import { autocomplete } from '@algolia/autocomplete-js';
import createSuggestionsPlugin from "@appbaseio/autocomplete-suggestions-plugin";

// appbase client config object
const appbaseClientConfig = {
  url: "https://appbase-demo-ansible-abxiydt-arc.searchbase.io",
  app: "best-buy-dataset",
  credentials: "b8917d239a52:82a2f609-6439-4253-a542-3697f5545947",
};

// reactivesearch api configuration
const rsApiConfig = {
  enableRecentSuggestions: true,
  enablePopularSuggestions: true,
  recentSuggestionsConfig: {
    size: 5,
    minChars: 5,
  },
  popularSuggestionsConfig: {
    size: 5,
    showGlobal: true,
  },
  size: 5,
};

const suggestionsPlugin = createSuggestionsPlugin(appbaseClientConfig, {
  ...rsApiConfig,
});

autocomplete({
  container: '#autocomplete',
  plugins: [suggestionsPlugin],
  // ...
});

Click here to checkout the advanced example to see all properties in action. autocomplete-plugin-example

Documentation

  1. appbaseClientConfig Object Required

    It is the first parameter accepted by createSuggestionsPlugin(), used for connecting to the appbase client. It accepts the following properties:

    • url String Required

    • app String Required name of the index as displayed in the dashboard

    • username String username as displayed in the access control dashboard

    • password String password as displayed in the access control dashboard

    • credentials String Required API key to access the cluster.

      credentials are not required if, url already contains it.

    • enableTelemetry Booleanwhen set to false, it disables telemetry. Defaults to true.

    • settings Object an object consisting of the properties to control your search experience. Read more here.

  2. queryConfig Object Required

    It is an object representing a ReactiveSearch query. Read about the properties accepted in this object here.

  3. renderConfig Object

    Although the plugin already comes with a default UI interface, One can customize the UI/ UX according to their needs using the renderConfig object**.**

    It accepts the following properties:

    • renderItem Function

      It is a callback that accepts parameters, one of them is the suggestion item itself, utilize it to render a custom UI for every suggestion.

      createSuggestionsPlugin(
          ...,
          ...,
          {
              renderItem: (props) => {
                  const { item, setQuery, refresh } = props;
                  if (item.type === "index") {
                  return (
                      <a
                      className="aa-item product-item"
                      href={item._source.url}
                      target="_blank"
                      rel="noreferrer"
                      >
                          <div className="product-image">
                          <img src={item._source.image} alt={item.value} />
                          </div>
                          <div className="product-details">
                              <h4>{item.value}</h4>
                              <p>{item._source.shortDescription}</p>
                          </div>
                      </a>
                      );
                  }
              },
          }
      )
    • onItemSelect Function

      It is a callback that accepts parameters(algolia control params), one of them is the setQuery function, utilize it to customize the behavior of what happens when an individual suggestion item is clicked.

      createSuggestionsPlugin(
          ...,
          ...,
          {
              renderItem: (props) => {
                  ...
              },
              onItemSelect: (props) => {
                  const {
                      item: { url, label },
                      setQuery,
                      refresh
                  } = props;
            
                  if (url) {
                      setQuery(label);
                      window.open(url);
                  } else {
                      setQuery(label);
                      refresh();
                  }
              },
          }
      )
    • renderHeader Function

      It is a callback that accepts parameters(algolia params), one may utilize it to render a custom header before the suggestions.

      createSuggestionsPlugin(
          ...,
          ...,
          {
              renderItem: (props) => {
                  ...
              },
              onItemSelect: (props) => {
                  ...
              },
              renderHeader: (props) => {
                  return (
                      <h4>
                       Products Listing <hr style={{ borderColor: "#d7d5f5" }} />
                      </h4>
                  );
              },
          }
      )
    • renderFooter Function

      It is a callback that accepts parameters(algolia params), one may utilize it to render a custom footer after the suggestions.

      createSuggestionsPlugin(
          ...,
          ...,
          {
              renderItem: (props) => {
                  ...
              },
              onItemSelect: (props) => {
                  ...
              },
              renderHeader: (props) => {
                  ...
              },
              renderFooter: (props) => {
                  return <hr style={{ borderColor: "#d7d5f5" }} />;
              },
          }
      )
    • renderNoResults Function

      It is a callback that accepts parameters(algolia params), one may utilize it to render a custom UI when no results are found.

      createSuggestionsPlugin(
          ...,
          ...,
          {
              renderItem: (props) => {
                  ...
              },
              onItemSelect: (props) => {
                  ...
              },
              renderHeader: (props) => {
                  ...
              },
              renderFooter: (props) => {
                  ...
              },
              renderNoResults: (props) => {
                  if (props.state.query === "") {
                      return <p>Search for something to get direct product suggestions!</p>;
                  } else {
                      return <p>No products found! Try searching for something else!</p>;
                  }
              }
          }
      )
    • useContextValue Boolean When set to true, the context value is set with the following value:

      {
          total: ...,           // total results found for the entered query
          time: ...,            // total time taken to perform the search
          resultsJson: ...      // the results that were found in json format
      }

      One can use this context value to display results stats.

      createSuggestionsPlugin(
          ...,
          ...,
          {
              renderItem: (props) => {
                  ...
              },
              onItemSelect: (props) => {
                  ...
              },
              renderHeader: (props) => {
                  ...
              },
              renderFooter: (props) => {
                  ...
              },
              renderNoResults: (props) => {
                  if (props.state.query === "") {
                      return <p>Search for something to get direct product suggestions!</p>;
                  } else {
                      return <p>No products found! Try searching for something else!</p>;
                  }
              },
              useContextValue: true
          }
      )