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

reactjs-dropdown-component

v1.2.0

Published

Single and multi select dropwdown menu components

Downloads

736

Readme

This package features two custom dropdown menu components for ReactJS.

WARNING: Breaking changes take effect from version 1.1.7. If you are using any of the earlier versions, refer to the previous README files.

Online demo

Single-selection | Multi-selection :-------------------------:|:-------------------------: | Single-selection searchable | Multi-selection searchable |

Installation

npm install --save reactjs-dropdown-component

Usage

Import the component you want to use;

import { DropdownMultiple, Dropdown } from 'reactjs-dropdown-component';

If you are using NextJS, import them dynamically instead;

import dynamic from 'next/dynamic';

const Dropdown = dynamic(
  async () => {
    const module = await import('reactjs-dropdown-component');
    const DD = module.Dropdown;

    return ({ forwardedRef, ...props }) => <DD ref={forwardedRef} {...props} />;
  },
  { ssr: false },
);

const DropdownMultiple = dynamic(
  async () => {
    const module = await import('reactjs-dropdown-component');
    const DDM = module.DropdownMultiple;

    return ({ forwardedRef, ...props }) => <DDM ref={forwardedRef} {...props} />;
  },
  { ssr: false },
);

The shape of the objects in the data array should be as follows:

const locations = [
  {
    label: 'New York',
    value: 'newYork',
  },
  {
    label: 'Oslo',
    value: 'oslo',
  },
  {
    label: 'Istanbul',
    value: 'istanbul',
  }
];

Use a function to pass to onChange prop. For <Dropdown> component item is an object, whereas for <DropdownMultiple> it is an array of objects.

  onChange = (item, name) => {
    ...
  }

Finally use the components as follows:

<Dropdown
  name="location"
  title="Select location"
  list={locations}
  onChange={this.onChange}
/>

<DropdownMultiple
  name="location"
  title="Select location"
  titleSingular="location"
  list={locations}
  onChange={this.onChange}
/>

Note that when multiple options are selected in <DropdownMultiple>, titleSingular value automatically becomes the plural form of the noun. If you want to use a custom plural title, define titlePlural as well.

<DropdownMultiple
  name="location"
  title="Velg sted"
  titleSingular="Sted"
  titlePlural="Steder"
  list={locations}
  onChange={this.onChange}
/>

Search functionality

Using searchable prop enables the search bar. Pass an array of strings corresponding to place holder and not found message respectively.

<Dropdown
  name="location"
  title="Select location"
  searchable={["Search for location", "No matching location"]}
  list={locations}
  onChange={this.onChange}
/>

Selecting item(s) by default

Use the select prop to define the initally selected item(s).

For <Dropdown>

select={{value: 'istanbul'}}

For <DropdownMultiple>

select={[{value: 'oslo'}, {value: 'istanbul'}]}

Calling internal functions

Pass a ref and use it to call the internal functions.

For <Dropdown>

<Dropdown
  ref={this.dropdownRef}
  ...
/>

this.dropdownRef.current.selectSingleItem({ value: 'oslo' });
this.dropdownRef.current.clearSelection();

For <DropdownMultiple>

<DropdownMultiple
  ref={this.dropdownRef}
  ...
/>

this.dropdownRef.current.selectMultipleItems([
  { value: 'istanbul' }
  { value: 'oslo' },
]);

this.dropdownRef.current.selectAll();
this.dropdownRef.current.deselectAll();

Custom styling

Use the following keys in an object passed to styles prop

wrapper
header
headerTitle
headerArrowUpIcon
headerArrowDownIcon
checkIcon
list
listSearchBar
scrollList
listItem
listItemNoResult

Example:

<Dropdown
  name="location"
  title="Select location"
  list={locations}
  onChange={this.onChange}
  styles={{
    headerTitle: { color: 'red' }
  }}
/>

Note that styles prop is meant for basic styling. Use stylesheet by targeting the specific class names for more complicated stylings.

Use id prop to set an additional class name to every element in the dropdown menu. That way you can style multiple dropdown menus with different stylings rules.

In order to define your own arrow and check icons, use arrowUpIcon, arrowDownIcon and checkIcon props. These props accept an element type.