spmjs

spm@3.x

Brand new static package manager for browser.

Getting Started 󰅴 Packages

react-select @0.9.1


A Select control built with and for ReactJS

spm install react-select@0.9.1

Version: 0.9.1 ~ stable

Build Status Coverage Status

React-Select

A Select control built with and for React. Initially built for use in KeystoneJS.

Demo & Examples

Live demo: jedwatson.github.io/react-select

To build the examples locally, run:

npm install
npm start

Then open localhost:8000 in a browser.

Project Status

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 implementation), as well as other select controls including Chosen and Select2.

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, 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:

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 Objects, 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(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}
/>

Multiselect options

You can enable multi-value selection by setting multi={true}. In this mode:

Async options

If you want to load options asynchronously, instead of providing an options Array, provide a asyncOptions Function.

The function takes two arguments String input, Function callbackand 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 cacheAsyncResults 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.

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}
/>

Async options with Promises

asyncOptions now supports Promises, which can be used in very much the same way as callbacks.

Everything that applies to asyncOptions 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:

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 };
    });
}

<Select
    name="form-field-name"
    value="one"
    asyncOptions={getOptions}
/>

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.

var Select = require('react-select');

var isLoadingExternally = true;

<Select
  name="form-field-name"
    isLoading={isLoadingExternally}
    ...
/>

Filtering options

You can control how options are filtered with the following properties:

matchProp and matchPos both default to "any". ignoreCase defaults to true.

Advanced filters

You can also completely replace the method used to filter either a single option, or the entire options array (allowing custom sort mechanisms, etc.)

For multi-select inputs, when providing a custom filterOptions method, remember to exclude current values from the returned array of options.

Further options

Property            |    Type        |    Description

:-----------------------|:--------------|:-------------------------------- addLabelText | string | text to display when allowCreate is true allowCreate | bool | allow new options to be created in multi mode (displays an "Add \

Methods

Right now there's simply a focus() method that gives the control focus. All other methods on <Select> elements should be considered private and prone to change.

// focuses the input element
<instance>.focus();

Contributing

See our CONTRIBUTING.md for information on how to contribute.

License

MIT Licensed. Copyright (c) Jed Watson 2015.