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-interactive-dropdown

v1.0.21

Published

A highly customizable and easy-to-use dropdown component for React.

Downloads

119

Readme

react-interactive-dropdown

react-interactive-dropdown is a highly customizable and easy-to-use dropdown component for React that supports multiple features like multi-select, server-side search, pagination, custom styling, and more. It leverages the power of react-window for rendering large lists efficiently.

Features

  • Single select and multi-select options
  • Customizable with icons, checkboxes, and end adornments
  • Searchable with both client-side and server-side options
  • Supports pagination with infinite scrolling using IntersectionObserver
  • Handles component rendering for custom options
  • Option to display selected items as chips
  • Fully customizable styles and components

Installation

Install the package via npm:

npm install react-interactive-dropdown

Usage Here is a basic example of how to use the InteractiveDropdown component:

import React, { useState } from "react";
import InteractiveDropdown from "react-interactive-dropdown";

const options = [
  { value: 1, label: "Option 1" },
  { value: 2, label: "Option 2" },
  { value: 3, label: "Option 3" },
];

function App() {
  const [selectedValue, setSelectedValue] = useState(null);
  const handleChange = (value) => {
    setSelectedValue(value);
  };
  return (
    <div>
      <InteractiveDropdown
        selectionList={options}
        placeholder="Select an option"
        value={selectedValue}
        onChange={handleChange}
        isSearchable={true}
        isMultiSelect={false}
        labelKeys={["label"]}
        valueKey="value"
      />
    </div>
  );
}
export default App;

Props

| Prop Name | Type | Default | Description | | --------------------- | ---------- | ----------- | ------------------------------------------------------- | | selectionList | array | [] | List of selection options. | | placeholder | string | undefined | Placeholder text for dropdown. | | labelKeys | array | [] | Keys used for labeling options. | | valueKey | string | undefined | Key for value of options. | | value | any | null | Selected value(s). | | onChange | function | (e) => {} | Callback function for handling selection changes. | | autoFormat | boolean | false | If true, automatically formats the labels. | | name | string | undefined | Dropdown name. | | isRequired | boolean | false | If true, the dropdown is required. | | label | string | null | Label for the dropdown. | | isSearchable | boolean | true | If true, enables search functionality. | | isMultiSelect | boolean | false | If true, enables multi-select. | | withCheckBox | boolean | false | If true, displays checkboxes for multi-select options. | | withChips | boolean | false | If true, displays chips for selected items. | | iconComponent | function | null | Custom component for option icons. | | isDisabled | boolean | false | If true, disables the dropdown. | | endAdornment | function | null | Additional adornment for the end of each option. | | chipStyle | object | undefined | Custom style for chips. | | isServerSideSearch | boolean | false | If true, search queries are handled server-side. | | onSearchQueryChange | function | (e) => {} | Callback function for search query changes. | | menuWidth | string | undefined | Custom width for the dropdown menu. | | isComponentOptions | boolean | false | If true, options are rendered as components. | | onLastRowIntersect | function | () => {} | Callback function for when the last row is intersected. | | isPagination | boolean | false | If true, enables pagination. |

Advanced Usage

UI Virtualization The dropdown uses react-window to efficiently render large lists. This improves performance by only rendering items that are currently visible in the viewport.

Server-Side Pagination The onLastRowIntersect prop allows you to handle server-side pagination. When the last row in the list becomes visible, this callback is triggered, enabling you to fetch and append more items.

<InteractiveDropdown
  selectionList={options}
  placeholder="Select an option"
  value={selectedValue}
  onChange={handleChange}
  isSearchable={true}
  isMultiSelect={false}
  labelKeys={["label"]}
  valueKey="value"
  onLastRowIntersect={() => {
    // api call for next data
  }}
  isPagination={true}
/>

Server-Side Option Search For handling search queries on the server side, use the isServerSideSearch prop. The onSearchQueryChange callback will be invoked whenever the search query changes, allowing you to fetch search results from your server.

<InteractiveDropdown
  selectionList={options}
  placeholder="Select an option"
  value={selectedValue}
  onChange={handleChange}
  isSearchable={true}
  isMultiSelect={false}
  labelKeys={["label"]}
  valueKey="value"
  isServerSideSearch={(e) => {
    //here is your server-side searh logic
  }}
/>

Custom Option Components You can pass a custom component for options using the isComponentOptions prop. This allows for highly customizable option rendering, as you can provide a component that will be rendered for each option in the dropdown.

consy options = {
  {value:"ITEM_1", label:"Item 1", comp: {<img src={item1_img_src}/>}},
  ...
}
<InteractiveDropdown
  selectionList={options}
  placeholder="Select an option"
  value={selectedValue}
  onChange={handleChange}
  isSearchable={true}
  isMultiSelect={false}
  labelKeys={["label"]}
  valueKey="value"
  isComponentOptions={true}
/>

Styling

The dropdown component uses the following CSS classes which you can override in your own stylesheet:

.inputfield-label: Styles for the label of the dropdown. .chip: Styles for selected items when withChips is true. .closeButton: Styles for the close button in chips. .options-container: Styles for the dropdown menu container. .menu-search-div: Styles for the search input container. .menu-search-input: Styles for the search input. .menu-list-div: Styles for the dropdown list container. .menu-div: Styles for each option in the dropdown menu. .styled-menu-div: Styles for the menu options. .selected-styled-menu-div: Styles for selected menu options. .no-option-div: Styles for the no options available message.

As this library is in its early stages, we highly encourage users to report any bugs or issues they encounter. We are continuously working around it to make the library more sustainable. Thank you for your feedback and support!

License

This project is licensed under the ISC License