
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
react-responsive-select
Advanced tools
An accessible React responsive select and multiselect form control
A React custom select control, that is both keyboard accessible like a native <select/>
on desktop. And also tappable / draggable on touch devices. As of version 2 it is now reading meaningfully on a screenreader (Only tested with VoiceOver on desktop so far).
It has some implementation examples
https://benbowes.github.io/react-responsive-select/demo/
Install the dependency - https://www.npmjs.com/package/react-responsive-select
npm install react-responsive-select -D
or yarn add -D react-responsive-select
Add ReactResponsiveSelect.js
And include the css file in your project. ./dist/ReactResponsiveSelect.css
Example usage:
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import ReactResponsiveSelect from 'react-responsive-select';
// import './node_modules/react-responsive-select/dist/ReactResponsiveSelect.css'; // To get basic styling (presuming you have an appropriate loader)
class Form extends Component {
reportChange(newValue) {
console.log(newValue);
}
render() {
return (
<form ref={r => this.form = r}>
<ReactResponsiveSelect
caretIcon={<span style={{float:'right'}}>+</span>}
name="make"
options={[
{ text: 'Any', value: 'null' },
{ text: 'Oldsmobile', value: 'oldsmobile', markup: <span>Oldsmobile</span> },
{ text: 'Ford', value: 'ford', markup: <span>Ford</span> }
]}
onChange={this.reportChange}
onSubmit={() => { this.form.submit(); }}
prefix="Make:"
selectedValue="mazda"
/>
</form>
);
}
}
ReactDOM.render(
<Form />,
document.getElementById('root')
);
A more detailed usage example can be found here: https://github.com/benbowes/react-responsive-select/blob/master/demo.js
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__select-container + .rrs__options-container {
/* height: 0;
visibility: hidden; */
height: auto;
visibility: visible;
}
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} :)`}
/>
The external API has not changed with version 2, but the DOM structure and CSS has (hence the major version change). To upgrade to this major version you'll need to do a minor upgrade of your CSS (If you have custom CSS overrides). Version 1 docs can be found here: https://github.com/benbowes/react-responsive-select/tree/1.5.3.
prop | type | description |
name (required) | String | The name to send with the selected option value(s) on form submit |
options (required) | Array of objects |
Note: |
onSubmit | Function | Some function that submits your form |
onChange | Function | Listen for changes on selected option change returns Note: |
caretIcon | JSX | Add a dropdown icon by using JSX markup |
selectedValue | String | Pre-select an option with this value - should match an existing option.value , or if omitted the first item will be selected |
prefix | String | Prefix for the select label |
disabled | Boolean | Disables the select control |
customLabelRenderer | Function | Allows you to format your own select label The customLabelRenderer function returns 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.js#L97) or the [multiselect demo](https://github.com/benbowes/react-responsive-select/blob/master/demo.js#L388) |
multiselect (MultiSelect mode) | Boolean | Makes the select control handle multiple selections. Check the implementation example here: https://benbowes.github.io/react-responsive-select/demo/ |
selectedValues (when MultiSelect mode) | Array of String values | Pre-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']}
|
For information about how this select control works on desktop and mobile see the Interaction tests readme README_INTERACTION_TESTS.md
FAQs
A customisable, touchable, React single-select / multi-select form control.
The npm package react-responsive-select receives a total of 1,637 weekly downloads. As such, react-responsive-select popularity was classified as popular.
We found that react-responsive-select demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.