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

react-nested-dropdown

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-nested-dropdown

Dropdown with submenu for React

  • 0.0.12
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
940
increased by28.24%
Maintainers
1
Weekly downloads
 
Created
Source

react-nested-dropdown

A simple and customizable nested dropdown component for React.

npm version npm downloads npm minified bundle size npm gzip minified bundle size

Features

  • Custom trigger element
  • Dropdown item with submenu
  • Multi level submenu support
  • Specific props to each dropdown item
  • Auto positioning of dropdown menu
  • Written in TypeScript 🤙

Installation

# using npm
npm install react-nested-dropdown
# using pnpm
pnpm install react-nested-dropdown
# using yarn
yarn add react-nested-dropdown

Basic usage

import React from 'react';

import { Dropdown } from 'react-nested-dropdown';
import 'react-nested-dropdown/dist/styles.css';

const items = [
  {
    label: 'Option 1',
    onSelect: () => console.log('Option 1 selected'),
  },
  {
    label: 'Option 2',
    items: [
      {
        label: 'Option 2.1',
        onSelect: () => console.log('Option 2.1 selected'),
      },
      {
        label: 'Option 2.2',
        onSelect: () => console.log('Option 2.2 selected'),
      },
    ],
  },
];

export const App = () => {
  return (
    <Dropdown items={items} containerWidth="300px">
      {({ isOpen, onClick }) => (
        <button type="button" onClick={onClick}>
          {isOpen ? 'Close dropdown' : 'Open dropdown'}
        </button>
      )}
    </Dropdown>
  );
};

Dropdown props

PropTypeDefaultDescription
itemsDropdownItem[][]An array of dropdown items to render in the menu.
containerWidthnumber or string300The width of the dropdown menu container. Can be a number for pixels or a string for any valid CSS width value.
onSelect(value: any, option: DropdownItem) => voidnullA callback function that is called when an option is selected. It is passed the value of the selected option and the option object itself.
children(params: { onClick: () => void, isOpen: boolean }) => React.ReactElementnullA function that returns a React element to be used as the trigger for the dropdown menu. The function is passed an object with an onClick function to open and close the dropdown, and an isOpen boolean to indicate the current open state of the dropdown.
classNamestringnullA custom class name to be applied to the root element of the component.
renderOption(option: DropdownItem) => React.ReactElementnullA function that returns a React element for rendering each option in the dropdown menu. It is passed the option object for the current item.
closeOnScrollbooleantrueIf set to true, the dropdown menu will close when the page is scrolled.

DropdownItem interface

PropTypeDefaultDescription
labelstring''The label to display for the item.
iconBeforeReact.ReactNodenullAn optional icon to display before the label.
iconAfterReact.ReactNodenullAn optional icon to display after the label.
itemsDropdownItem[]nullAn optional array of nested items to create a submenu.
itemsContainerWidthnumber or string150The width of the sub items menu container. Can be a number for pixels or a string for any valid CSS width value.
valueanyundefinedAn optional value for the item. This value will be in Dropdown's callback onSelect as first argument.
onSelect() => voidnullAn optional callback function to be called when the item is selected.
disabledbooleanfalseWhether the item should be disabled and unable to be selected.
classNamestringnullAn optional class name to be applied to the item element.

Keywords

FAQs

Package last updated on 05 Apr 2023

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