0.3.1 • Published 5 years ago

react-16-dropdown v0.3.1

Weekly downloads
842
License
ISC
Repository
github
Last release
5 years ago

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>Display label for the option
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>Title for the section
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)}
/>
0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

6 years ago

0.1.0-alpha.3

6 years ago

0.1.0-alpha.2

6 years ago

0.1.0

6 years ago

0.0.1-security

6 years ago