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

react-selectize

Package Overview
Dependencies
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-selectize

react implementation of selectize

  • 0.2.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12K
decreased by-4.15%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status Coverage Status

Motivation

  • existing components do not behave like built-in React.DOM.* components.
  • existing components synchronize props with state an anti pattern, which makes them prone to bugs & difficult for contributers to push new features without breaking something else.
  • use React Selectize as an example for other components, to build stateless, bug free components, that are easy to maintain and contribute too.
  • more features.

React Selectize

ReactSelectize is a stateless Select component for ReactJS, that provides a platform for the more developer friendly SimpleSelect & MultiSelect components.

Both SimpleSelect & MultiSelect have been designed to work as drop in replacement for the built-in React.DOM.Select component.

styles & features inspired by React Select & Selectize.

LIVE DEMO: furqanZafar.github.io/react-selectize

Features

Install

npm install react-selectize

Usage (livescript)

{create-factory}:React = require \react
{SimpleSelect, MultiSelect, ReactSelectize} = require \react-selectize
SimpleSelect = create-factory SimpleSelect
MultiSelect = create-factory MultiSelect
.
.
.
SimpleSelect do     
    placeholder: 'Select a fruit'
    options: <[apple mango orange banana]> |> map ~> label: it, value: it
    on-value-change: (value, callback) ~>
        alert value
        callback!
.
.
.
MultiSelect do
    placeholder: 'Select fruits'
    options: <[apple mango orange banana]> |> map ~> label: it, value: it
    on-values-change: (values, callback) ~>
        alert values
        callback!

Usage (jsx)

React = require("react");
ReactSelectize = require("react-selectize");
SimpleSelect = React.createFactory(ReactSelectize.SimpleSelect);
MultiSelect = React.createFactory(ReactSelectize.MultiSelect);
.
.
.
<SimpleSelect
    placeholder = "Select a fruit"
    onValueChange = {function(value, callback){
        alert(value);
        callback();
    }}
>
    <option value = "apple">apple</option>
    <option value = "mango">mango</option>
    <option value = "orange">orange</option>
    <option value = "banana">banana</option>
</SimpleSelect>
.
.
.
// Note: options can be passed as props as well, for example
<MultiSelect
    placeholder = "Select fruits"
    options = ["apple", "mango", "orange", "banana"].map(function(fruit){
        return {label: fruit, value: fruit};
    });
    onValuesChange = {function(values, callback){
        alert(values);
        callback();
    }}
/>

Usage (stylus)

to include the default styles when using SimpleSelect component, add the following import statement to your stylus file:

@import 'node_modules/react-selectize/src/SimpleSelect.css'

to include the default styles when using MultiSelect component, add the following import statement to your stylus file:

@import 'node_modules/react-selectize/src/MultiSelect.css'

Gotchas

  • the default structure of an option object is {label: String, value :: a} where a implies that value property can be of any equatable type

  • SimpleSelect notifies change via onValueChange prop whereas MultiSelect notifies change via onValuesChange prop

  • the onValueChange callback for SimpleSelect is passed 2 parameters. the selected option object (instead of the value property of the option object) and a callback

  • the onValuesChange callback for MultiSelect is passed 2 parameters an Array of selected option objects (instead of a collection of the value properties or a comma separated string of value properties) and a callback

  • all the on*Change functions receive a callback as the final parameter, which MUST always be invoked, for example when using state for the value prop of SimpleSelect the onValueChange callback implementation would look like this:

value = {{label: "apple", value: "apple"}}
onValueChange = {function(value, callback){
    self.setState(value, callback);
}}

when relying on the components internal state for managing the value:

onValueChange = {function(value, callback){
    console.log(value);
    callback(); // must invoke callback    
}}
  • when using custom option object, you MUST implement the uid function which accepts an option object and returns a unique id, for example:
// assuming the type of our option object is:
// {firstName :: String, lastName :: String, age :: Int}
uid = {function(item){
    return item.firstName + item.lastName;    
}}

the uid function is used internally for faster performance by avoiding unnecessary option & value rendering.

SimpleSelect props

PropertyTypeDescription
autosizeInputElement -> Intfunction($search){return $search.value.length * 10} custom logic for autosizing the input element
classNameStringclass name for the outer element, in addition to "simple-select"
disabledBooleandisables interaction with the Select control
dropdownDirectionIntdefaults to 1, setting it to -1 opens the dropdown upward
createFromSearch[Item] -> String -> Item?implement this function to create new items on the fly, function(options, search){return {label: search, value: search}}, return null to avoid option creation for the given parameters
filterOptions[Item]-> String -> [Item]implement this function for custom synchronous filtering logic, function(options, search) {return options}
groupIdItem -> bfunction(item){return item.groupId} this function is used to identify which group an option belongs to, it must return a value that matches the groupId property of an object in the groups collection
groups[Group]collection of objects where each object must atleast have a groupId property
groupsAsColumnsBooleandisplay option groups in columns
onBlurItem -> String -> Voidfunction(value, reason){} reason can be either "click" (loss of focus because the user clicked elsewhere), "tab" or "blur" (invoked refs.simpleSelect.blur())
onFocusItem -> String -> Voidfunction(value, reason){} reason can be either "event" (when the control gains focus outside) or "focus" (when the user invokes refs.simpleSelect.focus())
onSearchChangeString -> (a -> Void) -> Voidfunction(search, callback){self.setState({search: search}, callback);} or function(search,callback){callback();} i.e. callback MUST always be invoked
onValueChangeItem -> (a -> Void) -> Voidfunction(selectedValue, callback){self.setState({selectedValue: selectedValue}, callback)} or function(value, callback){callback()} i.e. callback MUST always be invoked
options[Item]list of items by default each option object MUST have label & value property, otherwise you must implement the render* & filterOptions methods
placeholderStringdisplayed when there is no value
renderNoResultsFoundItem -> String -> ReactElementfunction(item, search){return React.DOM.div(null);} returns a custom way for rendering the "No results found" error
renderGroupTitleInt -> Group -> ReactElementfunction(index, group){return React.DOM.div(null)} returns a custom way for rendering the group title
renderOptionItem -> ReactElementfunction(item){return React.DOM.div(null);} returns a custom way for rendering each option
renderValueItem -> ReactElementfunction(item){return React.DOM.div(null);} returns a custom way for rendering the selected value
restoreOnBackspaceItem -> Stringfunction(item){return item.label;} implement this method if you want to go back to editing the item when the user hits the [backspace] key instead of getting removed
searchStringthe text displayed in the search box
styleObjectthe CSS styles for the outer element
uid(Eq e) => Item -> efunction(item){return item.value} returns a unique id for a given option, defaults to the value property
valueItemthe selected value, i.e. one of the objects in the options array

SimpleSelect methods

MethodTypeDescription
focusa -> (a -> Void) -> Voidthis.refs.selectInstance.focus(callback) opens the list of options and positions the cursor in the input control, the callback fired when the options menu becomes visible
highlightFirstSelectableOptiona -> Voidthis.refs.selectInstance.highlightFirstSelectableOption()
valuea -> Itemthis.refs.selectInstance.value() returns the current selected item

MultiSelect props

In addition to the props above

PropertyTypeDescription
anchorItempositions the cursor ahead of the anchor item, set this property to undefined to lock the cursor at the start
createFromSearch[Item] -> [Item] -> String -> Item?function(options, values, search){return {label: search, value: search}}
filterOptions[Item] -> [Item] -> String -> [Item]function(options, values, search){return options}
onAnchorChangeItem -> (a -> Void) -> Voidfunction(anchor, callback){callback();} implement this method if you want to override the default behaviour of the cursor
onBlur[Item] -> String -> Voidfunction(values, reason){}
onFocus[Item] -> String -> Voidfunction(values, reason){}
onValuesChange[Item] -> (a -> Void) -> Voidfunction(values, callback){callback();}
maxValuesIntthe maximum values that can be selected, after which the control is disabled
closeOnSelectBooleanas the name implies, closes the options list on selecting an option

MultiSelect methods

same as SimpleSelect but use this.refs.multiSelectInstance.values() to get the selected values instead of the value method.

HighlightedText props

used for search highlighting

PropertyTypeDescription
partitions[[Int, Int, Boolean]]collection of ranges which should or should not be highlighted, its the result of the partitionString method of the prelude-extension library
textStringthe string that is partitioned, the partitions collection above only has the ranges & so we need to pass the original text as well
styleinline CSS styles objectinline styles applied to the root node
highlightStyleinline CSS styles objectinline styles applied to the highlighted spans

Development

  • npm install
  • gulp
  • visit localhost:8000
  • npm test , npm run coverage for unit tests & coverage
  • for production build/test run MINIFY=true gulp

Keywords

FAQs

Package last updated on 26 Sep 2015

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