[![NPM](https://img.shields.io/npm/v/react-select.svg)](https://www.npmjs.com/package/react-select) [![CDNJS](https://img.shields.io/cdnjs/v/react-select.svg)](https://cdnjs.com/libraries/react-select) [![Build Status](https://travis-ci.org/JedWatson/react-select.svg?branch=master)](https://travis-ci.org/JedWatson/react-select) [![Coverage Status](https://coveralls.io/repos/JedWatson/react-select/badge.svg?branch=master&service=github)](https://coveralls.io/github/JedWatson/react-select?branch=master) [![Supported by Thinkmill](https://thinkmill.github.io/badge/heart.svg)](http://thinkmill.com.au/?utm_source=github&utm_medium=badge&utm_campaign=react-select) React-Select ============ A Select control built with and for [React](http://facebook.github.io/react/index.html). Initially built for use in [KeystoneJS](http://www.keystonejs.com). ## Demo & Examples Live demo: [jedwatson.github.io/react-select](http://jedwatson.github.io/react-select/) ## Installation The easiest way to use react-select is to install it from npm and build it into your app with Webpack. ```js yarn add react-select ``` You can then import react-select and its styles in your application as follows: ```js import Select from 'react-select'; import 'react-select/dist/react-select.css'; ``` You can also use the standalone UMD build by including `dist/react-select.js` and `dist/react-select.css` in your page. If you do this you'll also need to include the dependencies. For example: ```html ``` ## Usage 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. You can use a `disabled` property to indicate whether the option is disabled or not. The `value` property of each option should be either a string or a number. When the value is changed, `onChange(selectedValueOrValues)` will fire. Note that (as of 1.0) you **must** handle the change and pass the updated `value` to the Select. ```js import React from 'react'; import Select from 'react-select'; class App extends React.Component { state = { selectedOption: '', } handleChange = (selectedOption) => { this.setState({ selectedOption }); console.log(`Selected: ${selectedOption.label}`); } render() { return ( ` component, which will be added to the base `.Select` className for the outer container. The built-in Options renderer also support custom classNames, just add a `className` property to objects in the `options` array. ### Multiselect options You can enable multi-value selection by setting `multi={true}`. In this mode: * Selected options will be removed from the dropdown menu by default. If you want them to remain in the list, set `removeSelected={false}` * The selected values are submitted in multiple `` fields, use the `joinValues` prop to submit joined values in a single field instead * The values of the selected items are joined using the `delimiter` prop to create the input value when `joinValues` is true * A simple value, if provided, will be split using the `delimiter` prop * The `onChange` event provides an array of selected options _or_ a comma-separated string of values (eg `"1,2,3"`) if `simpleValue` is true * By default, only options in the `options` array can be selected. Use the `Creatable` Component (which wraps `Select`) to allow new options to be created if they do not already exist. Hitting comma (','), ENTER or TAB will add a new option. Versions `0.9.x` and below provided a boolean attribute on the `Select` Component (`allowCreate`) to achieve the same functionality. It is no longer available starting with version `1.0.0`. * By default, selected options can be cleared. To disable the possibility of clearing a particular option, add `clearableValue: false` to that option: ```js var options = [ { value: 'one', label: 'One' }, { value: 'two', label: 'Two', clearableValue: false } ]; ``` Note: the `clearable` prop of the Select component should also be set to `false` to prevent allowing clearing all fields at once #### Accessibility Note Selected values aren't focus targets, which means keyboard users can't tab to them, and are restricted to removing them using backspace in order. This isn't ideal and I'm looking at other options for the future; in the meantime if you want to use a custom `valueComponent` that implements tabIndex and keyboard event handling, see #2098 for an example. ### Async options If you want to load options asynchronously, use the `Async` export and provide a `loadOptions` 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. The cached result set will be filtered as more specific searches are input, 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. Caching can be disabled by setting `cache` to `false` (Note that `complete: true` will then have no effect). 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. ```js import { Async } from 'react-select'; const getOptions = (input, callback) => { setTimeout(() => { 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); }; ``` #### Note about filtering async options The `Async` component doesn't change the default behaviour for filtering the options based on user input, but if you're already filtering the options server-side you may want to customise or disable this feature (see [filtering options](#filtering-options) below) ### Async options with Promises `loadOptions` supports Promises, which can be used in very much the same way as callbacks. Everything that applies to `loadOptions` with callbacks still applies to the Promises approach (e.g. caching, autoload, ...) An example using the `fetch` API and ES6 syntax, with an API that returns an object like: ```js import { Async } from 'react-select'; /* * assuming the API returns something like this: * const json = [ * { value: 'one', label: 'One' }, * { value: 'two', label: 'Two' } * ] */ const getOptions = (input) => { return fetch(`/users/${input}.json`) .then((response) => { return response.json(); }).then((json) => { return { options: json }; }); } ``` ### Async options loaded externally If you want to load options asynchronously externally from the `Select` component, you can have the `Select` component show a loading spinner by passing in the `isLoading` prop set to `true`. ```js import Select from 'react-select'; let isLoadingExternally = true; ``` ### Overriding default key-down behaviour with onInputKeyDown `Select` listens to `keyDown` events to select items, navigate drop-down list via arrow keys, etc. You can extend or override this behaviour by providing a `onInputKeyDown` callback. ```js function onInputKeyDown(event) { switch (event.keyCode) { case 9: // TAB // Extend default TAB behaviour by doing something here break; case 13: // ENTER // Override default ENTER behaviour by doing stuff here and then preventing default event.preventDefault(); break; } } ` tag | | `noResultsText` | string | 'No results found' | placeholder displayed when there are no matching search results or a falsy value to hide it (can also be a react component) | | `onBlur` | function | undefined | onBlur handler: `function(event) {}` | | `onBlurResetsInput` | boolean | true | whether to clear input on blur or not | | `onChange` | function | undefined | onChange handler: `function(newOption) {}` | | `onClose` | function | undefined | handler for when the menu closes: `function () {}` | | `onCloseResetsInput` | boolean | true | whether to clear input when closing the menu through the arrow | | `onFocus` | function | undefined | onFocus handler: `function(event) {}` | | `onInputChange` | function | undefined | onInputChange handler/interceptor: `function(inputValue: string): string` | | `onInputKeyDown` | function | undefined | input keyDown handler; call `event.preventDefault()` to override default `Select` behaviour: `function(event) {}` | | `onMenuScrollToBottom` | function | undefined | called when the menu is scrolled to the bottom | | `onOpen` | function | undefined | handler for when the menu opens: `function () {}` | | `onSelectResetsInput` | boolean | true | whether the input value should be reset when options are selected, for `multi` | `onValueClick` | function | undefined | onClick handler for value labels: `function (value, event) {}` | | `openOnClick` | boolean | true | open the options menu when the control is clicked (requires searchable = true) | | `openOnFocus` | boolean | false | open the options menu when the control gets focus | | `optionClassName`: string | undefined | additional class(es) to apply to the