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

react-otocomplete

v3.0.0

Published

higher order react autocomplete component

Downloads

12

Readme

Higher order React autocomplete component

DEMO

npm install react-otocomplete

Basic Usage

import React from 'react';
import AutoComplete from '../src/AutoComplete';
import ReactDOM from 'react-dom';

let Index = React.createClass({
  getInitialState() {
    return {
      value: ''
    };
  },
  /**
   * function when an item selected on Autocomplete
   * @param  {Object} selectedItem object containing selected item will be { value: SOURCE_ARRAY_ITEM }
   * @return you may need to set it as input value
   */
  handleSelect(selectedItem) {
    this.setState({
      'value': selectedItem.value
    });
  },

  render() {
    let wrapperStyle = {
      textAlign: 'center',
      marginTop: 42
    };

    // source is array of strings you want to list
    let source = ['Arthur C. Clarke', 'Philip K. Dick', 'Robert A. Henlein', 'Ursula K. Leguin', 'Kim Stanley Robinson', 'Isaac Asimov'];

    /*
     * itemFn will be created while rendering auto complete list
     *
     * <ul>
     *   React.createElement(itemFn, {...props})
     * </ul>
     *
     * this may be a pure function or a React class
     *
     * notice props object in function;
     * prop.highlighted will be ture if item is currently hovered or navigated with up/down keys otherwise undefined
     * props.value will be selected string
     */
    let itemFn = (props) => <div style={{ padding: 8,
      background: props.highlighted ? '#9b4dca' : 'white', // notice the prop higlighted, it will be true if item is currently hovered
      color: props.highlighted ? '#fff' : '#000',  }}>
      {props.value}
    </div>;

    return (
      <div style={wrapperStyle}>
        <AutoComplete
          source={source}
          itemComponent={itemFn}
          onSelect={(item) => this.handleSelect(item)}
          width={400}
        >
          {
            /*
              put your input as you like, this plugin does not create it but binds key events
            */
          }
          <input
            style={{ border: '2px solid #e5e5e5', width: 400, padding: 12, outline: 'none', height: 42 }}
            type="text"
            onChange={(e) => { this.setState({ value: e.target.value }); }}
            value={ this.state.value }
            placeholder="type your favorite Sci-Fi author"
          />
        </AutoComplete>

      </div>
    );
  }
});

ReactDOM.render(<Index />, document.getElementById('app'));

Advanced Usage

import React from 'react';
import AutoComplete from '../src/AutoComplete';
import ReactDOM from 'react-dom';

let Index = React.createClass({
  getInitialState() {
    return {
      value: ''
    };
  },

  /**
   * function when an item selected on Autocomplete
   * @param  {Object} selectedItem object is the selected object in the array itself {...OBJECT_IN_THE_ARRAY}
   * in this example it will be { name: AUTHOR_NAME }
   * @return you may need to set it as input value
   */
  handleSelect(selectedItem) {
    this.setState({
      'value': selectedItem.name
    });
  },
  render() {
    let wrapperStyle = {
      textAlign: 'center',
      marginTop: 42
    };

    let source = [{
      name: 'Arthur C. Clarke',
    }, {
      name: 'Philip K. Dick'
    }, {
      name: 'Robert A. Henlein'
    }, {
      name: 'Ursula K. Leguin'
    }, {
      name: 'Kim Stanley Robinson'
    }, {
      name: 'Isaac Asimov'
    }];

    /*
     * itemFn will be created while rendering auto complete list
     *
     * <ul>
     *   React.createElement(itemFn, {...props})
     * </ul>
     *
     * this may be a pure function or a React class
     *
     * notice props object in function;
     * prop.highlighted will be ture if item is currently hovered or navigated with up/down keys otherwise undefined
     * props will be selected object itself in this example it is { name: AUTHOR_NAME }
     */
    let itemFn = (props) => <div style={{ padding: 8,
      background: props.highlighted ? '#9b4dca' : 'white', // notice the prop higlighted, it will be true if item is currently hovered
      color: props.highlighted ? '#fff' : '#000',  }}>
      {props.name}
    </div>;

    // you can also pass keys as those will be used as fuse.js options (https://github.com/krisk/Fuse)
    // and can nested too like ['author.name']
    let keys = ['name'];

    return (
      <div style={wrapperStyle}>
        <AutoComplete
          source={source}
          itemComponent={itemFn}
          onSelect={(item) => this.handleSelect(item)}
          width={400}
          keys={keys}
        >
          {
            /*
              put your input as you like, this plugin does not create it but binds key events
            */
          }
          <input
            style={{ border: '2px solid #e5e5e5', width: 400, padding: 12, outline: 'none', height: 42 }}
            type="text"
            onChange={(e) => { this.setState({ value: e.target.value }); }}
            value={ this.state.value }
            placeholder="type your favorite Sci-Fi author"
          />
        </AutoComplete>

      </div>
    );
  }
});

ReactDOM.render(<Index />, document.getElementById('app'));