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-autocomplete-input-component

v2.0.16

Published

React autocomplete input that is accessible, customizable, and easy to implement.

Downloads

28

Readme

React Autocomplete Input Component

  import { AutoComplete } from 'react-autocomplete-input-component';

  <AutoComplete
      list={[1, 'one', 2, 'two', 3, 'three']}
      handleHighlight={(highlightedItem) => {
        console.log(highlightedItem)
      }}
      onSelect={(selectedItem) => {
        console.log(selectedItem)
      }}
  />

Demo

Check out more examples

Install

npm

npm install --save react-autocomplete-input-component

Props

list: array

  • array of the values to be searched for a match to the user input
  • getDisplayValue is needed if list array contains objects
  • can be passed in as an empty array and can always be changed dynamically

getDisplayValue: function (Optional)

  • only needed if list contains objects
  • function used to filter out the property value to be displayed in the dropdown
<AutoComplete
    list={[
      { name: 'Tom', id: 3233 },
      { name: 'Tommy', id: 3445 },
      { name: 'Thomas', id: 3663 }
    ]}
    getDisplayValue={(list) => {
      return list.map((listItem) => listItem.name)
    }}
  />

handleHighlight: function (Optional)

  • callback function invoked when the highlighted item changes
  • its only argument is the highlighted item's value from the original list
  • if the highlighted item is a property value from an object, the whole object is passed in

onSelect: function (Optional)

  • callback function invoked when an item is selected
  • its only argument is the selected item's value from the original list
  • if the selected item is a property value from an object, the whole object is passed in

handleNewValue: function (Optional)

  • callback function invoked in place of onSelect when there is no matching value for the text input
  • the input value is its only Argument
  • if it is not passed in, the onSelect function will run with the text input being its only argument
  import { AutoComplete } from 'react-autocomplete-input-component';

  <AutoComplete
      list={[1, 'one', 2, 'two', 3, 'three']}
      onSelect={(selectedItem) => {
        console.log(selectedItem)
      }}
      handleNewValue={(inputValue) => {
        console.log(inputValue)
      }}
  />

onSelectError: function (Optional)

  • used if new values are not permitted
  • callback function invoked if onSelect fires when there is no match for the input value and the handleNewValue function is not passed in
  • the input value is its only argument

noMatchMessage: string (Optional)

  • message displayed in the dropdown when there is no match for the current input value
  • default - will show no message
  • if a string is passed in - it will be the message shown
<AutoComplete
    onSelectError={() => window.alert("TRY AGAIN")}
    noMatchMessage={"No matches found"}
  />

open : boolean (Optional)

  • can be used to control the position of the dropdown
  • true opens the dropdown and false closes the dropdown

onDropDownChange: function (Optional)

  • callback function invoked whenever dropdown is opened or closed
  • its only argument is the current position of the dropdown
  const [openDropDown, setOpenDropDown] = useState()

  const toggleDropDown = () => {
    setOpenDropDown(openDropDown ? false : true)
  }

  return(
    <>
      <button className='ignore' onClick={toggleDropDown} />
      <AutoComplete
          onDropDownChange={(isOpen) => setOpenDropDown(isOpen)}
          open={openDropDown}
      />
    </>
  )

disableOutsideClick : boolean (Optional)

  • false (default) the dropdown closes when mouse is clicked outside of the auto-complete wrapper div
  • setting to true will disable the feature
  • to ignore a specific element give the element a className of ignore

inputProps: Object (Optional)

  • Sets HTMLInputElement properties with some exceptions
  • autocomplete, onChange, onKeyDown, onFocus cannot be overridden
  <AutoComplete
      inputProps={{
        placeholder: "search...",
        onMouseOver: () => setOpenDropDown(true)
      }}
      showAll={true}
      highlightFirstItem={false}
  />

showAll: boolean (Optional)

  • false (default) dropdown doesn't appear until input value matches an item's prefix
  • true - If the input is focused and empty the dropdown displays all list items

highlightFirstItem: boolean (Optional)

  • true (default) - automatically highlights first item in dropdown
  • false - highlight is hidden until arrow key is pressed or hover with mouse

submit : boolean (Optional)

  • can be used with controlSubmit to only fire onSelect or handleNewValue when passed in as true

controlSubmit: boolean (Optional)

  • when set to true, onSelect and handleNewValue will only fire when submit is passed in as true
  const [submit, setSubmit] = useState(false);

  const toggleSubmit = (() => {
    setSubmit(true)
  })

  return(
    <>
      <button className='ignore' onClick={toggleSubmit}>SUBMIT</button>
      <AutoComplete
          controlSubmit={true}
          submit={submit}
          onSelect={(selectedItem) => {
            console.log(selectedItem)
            setSubmit(false)
          }}
      />
    </>
  )

wrapperStyle: Object (Optional)

  • Style Object for the div wrapping the whole component
  • CSS can also be used with the class name autocomplete-wrapper

inputStyle: Object (Optional)

  • Style Object for the input element
  • CSS can also be used with the class name autocomplete-input

dropDownStyle: Object (Optional)

  • Style Object for the dropdown container div
  • CSS can also be used with the class name dropdown-container

listItemStyle: Object (Optional)

  • Style Object for each item div in the dropdown
  • CSS can also be used with the class name dropdown-item

highlightedItemStyle: Object (Optional)

  • Style Object for the highlighted item
  • CSS can also be used with the class name highlighted-item
  <AutoComplete
      highlightedItemStyle={{
        backgroundColor: "dodgerBlue"
      }}
      listItemStyle={{
        cursor: "pointer",
        padding: "5px"
      }}
      dropDownStyle={{
        backgroundColor: "antiquewhite",
        width: "215px"
      }}
  />

Tests

Add to the tests: src/AutoComplete.test.js
Run the tests: npm test

Available Scripts

In the project directory, you can run:

npm start

Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.

The page will reload if you make edits.
You will also see any lint errors in the console.

npm test

Launches the test runner in the interactive watch mode.