alexcarpenter / use-drop

A minimalist approach to custom dropdowns and autocompletes.

Home Page:https://use-drop.now.sh/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

use-drop npm

A minimalist approach to custom dropdowns and autocompletes.

Features

  • bring your own markup
  • no magic
  • full a11y support
  • supports multi-select
  • controlled, integrate with any form library

Install

npm i use-drop --save

Usage

import React from "react";
import cx from "classnames";

import { useSelect } from "use-drop";

function Dropdown() {
  const [label, labelSet] = React.useState("Please select");

  const {
    id,
    items,
    isOpen,
    getControlProps,
    getDropProps,
  } = useSelect({
    items: [
      { value: "san-francisco", label: "San Francisco" },
      { value: "los-angeles", label: "Los Angeles" },
      { value: "san-diego", label: "San Diego" },
      { value: "new-york", label: "New York" },
      { value: "albany", label: "Albany" },
      { value: "rochester", label: "Rochester" }
    ],
    onSelect(item) {
      labelSet(item.label); // set to active item
    },
    onRemove() {
      labelSet("Please select"); // reset to placeholder
    },
  });

  return (
    <>
      <label htmlFor={id}>Cities</label>
      <button id={id} controlProps={getControlProps()}>{label}</button>

      {isOpen && (
        <ul {...getDropProps()}>
          {items.map(i => (
            <li
              key={i.value}
              className={cx({
                'is-selected': i.selected,
                'is-highlighted': i.highlighted,
              })}
              {...i.getItemProps()}
            >
              {i.label}
            </li>
          ))}
        </ul>
      )}
    </>
  );
}

License

MIT License © Eric Bailey

About

A minimalist approach to custom dropdowns and autocompletes.

https://use-drop.now.sh/


Languages

Language:TypeScript 100.0%