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

search-suggestion

v1.0.3

Published

Simple, lightweight, flexible search suggestion, autocomplete component

Downloads

159

Readme

Build Status codecov npm version License: MIT PRs Welcome

Search Suggestion

Simple lightweight search suggestion component. This component was inspired by downshift (paypal) component.

The component applied Function as Child Components pattern. You can read more about this pattern here

Table content

Installation

npm install --save search-suggestion

or

yarn add search-suggestion

Usage

You can check out the basic demo here: https://codesandbox.io/s/n45j5zjwyj

import React, { Component } from 'react';
import { render } from 'react-dom';
import Suggestion from 'search-suggestion';

const items = ['apple', 'pear', 'orange', 'grape', 'banana'];

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      currentData: []
    };
  }

  createData = (word, data) => {
    const re = new RegExp(`${word.toLowerCase()}.*\\B`, 'g');
    return data.filter(item => re.test(item.toLowerCase()));
  };

  handleChange = e => {
    const value = e.target.value;
    let filterData = [];
    if (value) {
      filterData = this.createData(value, items);
    }
    this.setState({
      currentData: filterData
    });
  };

  render() {
    return (
      <Suggestion
        getDisplayName={item => item}
        items={this.state.currentData}
        onSelectedItem={item => alert(item)}
      >
        {({
          getInputProps,
          getListItemProps,
          getItemProps,
          inputValue,
          selectedItem,
          highlightedIndex,
          items,
          isOpen,
          clearInputValue
        }) => (
          <div>
            <p>selected item: {selectedItem}</p>
            <input
              {...getInputProps({
                placeholder: 'Select fruit',
                onChange: this.handleChange
              })}
            />
            {isOpen && (
              <div {...getListItemProps()}>
                {items.map((item, index) => (
                  <div
                    {...getItemProps({ item, index })}
                    key={item.code}
                    style={{
                      backgroundColor:
                        highlightedIndex === index
                          ? 'rgb(232, 232, 232)'
                          : 'white',
                      fontWeight:
                        selectedItem && selectedItem === item
                          ? 'bold'
                          : 'normal'
                    }}
                  >
                    {item}
                  </div>
                ))}
              </div>
            )}
          </div>
        )}
      </Suggestion>
    );
  }
}

render(<App />, document.getElementById('root'));

Examples

Props

items

Array | default is an empty array

Use as a datasource to render list result.

getDisplayName

function(item: any) => return a text

Use to display text content of selected item.

onSelectedItem

function(item: any)

Use to set new selected item value and extend the actions based on that selected item.

Child callback functions

getInputProps

function({})

Returns the props you should apply to the input element that you render.

getListItemProps

function({})

Returns the props you should apply to the list item element that you render.

getItemProps

function({})

Returns the props you should apply to any menu item elements you render.

Actions

clearInputValue

function()

Clear current value in input element

States

inputValue

string

The current value of input element

highlightedIndex

int

The current index of highlighted item

items

array

The current data of menu

isOpen

boolean

The menu open state

selectedItem

The value of selected item

Showcase

Websites built with Search Suggestion component

Chợ tốt

suggestion

Chợ tốt nhà

new_property_suggestion

Alternatives

If you don’t agree with the choices made in this project, you might want to explore alternatives with different tradeoffs. Some of the more popular and actively maintained ones are: