1.0.21 • Published 11 months ago

react-interactive-dropdown v1.0.21

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

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 NameTypeDefaultDescription
selectionListarray[]List of selection options.
placeholderstringundefinedPlaceholder text for dropdown.
labelKeysarray[]Keys used for labeling options.
valueKeystringundefinedKey for value of options.
valueanynullSelected value(s).
onChangefunction(e) => {}Callback function for handling selection changes.
autoFormatbooleanfalseIf true, automatically formats the labels.
namestringundefinedDropdown name.
isRequiredbooleanfalseIf true, the dropdown is required.
labelstringnullLabel for the dropdown.
isSearchablebooleantrueIf true, enables search functionality.
isMultiSelectbooleanfalseIf true, enables multi-select.
withCheckBoxbooleanfalseIf true, displays checkboxes for multi-select options.
withChipsbooleanfalseIf true, displays chips for selected items.
iconComponentfunctionnullCustom component for option icons.
isDisabledbooleanfalseIf true, disables the dropdown.
endAdornmentfunctionnullAdditional adornment for the end of each option.
chipStyleobjectundefinedCustom style for chips.
isServerSideSearchbooleanfalseIf true, search queries are handled server-side.
onSearchQueryChangefunction(e) => {}Callback function for search query changes.
menuWidthstringundefinedCustom width for the dropdown menu.
isComponentOptionsbooleanfalseIf true, options are rendered as components.
onLastRowIntersectfunction() => {}Callback function for when the last row is intersected.
isPaginationbooleanfalseIf 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

1.0.21

11 months ago

1.0.20

11 months ago

1.0.19

11 months ago

1.0.18

11 months ago

1.0.17

11 months ago

1.0.16

11 months ago

1.0.15

11 months ago

1.0.14

11 months ago

1.0.12

11 months ago

1.0.11

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago