Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-16-dropdown

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-16-dropdown

A zero-dependency, lightweight and fully customizable dropdown (not select) for React.

  • 0.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
77
decreased by-23%
Maintainers
1
Weekly downloads
 
Created
Source

react-16-dropdown

A zero-dependency, lightweight and fully customizable dropdown (not select) for React. You can find examples here

Installation

npm install --save react-16-dropdown

Basic usage

import Dropdown from 'react-16-dropdown';

const options = [{
  label: 'Prestige 🎩',
  value: 'prestige',
}, {
  label: 'Inception 😴',
  value: 'inception',
}];

<Dropdown
  align='left'
  className='custom-classname'
  closeOnEscape={true}
  options={options}
  triggerLabel='Movies 🍿'
  onClick={val => console.log(val)}
/>

Supported props

You can pass the following props to the Dropdown component -

NameDefaultAllowed valuesDescription
alignleftleft, rightDecides the alignment of the menu w.r.t trigger
autoFocusfalseBooleanShould the trigger be focused by default
className''StringAdds the given class to the wrapper element
closeOnClickOutsidetrueBooleanShould the dropdown menu close on clicking outside the menu
closeOnEscapetrueBooleanShould the dropdown menu close on pressing Escape
closeOnOptionClicktrueBooleanShould the dropdown close when option is clicked
disabledfalseBooleanDisable the trigger
idundefinedStringHTML attribute id for the wrapper component
focusedundefinedStringDefault focused component in controlled mode
menuComponentMenu⁽¹⁾ ⁽²⁾ReactElementComponent to replace the default menu
menuPortalTargetbodyStringSelector for the portal to be attached as a child to
menuRendererMenuRenderer⁽¹⁾ReactElementComponent to render the menu
menuSectionRendererMenuRenderer⁽¹⁾ReactElementComponent to render menu sections
onClick*undefinedFunctionHandler for option click event
onCloseundefinedFunctionFunction to be called when menu closes
onOpenundefinedFunctionFunction to be called when menu opens
onMenuKeyDownundefinedFunctionFunction to be called when keydown event is triggered on the menu or bubbled up from option
onTriggerClickundefinedFunctionFunction to be called when the trigger element is clicked
onTriggerKeyDownundefinedFunctionFunction to be called when a key is pressed on the trigger
openundefinedBooleanProp to control open/closed state of the menu
optionComponentOption⁽¹⁾ ⁽²⁾ReactElementComponent to replace the default option
optionRendererOptionRenderer⁽¹⁾ReactElementComponent to render option
options*undefinedArrayAn array of objects
portalClassName''StringAdds the given class to portal component
sectionsundefinedArraySections array for menu with sections
triggerComponentTrigger⁽¹⁾ ⁽²⁾ReactElementComponent to replace the default trigger
triggerLabelOpen menuStringText for the default trigger button
triggerRendererTriggerRenderer⁽¹⁾ReactElementComponent to render the trigger

The options prop is an array of objects. Each object can have the following keys -

KeyValueDescription
value*StringUnique identifier for each option
label*<StringReactElement>
classNameStringCustom class name for the option
disabledBooleanIs the option disabled?

In case you are using sections you need to pass the sections prop, which is an array of objects. Each object can have the following keys -

KeyValueDescription
id*StringUnique identifier for each section
options*ArrayArray of options under this section
title<StringReactElement>
classNameStringCustom class name for section

⁽¹⁾ Default internal component

⁽²⁾ If you replace the component (instead of using renderers), you will have to pass down all the handlers, refs and other props down to your components.

* Required props

Customization

You can customize any part of the dropdown to suit your needs. In most cases, modifying existing classes/adding your own classes should do the trick. For advanced use cases, you can use custom render components. If you want to take over individual components of the dropdown, you can replace the menu, option or trigger default components.

Using renderers -

<Dropdown
  options={colorOptions}
  triggerRenderer={() => <button className='btn btn-dark ml-2'>Option renderer</button>}
  optionRenderer={props => <div className={`option option--${props.value}`}>{props.label}</div>}
  onClick={e => console.log(e)}
/>

Using components -

function CustomButtonComponent(props) {
  return (
    <a
      className='btn btn-outline-info'
      ref={props.triggerRef}
      onClick={props.onClick}
      onKeyDown={props.onKeyDown}
    >
      Custom link component
    </a>
  );
}

<Dropdown
  options={options}
  triggerComponent={CustomButtonComponent}
  onClick={e => console.log(e)}
/>

Controlled Component

You can also use the dropdown as a controlled component if you pass the open prop.

<Dropdown
  open={true}
  options={options}
  onTriggerClick={() => { /* do something */ }}
  onClick={e => console.log(e)}
/>

Sections

Dropdown sections with titles are also supported. Although, you can only have one level of sections. Instead of the options array, you need to pass the sections, which is an array of sections containing options. You need to pass a unique id for each section.

const sections = [{
  title: 'Movies',
  id: 'movies',
  options: movieOptions,
}, {
  title: 'Fruits',
  id: 'fruits',
  options: fruitOptions,
}];

<Dropdown
  closeOnEscape
  sections={sections}
  triggerLabel='Sections'
  onClick={e => console.log(e)}
/>

Keywords

FAQs

Package last updated on 22 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc