Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
react-select-math512
Advanced tools
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
npm start
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 implementation), 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. 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}
/>
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 argumentonChange
is always a string, regardless of whether the values of the selected options are numbers or stringsoptions
array can be selected. Setting allowCreate
to true allows new options to be created if they do not already exist.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 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 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}
/>
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}
/>
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}
...
/>
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 filteringignoreCase
: Boolean
: whether to ignore case or match the text exactly when filteringmatchProp
and matchPos
both default to "any"
.
ignoreCase
defaults to true
.
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
, matchProp
and ignoreCase
options.filterOptions
: function(Array options, String filter, Array currentValues)
returns Array filteredOptions
. Will override filterOption
, matchPos
, matchProp
and ignoreCase
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
:-----------------------|:--------------|:--------------------------------
addLabelText | string | text to display when allowCreate
is true
allowCreate | bool | allow new options to be created in multi mode (displays an "Add <option> ?" item when a value not already in the options
array is entered)
asyncOptions | func | function to call to get options
autoload | bool | whether to auto-load the default async options set
backspaceRemoves | bool | whether pressing backspace removes the last item when there is no input value
cacheAsyncResults | bool | enables the options cache for asyncOptions
(default: true
)
className | string | className for the outer element
clearable | bool | should it be possible to reset value
clearAllText | string | title for the "clear" control when multi
is true
clearValueText | string | title for the "clear" control
delimiter | string | delimiter to use to join multiple values
disabled | bool | whether the Select is disabled or not
filterOption | func | method to filter a single option: function(option, filterString)
filterOptions | func | method to filter the options array: function([options], filterString, [values])
ignoreCase | bool | whether to perform case-insensitive filtering
inputProps | object | custom attributes for the Input (in the Select-control) e.g: {'data-foo': 'bar'}
isLoading | bool | whether the Select is loading externally or not (such as options being loaded)
labelKey | string | the option property to use for the label
matchPos | string | (any, start) match the start or entire string when filtering
matchProp | string | (any, label, value) which option property to filter on
multi | bool | multi-value input
name | string | field name, for hidden <input />
tag
newOptionCreator | func | factory to create new options when allowCreate
is true
noResultsText | string | placeholder displayed when there are no matching search results
onBlur | func | onBlur handler: function(event) {}
onChange | func | onChange handler: function(newValue) {}
onFocus | func | onFocus handler: function(event) {}
onInputChange | func | onInputChange handler: function(inputValue) {}
onOptionLabelClick | func | onClick handler for value labels: function (value, event) {}
optionRenderer | func | function which returns a custom way to render the options in the menu
options | array | array of options
placeholder | string | field placeholder, displayed when there's no value
searchable | bool | whether to enable searching feature or not
searchingText | string | message to display whilst options are loading via asyncOptions, or when isLoading
is true
searchPromptText | string | label to prompt for search input
value | any | initial field value
valueKey | string | the option property to use for the value
valueRenderer | func | function which returns a custom way to render the value selected
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();
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-math512 receives a total of 0 weekly downloads. As such, react-select-math512 popularity was classified as not popular.
We found that react-select-math512 demonstrated a not healthy version release cadence and project activity because the last version was released 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.