Product
Introducing Ruby Support in Socket
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
react-select
Advanced tools
The react-select npm package is a flexible and customizable select input control for React applications. It provides a powerful set of features that allow developers to create simple dropdowns, multi-selects, async data loading, and more, with a focus on accessibility and customizability.
Basic Single Select
This feature allows you to create a basic single selection dropdown where a user can select one option from a predefined list.
{"import Select from 'react-select';\n\nconst options = [\n { value: 'chocolate', label: 'Chocolate' },\n { value: 'strawberry', label: 'Strawberry' },\n { value: 'vanilla', label: 'Vanilla' }\n];\n\nconst MyComponent = () => (\n <Select options={options} />\n);"}
Multi-Select
This feature enables users to select multiple options from the dropdown. It is useful for fields that require more than one selection.
{"import Select from 'react-select';\n\nconst options = [\n { value: 'chocolate', label: 'Chocolate' },\n { value: 'strawberry', label: 'Strawberry' },\n { value: 'vanilla', label: 'Vanilla' }\n];\n\nconst MyComponent = () => (\n <Select isMulti options={options} />\n);"}
Asynchronous Loading
This feature allows for loading options asynchronously, which is useful when the options are not available at initial render time or need to be fetched based on user input.
{"import Select from 'react-select';\nimport AsyncSelect from 'react-select/async';\n\nconst loadOptions = (inputValue, callback) => {\n setTimeout(() => {\n callback(filterOptions(inputValue));\n }, 1000);\n};\n\nconst MyComponent = () => (\n <AsyncSelect loadOptions={loadOptions} />\n);"}
Creatable Select
This feature allows users to create a new option on the fly if the option they are looking for is not available in the list.
{"import CreatableSelect from 'react-select/creatable';\n\nconst options = [\n { value: 'chocolate', label: 'Chocolate' },\n { value: 'strawberry', label: 'Strawberry' },\n { value: 'vanilla', label: 'Vanilla' }\n];\n\nconst MyComponent = () => (\n <CreatableSelect isMulti options={options} />\n);"}
Downshift is a set of primitives to build simple, flexible, WAI-ARIA compliant enhanced input React components. Its major focus is on flexibility and providing the building blocks for creating custom dropdown components. It is more low-level than react-select and requires more boilerplate to achieve similar functionality.
React-autosuggest is a WAI-ARIA compliant React autosuggest component that provides a simple yet customizable input field with suggestions. While it is similar to react-select in providing suggestions, it does not offer as many features out of the box, such as multi-select or async options.
Ant Design (antd) is a design system with a set of high-quality React components, one of which is a Select component. It offers a comprehensive suite of features similar to react-select but is part of a larger framework, which might be more than needed if only a select component is required.
A Select control built with and for React. Initially built for use in KeystoneJS.
Live demo: jedwatson.github.io/react-select
To build the examples locally, run:
npm install
gulp dev
Then open localhost:8000
in a browser.
This project is quite stable and ready for production use, however there are plans to improve it including:
It's loosely based on Selectize (in terms of behaviour and user experience) and React-Autocomplete (as a native React Combobox implemenation), as well as other select controls including Chosen and Select2.
The easiest way to use React-Select is to install it from NPM and include it in your own React build process (using Browserify, etc).
npm install react-select --save
You can also use the standalone build by including dist/select.js
and dist/default.css
in your page. If you use this, make sure you have already included the following dependencies:
React-Select generates a hidden text field containing the selected value, so you can submit it as part of a standard form. You can also listen for changes with the onChange
event property.
Options should be provided as an Array
of Object
s, each with a value
and label
property for rendering and searching.
When the value is changed, onChange(newValue, [selectedOptions])
will fire.
var Select = require('react-select');
var options = [
{ value: 'one', label: 'One' },
{ value: 'two', label: 'Two' }
];
function logChange(val) {
console.log("Selected: " + val);
}
<Select
name="form-field-name"
value="one"
options={options}
onChange={logChange}
/>
You can enable multi-value selection by setting multi={true}
. In this mode:
delimiter
property to create the input valuedelimiter
propertyonChange
event provides an array of the selected options as the second argumentIf you want to load options asynchronously, instead of providing an options
Array, provide a asyncOptions
Function.
The function takes two arguments String input, Function callback
and will be called when the input text is changed.
When your async process finishes getting the options, pass them to callback(err, data)
in a Object { options: [] }
.
The select control will intelligently cache options for input strings that have already been fetched. Async options will still be filtered like the normal options array, so if your async process would only return a smaller set of results for a more specific query, also pass complete: true
in the callback object.
Unless you specify the property autoload={false}
the control will automatically load the default set of options (i.e. for input: ''
) when it is mounted.
var Select = require('react-select');
var getOptions = function(input, callback) {
setTimeout(function() {
callback(null, {
options: [
{ value: 'one', label: 'One' },
{ value: 'two', label: 'Two' }
],
// CAREFUL! Only set this to true when there are no more options,
// or more specific queries will not be sent to the server.
complete: true
});
}, 500);
};
<Select
name="form-field-name"
value="one"
asyncOptions={getOptions}
/>
You can control how options are filtered with the following properties:
matchPos
: "start"
or "any"
: whether to match the text entered at the start or any position in the option valuematchProp
: "label"
, "value"
or "any"
: whether to match the value, label or both values of each option when filteringBoth properties default to "any"
.
You can also completely replace the method used to filter either a single option, or the entire options array (allowing custom sort mechanisms, etc.)
filterOption
: function(Object option, String filter)
returns Boolean
. Will override matchPos
and matchProp
options.filterOptions
: function(Array options, String filter, Array currentValues)
returns Array filteredOptions
. Will override filterOption
, matchPos
and matchProp
options.For multi-select inputs, when providing a custom filterOptions
method, remember to exclude current values from the returned array of options.
Property | Type | Description
:-----------------------|:--------------|:-------------------------------- value | any | initial field value multi | bool | multi-value input disabled | bool | whether the Select is disabled or not options | array | array of options delimiter | string | delimiter to use to join multiple values asyncOptions | func | function to call to get options autoload | bool | whether to auto-load the default async options set placeholder | string | field placeholder, displayed when there's no value noResultsText | string | placeholder displayed when there are no matching search results clearable | bool | should it be possible to reset value clearValueText | string | title for the "clear" control clearAllText | string | title for the "clear" control when multi: true searchable | bool | whether to enable searching feature or not searchPromptText | string | label to prompt for search input name | string | field name, for hidden tag onChange | func | onChange handler: function(newValue) {} onFocus | func | onFocus handler: function(event) {} onBlur | func | onBlur handler: function(event) {} className | string | className for the outer element filterOption | func | method to filter a single option: function(option, filterString) filterOptions | func | method to filter the options array: function([options], filterString, [values]) matchPos | string | (any, start) match the start or entire string when filtering matchProp | string | (any, label, value) which option property to filter on inputProps | object | custom attributes for the Input (in the Select-control) e.g: {'data-foo': 'bar'}
See our CONTRIBUTING.md for information on how to contribute.
MIT Licensed. Copyright (c) Jed Watson 2015.
FAQs
A Select control built with and for ReactJS
The npm package react-select receives a total of 3,800,643 weekly downloads. As such, react-select popularity was classified as popular.
We found that react-select demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers 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.
Product
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.