0.2.6 • Published 5 years ago

use-downshift v0.2.6

Weekly downloads
8
License
MIT
Repository
github
Last release
5 years ago

use-downshift

MIT License downloads PRs Welcome

Installation

$ yarn add use-downshift
# or
$ npm i use-downshift

Usage

import React from 'react'

import useDownshift from 'use-downshift'

const items = [
  {value: 'apple'},
  {value: 'pear'},
  {value: 'orange'},
  {value: 'grape'},
  {value: 'banana'},
]

function MyComponent() {
  const {
    isOpen,
    getRootProps,
    getMenuProps,
    getItemProps,
    getLabelProps,
    getInputProps,
    highlightedIndex,
    inputValue,
    selectedItem
  } = useDownshift({
    onChange: selection => alert(`You selected ${selection.value}`),
    itemToString: item => (item ? item.value : '')
  })

  return (
    <div {...getRootProps()}>
      <label {...getLabelProps()}>{label}</label>
      <input {...getInputProps({ placeholder: 'search...' })} />
      {isOpen && (
        <ul {...getMenuProps()}>
          {items
            .filter(item => !inputValue || item.value.includes(inputValue))
            .map((item, index) => (
              <li
                {...getItemProps({
                  key: index,
                  item,
                  style: {
                    background: highlightedIndex === index ? 'lightgray' : 'white',
                    fontWeight: selectedItem === item ? 'bold' : 'normal',
                  }
                })}
              >
                {item.value}
              </li>
            ))}
        </ul>
      )}
    </div>
  )
}

export default MyComponent

Roadmap

  • move to ts
  • tests
  • ci/cd
  • semantic-release
  • docs
  • all-contributors

Inspiration

downshift

LIENSE

MIT

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.1.0-beta.1

5 years ago

0.1.0-beta.0

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago