Skip to content

Repository files navigation

Build StatusCoverage 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:

  • CSS Styles and theme support (working, could be improved)
  • Documentation website (currently just examples)
  • Custom options rendering (in progress)

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:

  • Selected options will be removed from the dropdown menu
  • The values of the selected items are joined using the delimiter property to create the input value
  • A simple value, if provided, will be split using the delimiter property
  • The onChange event provides an array of the selected options as the second argument
  • The first argument to onChange is always a string, regardless of whether the values of the selected options are numbers or strings
  • By default, only options in the options array can be selected. Setting allowCreate to true allows new options to be created if they do not already exist.

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

Filtering options

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 value
  • matchProp: "label", "value" or "any": whether to match the value, label or both values of each option when filtering
  • ignoreCase: Boolean: whether to ignore case or match the text exactly when filtering

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.)

  • 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.

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 <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 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: true clearValueText | string | title for the "clear" control delimiter | string | delimiter to use to join multiple values disableCache | bool | disables the options cache for asyncOptions 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'} 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 tag 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) {} 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 searchPromptText | string | label to prompt for search input value | any | initial field value valueRenderer | func | function which returns a custom way to render the value selected

Contributing

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

License

MIT Licensed. Copyright (c) Jed Watson 2015.

About

A Select control built with and for React JS

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
GitHub - ngunday/react-select: A Select control built with and for React JS · GitHub
Skip to content

Repository files navigation

Build StatusCoverage 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:

  • CSS Styles and theme support (working, could be improved)
  • Documentation website (currently just examples)
  • Custom options rendering (in progress)

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:

  • Selected options will be removed from the dropdown menu
  • The values of the selected items are joined using the delimiter property to create the input value
  • A simple value, if provided, will be split using the delimiter property
  • The onChange event provides an array of the selected options as the second argument
  • The first argument to onChange is always a string, regardless of whether the values of the selected options are numbers or strings
  • By default, only options in the options array can be selected. Setting allowCreate to true allows new options to be created if they do not already exist.

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

Filtering options

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 value
  • matchProp: "label", "value" or "any": whether to match the value, label or both values of each option when filtering
  • ignoreCase: Boolean: whether to ignore case or match the text exactly when filtering

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.)

  • 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.

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 <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 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: true clearValueText | string | title for the "clear" control delimiter | string | delimiter to use to join multiple values disableCache | bool | disables the options cache for asyncOptions 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'} 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 tag 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) {} 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 searchPromptText | string | label to prompt for search input value | any | initial field value valueRenderer | func | function which returns a custom way to render the value selected

Contributing

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

License

MIT Licensed. Copyright (c) Jed Watson 2015.

About

A Select control built with and for React JS

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - ngunday/react-select: A Select control built with and for React JS · GitHub
Skip to content

Repository files navigation

Build StatusCoverage 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:

  • CSS Styles and theme support (working, could be improved)
  • Documentation website (currently just examples)
  • Custom options rendering (in progress)

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:

  • Selected options will be removed from the dropdown menu
  • The values of the selected items are joined using the delimiter property to create the input value
  • A simple value, if provided, will be split using the delimiter property
  • The onChange event provides an array of the selected options as the second argument
  • The first argument to onChange is always a string, regardless of whether the values of the selected options are numbers or strings
  • By default, only options in the options array can be selected. Setting allowCreate to true allows new options to be created if they do not already exist.

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

Filtering options

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 value
  • matchProp: "label", "value" or "any": whether to match the value, label or both values of each option when filtering
  • ignoreCase: Boolean: whether to ignore case or match the text exactly when filtering

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.)

  • 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.

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 <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 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: true clearValueText | string | title for the "clear" control delimiter | string | delimiter to use to join multiple values disableCache | bool | disables the options cache for asyncOptions 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'} 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 tag 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) {} 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 searchPromptText | string | label to prompt for search input value | any | initial field value valueRenderer | func | function which returns a custom way to render the value selected

Contributing

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

License

MIT Licensed. Copyright (c) Jed Watson 2015.

About

A Select control built with and for React JS

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - ngunday/react-select: A Select control built with and for React JS · GitHub
Skip to content

Repository files navigation

Build StatusCoverage 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:

  • CSS Styles and theme support (working, could be improved)
  • Documentation website (currently just examples)
  • Custom options rendering (in progress)

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:

  • Selected options will be removed from the dropdown menu
  • The values of the selected items are joined using the delimiter property to create the input value
  • A simple value, if provided, will be split using the delimiter property
  • The onChange event provides an array of the selected options as the second argument
  • The first argument to onChange is always a string, regardless of whether the values of the selected options are numbers or strings
  • By default, only options in the options array can be selected. Setting allowCreate to true allows new options to be created if they do not already exist.

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

Filtering options

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 value
  • matchProp: "label", "value" or "any": whether to match the value, label or both values of each option when filtering
  • ignoreCase: Boolean: whether to ignore case or match the text exactly when filtering

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.)

  • 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.

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 <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 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: true clearValueText | string | title for the "clear" control delimiter | string | delimiter to use to join multiple values disableCache | bool | disables the options cache for asyncOptions 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'} 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 tag 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) {} 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 searchPromptText | string | label to prompt for search input value | any | initial field value valueRenderer | func | function which returns a custom way to render the value selected

Contributing

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

License

MIT Licensed. Copyright (c) Jed Watson 2015.

About

A Select control built with and for React JS

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' GitHub - ngunday/react-select: A Select control built with and for React JS · GitHub
Skip to content

Repository files navigation

Build StatusCoverage 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:

  • CSS Styles and theme support (working, could be improved)
  • Documentation website (currently just examples)
  • Custom options rendering (in progress)

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:

  • Selected options will be removed from the dropdown menu
  • The values of the selected items are joined using the delimiter property to create the input value
  • A simple value, if provided, will be split using the delimiter property
  • The onChange event provides an array of the selected options as the second argument
  • The first argument to onChange is always a string, regardless of whether the values of the selected options are numbers or strings
  • By default, only options in the options array can be selected. Setting allowCreate to true allows new options to be created if they do not already exist.

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

Filtering options

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 value
  • matchProp: "label", "value" or "any": whether to match the value, label or both values of each option when filtering
  • ignoreCase: Boolean: whether to ignore case or match the text exactly when filtering

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.)

  • 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.

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 <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 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: true clearValueText | string | title for the "clear" control delimiter | string | delimiter to use to join multiple values disableCache | bool | disables the options cache for asyncOptions 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'} 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 tag 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) {} 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 searchPromptText | string | label to prompt for search input value | any | initial field value valueRenderer | func | function which returns a custom way to render the value selected

Contributing

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

License

MIT Licensed. Copyright (c) Jed Watson 2015.

About

A Select control built with and for React JS

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - ngunday/react-select: A Select control built with and for React JS · GitHub
Skip to content

Repository files navigation

Build StatusCoverage 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:

  • CSS Styles and theme support (working, could be improved)
  • Documentation website (currently just examples)
  • Custom options rendering (in progress)

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:

  • Selected options will be removed from the dropdown menu
  • The values of the selected items are joined using the delimiter property to create the input value
  • A simple value, if provided, will be split using the delimiter property
  • The onChange event provides an array of the selected options as the second argument
  • The first argument to onChange is always a string, regardless of whether the values of the selected options are numbers or strings
  • By default, only options in the options array can be selected. Setting allowCreate to true allows new options to be created if they do not already exist.

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

Filtering options

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 value
  • matchProp: "label", "value" or "any": whether to match the value, label or both values of each option when filtering
  • ignoreCase: Boolean: whether to ignore case or match the text exactly when filtering

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.)

  • 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.

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 <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 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: true clearValueText | string | title for the "clear" control delimiter | string | delimiter to use to join multiple values disableCache | bool | disables the options cache for asyncOptions 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'} 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 tag 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) {} 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 searchPromptText | string | label to prompt for search input value | any | initial field value valueRenderer | func | function which returns a custom way to render the value selected

Contributing

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

License

MIT Licensed. Copyright (c) Jed Watson 2015.

About

A Select control built with and for React JS

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - ngunday/react-select: A Select control built with and for React JS · GitHub
Skip to content

Repository files navigation

Build StatusCoverage 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:

  • CSS Styles and theme support (working, could be improved)
  • Documentation website (currently just examples)
  • Custom options rendering (in progress)

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:

  • Selected options will be removed from the dropdown menu
  • The values of the selected items are joined using the delimiter property to create the input value
  • A simple value, if provided, will be split using the delimiter property
  • The onChange event provides an array of the selected options as the second argument
  • The first argument to onChange is always a string, regardless of whether the values of the selected options are numbers or strings
  • By default, only options in the options array can be selected. Setting allowCreate to true allows new options to be created if they do not already exist.

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

Filtering options

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 value
  • matchProp: "label", "value" or "any": whether to match the value, label or both values of each option when filtering
  • ignoreCase: Boolean: whether to ignore case or match the text exactly when filtering

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.)

  • 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.

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 <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 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: true clearValueText | string | title for the "clear" control delimiter | string | delimiter to use to join multiple values disableCache | bool | disables the options cache for asyncOptions 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'} 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 tag 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) {} 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 searchPromptText | string | label to prompt for search input value | any | initial field value valueRenderer | func | function which returns a custom way to render the value selected

Contributing

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

License

MIT Licensed. Copyright (c) Jed Watson 2015.

About

A Select control built with and for React JS

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); GitHub - ngunday/react-select: A Select control built with and for React JS · GitHub
Skip to content

Repository files navigation

Build StatusCoverage 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:

  • CSS Styles and theme support (working, could be improved)
  • Documentation website (currently just examples)
  • Custom options rendering (in progress)

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:

  • Selected options will be removed from the dropdown menu
  • The values of the selected items are joined using the delimiter property to create the input value
  • A simple value, if provided, will be split using the delimiter property
  • The onChange event provides an array of the selected options as the second argument
  • The first argument to onChange is always a string, regardless of whether the values of the selected options are numbers or strings
  • By default, only options in the options array can be selected. Setting allowCreate to true allows new options to be created if they do not already exist.

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

Filtering options

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 value
  • matchProp: "label", "value" or "any": whether to match the value, label or both values of each option when filtering
  • ignoreCase: Boolean: whether to ignore case or match the text exactly when filtering

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.)

  • 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.

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 <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 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: true clearValueText | string | title for the "clear" control delimiter | string | delimiter to use to join multiple values disableCache | bool | disables the options cache for asyncOptions 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'} 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 tag 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) {} 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 searchPromptText | string | label to prompt for search input value | any | initial field value valueRenderer | func | function which returns a custom way to render the value selected

Contributing

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

License

MIT Licensed. Copyright (c) Jed Watson 2015.

About

A Select control built with and for React JS

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages