🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

react-responsive-select

Package Overview
Dependencies
Maintainers
1
Versions
136
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-responsive-select

A React customisable, touchable, single-select / multi-select form component. Built with keyboard and screen reader accessibility in mind.

4.4.1
Source
npm
Version published
Weekly downloads
2.1K
41.41%
Maintainers
1
Weekly downloads
 
Created
Source

React Responsive Select

Build status

A customisable, touchable, React single-select / multi-select form control.

Built with keyboard and screen reader accessibility in mind.

Features

  • Single and Multi select modes
  • Accessible WAI ARIA compliance
  • Touch friendly
  • Keyboard friendly
  • Similar interaction experience across platforms
  • Easy to style

Demo

https://benbowes.github.io/react-responsive-select/demo/

Codepen

Screen reader demo

Voice over demo gif

Getting started

Install the dependency - https://www.npmjs.com/package/react-responsive-select

npm install react-responsive-select -D

Add ReactResponsiveSelect.js

And include the CSS file in your project via an import, or by copy/pasting the CSS from: ../node_modules/react-responsive-select/dist/ReactResponsiveSelect.css

Example usage:

import React from 'react';
import ReactResponsiveSelect from 'react-responsive-select';

const reportChange = (newValue) => console.log(newValue);

const Form = () => (
  <form ref={r => this.form = r}>
    <ReactResponsiveSelect
      name="make"
      options={[
        { text: 'Any', value: 'null' },
        { text: 'Oldsmobile', value: 'oldsmobile', markup: <span>Oldsmobile</span> },
        { text: 'Ford', value: 'ford', markup: <span>Ford</span> }
      ]}
      prefix="Make:"
      selectedValue="mazda"
      onSubmit={() => { this.form.submit(); }}
      onChange={reportChange}
      caretIcon={<span>+</span>}
    />
  </form>
);

A more detailed usage example can be found here: https://github.com/benbowes/react-responsive-select/blob/master/demo/src/index.js

Altering styling

The CSS in ./dist/ReactResponsiveSelect.css is plain css. Include it in your project via an import or copy paste it's contents into your stylesheet. Customisations can be done via overriding the styles or rewriting the classes.

The class names themselves are not configurable.

To aid in styling the hover/selected states in the options list, I would suggest overriding the class that hides the options temporarily e.g.

.rrs__button + .rrs__options {
  /* height: 0;
  visibility: hidden; */
  height: auto;
  visibility: visible;
}

Custom labelling

You can hook into the onChange function via the customLabelRenderer function prop. This allows you to render a custom label. See the API table for what the selectedOption object has in it.

<ReactResponsiveSelect
  name="make2"
  options={options}
  onSubmit={() => { this.form.submit(); }}
  // (Optional) format your own label text like this
  customLabelRenderer={selectedOption => `Selected make is ${selectedOption.text} :)`}
/>

API

Single Select

proptypedescription
name (required)StringThe name to send with the selected option value(s) on form submit
options (required)Array of objects

[{ text: "Fiat", value: "fiat", markup: <span>Fiat</span> disabled: true; }]

text: (Required) display value for the select and the default for the option label

value: (Required) value that is submitted

markup: (Optional) JSX markup used as the option label. Allows for the use of badges and icons...

disabled: (Optional) disable option - option cannot be selected and is greyed

Note: text is used as the option label when markup is not present

onSubmitFunctionSome function that submits your form
onChangeFunction

Listen for changes on selected option change

returns { altered: true||false, name: select.name, value: option.value, text: option.text }

Note: altered signifies whether a select has been changed from it's original value.

onBlurFunction

Listen for blur when select loses focus

returns { altered: true||false, name: select.name, value: option.value, text: option.text }

Note: altered signifies whether a select has been changed from it's original value.

caretIconJSXAdd a dropdown icon by using JSX markup
selectedValueStringPre-select an option with this value - should match an existing option.value, or if omitted the first item will be selected
prefixStringPrefix for the select label
disabledBooleanDisables the select control
noSelectionLabelstringA custom label to be used when nothing is selected. When used, the first option is not automatically selected
customLabelRendererFunction

Allows you to format your own select label

The customLabelRenderer function returns an option object e.g. { name: select.name, value: option.value, text: option.text, markup: JSX Object }

To use this feature you need to return some JSX; using values from the above object to create your own custom label.

See the example in the [singleselect demo](https://github.com/benbowes/react-responsive-select/blob/master/demo/src/index.js#L144).

Multi Select

Same as Single Select API but with the following amendments

multiselectBooleanMakes the select control handle multiple selections. Check the implementation example here: https://benbowes.github.io/react-responsive-select/demo/
selectedValuesArray of String valuesPre-select several options with this value - should match against an existing option.value, or if omitted, the first item will be selected. e.g. selectedValues={['mazda','ford']}
customLabelRendererFunction

Allows you to format your own select label

The customLabelRenderer function returns an array option objects e.g. [{ name: select.name, value: option.value, text: option.text, markup: JSX Object }]

To use this feature you need to return some JSX; using values from the above object to create your own custom label.

See the example in the [multiselect demo](https://github.com/benbowes/react-responsive-select/blob/master/demo/src/index.js#L589-L591).

noSelectionLabelstringA custom label to be used when nothing is selected. When used, the first option is not automatically selected
onChangeFunction

Listen for changes in selection

returns { altered: true||false, options: [{name: select.name, value: option.value, text: option.text }]}

Note: altered signifies whether a select has been changed from it's original value.

onBlurFunction

Listen for blur when select loses focus

returns { altered: true||false, options: [{name: select.name, value: option.value, text: option.text }]}

Note: altered signifies whether a select has been changed from it's original value.

CDN

JS: https://unpkg.com/react-responsive-select@latest/dist/ReactResponsiveSelect.js

CSS: https://unpkg.com/react-responsive-select@latest/dist/ReactResponsiveSelect.css

The Codepen examples are consuming react-responsive-select via CDN if you'd like a guide.

Babel Preset Env Targets

{
  "chrome": "62",
  "android": "4.2",
  "edge": "13",
  "firefox": "56",
  "ie": "11",
  "ios": "10",
  "safari": "10"
}

React Responsive Select 2.x => 3.x upgrade guide

See: https://github.com/benbowes/react-responsive-select/releases/tag/3.0.0

React Responsive Select 3.x => 4.x

Upgraded from React 15.x to React 16.x

Interaction Tests

For information about how this select control works on desktop and mobile see the Interaction tests readme README_INTERACTION_TESTS.md

Keywords

select

FAQs

Package last updated on 10 Sep 2018

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