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

bonanza

v2.1.4

Published

Asynchronous autocomplete with infinite scroll

Downloads

148

Readme

Bonanza Build Status Greenkeeper badge

Customizable autocomplete for really large collections

See examples here!

Example page

Install

Use it as an npm package:

npm install bonanza --save

Or just download it from bower

bower install bonanza --save

Angular support

Just use this package instead

Usage

You just need an input to work on and a function (a.k.a. callback) that returns your favorite data

bonanza(element, options?, callback | list)

  • element (required): a HTMLInput Javascript element that will be used for autocompletion
  • options (optional): a set of parameters to customize bonanza as needed
  • callback | list (required): bonanza doesn't know how to get your data, so it needs a function that receives a query, and returns the rows as an Array that will be used to fill the autocomplete info, or even an array with all the results that you need to display.

The example below shows you how to easily set up bonanza for an input element:

bonanza(element, function (query, callback) {
  get('/someApi?query=' + query.search + '&offset=' + query.offset + '&limit=' + query.limit, function (err, data) {
    if (err) {
      callback(err);
      return;
    }
    
    callback(null, data);
  });
});

The options are detailed below:

options.templates

An object with a set of functions. Here you have a detailed table with all that you need to replace:

Property | Default | Usage ---------- | -------------------------------------------- | --------------------------------------- itemLabel | (obj) => obj | List items item | ...customCode | Custom HTML for list item label | (obj) => obj | Input value when user selects some item isDisabled | (obj) => false | Marks an item as disabled noResults | (search) => \No results for "${search}"`| Showed when query returns no results loadMore |... | Showed at bottom when there is more data to show loading |Loading ...` | Showed at bottom when loading more data

options.css

An object with a set of CSS class names. The detailed list of css classes by default can be found below:

Property | Default | Purpose ------------- | ----------------------- | --------------------------------------- container | bz-container | The div class for the main container hide | bz-hide | Class to hide the container list | bz-list | The class for the ul inside the container item | bz-list-item | Every li element for the results disabled | bz-list-item-disabled | A disabled element in the list selected | bz-list-item-selected | When an item is being selected loading | bz-list-loading | The "loading" li item loadMore | bz-list-load-more | The "load more" li item noResults | bz-list-no-results | The li item showed when no results for last search inputLoading | bz-loading | A class for the input element when loading match | bz-text-match | A class when the search matches the text on an item

options.openOnFocus

A boolean that decides whatever the list will be open on focus event or not. true by default.

options.showLoading

If true it will show a loading text when bonanza does the first search (with the list empty). true by defualt.

options.showLoadMore

If true it will show a "load more" legend text when bonanza has more items to show. true by defualt.

options.limit

The max number of rows expected, 10 by default.

options.scrollDistance

The distance in px before the bottom that will make bonanza start loading more items. 0 by default.

options.hasMoreItems

A function that helps to decide whatever the list being accesed has more items to display or not. By default is this function:

function (result) { return !!result.length && result.length === this.limit; }

options.getItems

This will be used to parse what you send from your callback, to the array that bonanza expects as a result. It contains the following function by default:

function (result) { return result; }

API

The object being returned after initializing bonanza is an EventEmitter instance. bonanza emits a set of events detailed below:

Event | Definition ------- | --------------------------------------- focus | When the user made focus on the input element change | Every time the user picks an option select | Every time the user selects an option, usually navigating the list with the arrow keys cancel | Every time the user cancels the operation, usually touching the esc key open | When something opens the items list close | When the user or an event closes the list search | When starting to do a search success | When the search returns data error | Every time an error sadly occurs

Contributing

The dist folder as well as the version in the package.json file should not be modified. Create a PR with your changes and if needed a test asserting them. Once merged a new version will be uploaded to bower and npm with the dist folder updated.

License

MIT