Canopy React Multi-select Component
- React >=0.13.3
- Lodash methods without, contains, union, isNull
- Install through
npm install --save cpr-multiselect
- Load the required stylesheet
src/multi-selector.css
- Optional - if you want some default styling use the stylesheet:
build/external-styles.css
items
: Items to choose frominitialSelectedItems
: The selected itemsonChange
: Called when selected items changeonInputChange
: Called when the text input changesItemComponent
: Custom component for each item in the dialoggetItemTitle
: Called to display the title of each itemplaceholder
: Placeholder for the text inputmaxLength
: Maximum length for the text inputnoRestrict
: Pass this if you don't want to restrict the input so users can enter custom values. Custom values will be appended toselectedItems
as strings.noResultsPhrase
: The phrase to show when there are no matching results, or whenitems
is empty. The default is "No items found."pressEnterToAddPhrase
: IfnoRestrict
is true, this phrase will be displayed when users enter custom values. The default is "Press Enter to add".customCSSClass
: A css class to be put on the cpr-multiselector element
import MultiSelect from 'cpr-multiselect';
let items = [
{
"lastName": "Seward",
"firstName": "William"
},
{
"lastName": "Montgomery",
"firstName": "Blair"
},
{
"lastName": "Meriwether",
"firstName": "Lewis"
}
];
function itemsChanged(selectedItems) {
console.log(selectedItems);
}
function inputChange(input) {
//Input will be whatever current text is in the input
}
<MultiSelect items={items} onChange={itemsChanged} onInputChange={inputChange}></MultiSelect>
import MultiSelect from 'cpr-multiselect';
let items = [
{
"lastName": "Seward",
"firstName": "William"
},
{
"lastName": "Montgomery",
"firstName": "Blair"
},
{
"lastName": "Meriwether",
"firstName": "Lewis"
}
];
/**
* Each item has a title which can be manipulated with a callback function
*/
function getCustomTitle(item) {
return item.firstName;
}
/**
* Each item rendered in the dialog can have a custom component
*/
let PersonComponent = React.createClass({
render () {
const firstName = this.props.item.firstName;
const lastName = this.props.item.lastName;
return (
<div>
<div>{`${firstName[0]}${lastName[0]}`}</div>
<div className="cpr-multi-selector-item__title">{`${firstName} ${lastName}`}</div>
</div>
)
}
});
<MultiSelect items={items} onChange={itemsChanged} getItemTitle={getCustomTitle} ItemComponent={PersonComponent}></MultiSelect>