Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-select-checked

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-select-checked

A React select based on JedWatson/React-Select with checkmarks on selected options.

  • 0.1.12
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
87
increased by31.82%
Maintainers
1
Weekly downloads
 
Created
Source

React-Select-Checked

A React select based on JedWatson/React-Select with checkmarks on selected options.

Installation

npm install react-select-checked --save

At this point you can import react-select-checked in your application as follows:

import CheckedSelect from 'react-select-checked';

Usage

React-Select-Checked 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(selectedValueOrValues) will fire, allowing you to re-render with an updated value= prop.

var CheckedSelect = require('react-select-checked');

var options = [
    {label: 'Chocolate', value: 'chocolate', disabled: true},
    {label: 'Vanilla', value: 'vanilla'},
    {label: 'Strawberry', value: 'strawberry'},
    {label: 'Caramel', value: 'caramel'},
];

var currentSelection = [{label: 'Caramel', value: 'caramel'}];

function logChange(val) {
  console.log('Selected value: ', val);
  setState({currentSelection: val});
}

<CheckedSelect
  name="form-field-name"
  value={currentSelection}
  options={options}
  onChange={logChange}
/>

Async options

If you want to load options asynchronously, set async attribute to true and 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: [] }.

function logChange(val) {
  console.log('Selected value: ', val);
  setState({value: val});
}

function getOptions (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
        });
    }, 1000);
}

<CheckedSelect
    async
    loadOptions={getOptions}
    onChange={logChange}
    placeholder="Select your favourite(s)"
    value={value}
/>

Async options with Promises

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

function logChange(val) {
  console.log('Selected value: ', val);
  setState({value: val});
}

function getGitHubUsers(input) {
    return fetch(
        'https://api.github.com/search/repositories?q=stars:%3E1+language:javascript&sort=stars&order=desc&type=Repositories'
    ).then(
        response => {
           return response.json();
        }).then(json => {
            let githubUsers = json.items.map(user => {
                return {
                    label: user.full_name,
                    value: user.name
                };
            });
            return { options: githubUsers };
        });
}

<CheckedSelect
    async
    loadOptions={getGitHubUsers}
    onChange={logChange}
    placeholder="Select your favourite(s)"
    value={value}
/>

Further options

PropertyTypeDefaultDescription
addAllTitlestring'Add all'text to display in the Add all button
asyncboolfalseif this property is specified then a loadOptions method should also be used.
clearAllTitlestring'Clear'text to display in the Clear button
disabledboolfalsewhether the CheckedSelect is disabled or not
ignoreAccentsbooltruewhether to strip accents when filtering
ignoreCasebooltruewhether to perform case-insensitive filtering
loadOptionsfuncundefinedunction that returns a promise or calls a callback with the options: function(input, [callback])
namestringundefinedfield name, for hidden <input /> tag
noResultsTextstringdefault in react-select (as of 4/2018, it's '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)
onChangefuncundefinedonChange handler: function(newOption) {}
optionsarrayundefinedarray of options
placeholderstring|node'Please select ..'field placeholder, displayed when there's no value

Keywords

FAQs

Package last updated on 23 Nov 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc