3.0.0 • Published 3 years ago

react-dropdown-aria v3.0.0

Weekly downloads
13,055
License
MIT
Repository
-
Last release
3 years ago

CircleCI npm npm bundle size npm

react-dropdown-aria

This component was created to be a light weight and fully accessible dropdown component for React. For a more feature heavy and powerful dropdown look to the react-select package. (This dropdown is inspired by react-select)

Upgrading to v3

Please take note when upgrading from v2 to v3 there are breaking changes. Be sure to carefully read the changelog entry for v3.0.0.

Features

  1. Fully customizable styling (powered by styled-components)
  2. Grouped options
  3. Accessible
  4. Searchable
  5. Custom render function props
  6. Typescript typings

Peer dependencies:

  • Styled-components (Used for styling)

Demo And Examples

For demo and examples see https://jfangrad.github.io/react-dropdown-aria/

Installation

Simply use npm or yarn to install the package.

// Yarn
$ yarn add react-dropdown-aria styled-components

// npm
$ npm install --save react-dropdown-aria styled-components

You can then include it in your project as needed:

import Dropdown from 'react-dropdown-aria';

Usage

Options should be provided to the Dropdown as an array of objects.

Simple Example:

<Dropdown
  ...
  options: {[
    { value: 'one' },
    { value: 'two' },
    { value: 'three' }
  ]}
/>

Each option object in the array of options can have the following keys:

keyTypeDefaultDescription
ariaLabelstringnullAria Label to be applied to the option
classNamestringnullCSS class to be applied to this option
titlestringnullHTML title to be used for the option
valuestringnullThe value to be displayed in the dropdown (Required)

Groups Example

Groups of items can also be implemented by pasing an array of objects of the following form to the dropdown options prop:

<Dropdown
  ...
  options: {[
    { label: 'Group 1', groupOptions: optionsGroup1 },
    { label: 'Group 2', groupOptions: optionsGroup2 },
  ]}
/>

Where the groupOptions is an array of options as described above, and the label is the string to display above the group in the dropdown.

Styling

Custom styling can be applied to the dropdown through 2 ways: 1. CSS className props (As seen in props table below) 2. JavaScript Objects passed to the theme prop (Example) 3. Using the styled-components library (Example)

Option 1

The suggested method is by using the theme prop as shown in the Custom Styling Example. The theme prop can have keys of the following form:

type RdaTheme = {
  wrapper?: ThemeObjectOrFunction<DropdownWrapperProps>;
  selector?: ThemeObjectOrFunction<DropdownSelectorProps>;
  selectorSearch?: ThemeObjectOrFunction;
  selectedValue?: ThemeObjectOrFunction<SelectedValueProps>;
  placeholder?: ThemeObjectOrFunction<PlaceholderProps>;
  arrow?: ThemeObjectOrFunction<ArrowProps>;
  optionContainer?: ThemeObjectOrFunction<OptionContainerProps>;
  groupContainer?: ThemeObjectOrFunction;
  groupHeading?: ThemeObjectOrFunction;
  groupDivider?: ThemeObjectOrFunction;
  optionItem?: ThemeObjectOrFunction<OptionItemWrapProps>;
};

As is shown above, each key in the theme object can be either a plain object or function. If you need access to the current state of the dropdown then you should pass a function which will be called with the appropiate state. The theme object you provide will be merged with the default theme.

option 3

If you would prefer to use option 3, you can apply styles to the sub components of the dropdown using the method described here. The sub components can be imported via

import { StyledDropdownComponents } from 'react-dropdown-aria';

Each component is a property on that import and has the same keys as those listed above using typical component casing. (Example)

Dropdown Props

PropertyTypeDefaultDescription
ariaDescribedBystringnullID of element that should be used to describe the dropdown
ariaLabelstringnullAria Label to be applied to the main dropdown button
ariaLabelledBystringnullID of element that should be used as the label for the dropdown
arrowRendererfunctionundefinedCustom function to render the arrow for the dropdown
centerTextbooleanfalseWhether main dropdown button text should be centered or not
classNamestringundefinedCSS class to be applied to the drodown
contentClassNamestringundefinedCSS class to be applied to the dropdown option ul container
defaultOpenbooleanfalseWhether the dropdown should be open by default
disabledbooleanfalseWhether the dropdown should be disabled or not
heightnumbernullUse to set the dropdown height manually
hideArrowbooleanfalseControls whether dropdown component has the arrow or not
idstringundefinedid to be passed to the main dropdown button
maxContentHeightnumbernullControls the max height of the dropdown area that contains all options
onChangefunctionundefinedFunction called when the selected value changes
openUpbooleanfalseWhether dropdown should open up or not
optionRendererfunctionundefinedCustom function to render the options displayed in the dropdown
optionsarray[]Array of option objects
optionClassNamestringundefinedCSS class to be applied to all options in the dropdown
pageKeyTraverseSizenumber10Number of options page up or page down will move the current focused by
placeholderstringSelect...Placeholder value for dropdown
valuestringnullThe value of the option that is currently selected (as set by setSelected).
selectedValueClassNamestringundefinedCSS class to be applied to main drodown button text
searchablebooleantrueWhether or not the keyboard can be used to quickly navigate to an item through typing
themeRdaThemeundefinedUse to change the style of the dropdown through js instead of css (see styling section)
widthnumbernullUse to set the dropdown width manually

Developing

Clone the repo

git clone https://github.com/jfangrad/react-dropdown-aria.git

Install node modules

yarn install

Then in two seperate consoles start the library and demo

yarn lib:dev
yarn demo:start
3.0.0

3 years ago

3.0.0-rc.1

3 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.0

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago