# react-select **Repository Path**: mirrors_FezVrasta/react-select ## Basic Information - **Project Name**: react-select - **Description**: A Select control built with and for React JS - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-08 - **Last Updated**: 2026-06-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README [![NPM](https://img.shields.io/npm/v/react-select.svg)](https://www.npmjs.com/package/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) [![CDNJS](https://img.shields.io/cdnjs/v/react-select.svg)](https://cdnjs.com/libraries/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). ## New version 1.0.0-rc I've nearly completed a major rewrite of this component (see issue [#568](https://github.com/JedWatson/react-select/issues/568) for details and progress). The new code has been merged into `master`, and `react-select@1.0.0-rc` has been published to npm and bower. 1.0.0 has some breaking changes. The documentation is still being updated for the new API; notes on the changes can be found in [CHANGES.md](https://github.com/JedWatson/react-select/blob/master/CHANGES.md) and will be finalised into [HISTORY.md](https://github.com/JedWatson/react-select/blob/master/HISTORY.md) soon. Testing, feedback and PRs for the new version are appreciated. ## Demo & Examples Live demo: [jedwatson.github.io/react-select](http://jedwatson.github.io/react-select/) The live demo is still running `v0.9.1`. To build the **new 1.0.0** examples locally, clone this repo then run: ```javascript npm install npm start ``` Then open [`localhost:8000`](http://localhost:8000) in a browser. ## Installation The easiest way to use React-Select is to install it from NPM and include it in your own React build process (using [Browserify](http://browserify.org), etc). ```javascript npm install react-select --save ``` At this point you can import react-select and its styles in your application as follows: ```js import Select from 'react-select'; // Be sure to include styles at some point, probably during your bootstrapping import 'react-select/dist/react-select.css'; ``` You can also use the standalone build by including `react-select.js` and `react-select.css` in your page. (If you do this though 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 set to either a string or a number. When the value is changed, `onChange(selectedValueOrValues)` will fire. ```javascript var Select = require('react-select'); var options = [ { value: 'one', label: 'One' }, { value: 'two', label: 'Two' } ]; function logChange(val) { console.log("Selected: " + val); } ` 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 * The selected values are submitted in multiple `` fields, use `joinValues` 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: ```javascript 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 ### Async options If you want to load options asynchronously, instead of providing an `options` Array, 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. ```javascript 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); }; ``` ### 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: ```javascript import Select 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`. ```javascript var Select = require('react-select'); var isLoadingExternally = true; ``` ### Overriding default key-down behavior with onInputKeyDown `Select` listens to `keyDown` events to select items, navigate drop-down list via arrow keys, etc. You can extend or override this behavior by providing a `onInputKeyDown` callback. ```js function onInputKeyDown(event) { switch (event.keyCode) { case 9: // TAB // Extend default TAB behavior by doing something here break; case 13: // ENTER // Override default ENTER behavior 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 | func | undefined | onBlur handler: `function(event) {}` | | onBlurResetsInput | bool | true | whether to clear input on blur or not | | onChange | func | undefined | onChange handler: `function(newValue) {}` | | onClose | func | undefined | handler for when the menu closes: `function () {}` | | onCloseResetsInput | bool | true | whether to clear input when closing the menu through the arrow | | onFocus | func | undefined | onFocus handler: `function(event) {}` | | onInputChange | func | undefined | onInputChange handler: `function(inputValue) {}` | | onInputKeyDown | func | undefined | input keyDown handler; call `event.preventDefault()` to override default `Select` behavior: `function(event) {}` | | onOpen | func | undefined | handler for when the menu opens: `function () {}` | | onValueClick | func | undefined | onClick handler for value labels: `function (value, event) {}` | | openOnFocus | bool | false | open the options menu when the input gets focus (requires searchable = true) | | optionRenderer | func | undefined | function which returns a custom way to render the options in the menu | | options | array | undefined | array of options | | placeholder | string\|node | 'Select ...' | field placeholder, displayed when there's no value | | scrollMenuIntoView | bool | true | whether the viewport will shift to display the entire menu when engaged | | searchable | bool | true | whether to enable searching feature or not | | searchPromptText | string\|node | 'Type to search' | label to prompt for search input | | loadingPlaceholder | string\|node | 'Loading...' | label to prompt for loading search result | | tabSelectsValue | bool | true | whether to select the currently focused value when the `[tab]` key is pressed | | value | any | undefined | initial field value | | valueKey | string | 'value' | the option property to use for the value | | valueRenderer | func | undefined | function which returns a custom way to render the value selected `function (option) {}` | ### Methods Right now there's simply a `focus()` method that gives the control focus. All other methods on `