BETA

srigar/multiselect-react-dropdown

React multiselect dropdown with search and various options


180
69
34.6K
GitHubNPMWebsite

REACT MULTISELECT DROPDOWN

Storybook Version Downloads License gzip Tweet

πŸ’₯πŸ’₯πŸ’₯ React Library for Component Lazyloading. Tiny and Efficient. Check it Out πŸ’₯πŸ’₯πŸ’₯

Description

A React component which provides multi select functionality with various features like selection limit, CSS customization, checkbox, search option, disable preselected values, flat array, keyboard navigation for accessibility and grouping features. Also it has feature to behave like normal dropdown(means single select dropdown).

Multiselect

πŸŽ‰πŸŽ‰ New features in >=2.0.0

✨ SSR Support
πŸƒ Light weight
πŸš€ Typescript

πŸ³οΈβ€πŸŒˆ Getting Started

1. Installation πŸ”§

npm install multiselect-react-dropdown  

yarn add multiselect-react-dropdown

2. Demo πŸ‘οΈ

React-multi-select-dropdown


3. Basic Usage πŸ“‘

import Multiselect from 'multiselect-react-dropdown';

this.state = {
    options: [{name: 'Option 1️⃣', id: 1},{name: 'Option 2️⃣', id: 2}]
};

<Multiselect
options={this.state.options} // Options to display in the dropdown
selectedValues={this.state.selectedValue} // Preselected value to persist in dropdown
onSelect={this.onSelect} // Function will trigger on select event
onRemove={this.onRemove} // Function will trigger on remove event
displayValue="name" // Property name to display in the dropdown options
/>

onSelect(selectedList, selectedItem) {
    ...
}

onRemove(selectedList, removedItem) {
    ...
}

4. Props πŸ’¬

PropTypeDefaultDescription
optionsarray[]Dropdown options
onSelectfunctionfuncCallback function will invoked on select event. Params are selectedList & selectedItem
onRemovefunctionfuncCallback function will invoked on remove event. Params are selectedList & removedItem
singleSelectbooleanfalseMake it true to behave like a normal dropdown(single select dropdown)
selectedValuesarray[]Preselected value to persist in dropdown
showCheckboxboolfalseTo display checkbox option in the dropdown
selectionLimitnumber-1You can limit the number of items that can be selected in a dropdown
placeholderstringSelectPlaceholder text
disablePreSelectedValuesboolfalsePrevent to deselect the preselected values
isObjectbooltrueMake it false to display flat array of string or number Ex. ['Test1',1]
displayValuestringvalueProperty name in the object to display in the dropdown. Refer Basic Usage section
emptyRecordMsgstringNo options availableMessage to display when no records found
groupBystring''Group the popup list items with the corresponding category by the property name in the object
closeIconstringcircleOption to select close icon instead of default. Refer Close Icon section
styleobject{}CSS Customization for multiselect. Refer below object for css customization.
caseSensitiveSearchboolfalseEnables case sensitivity on the search field.
closeOnSelectbooltrueDropdown get closed on select/remove item from options.
idstring''Id for the multiselect container and input field(In input field it will append '{id}_input').
classNamestring''Class for the multiselect container wrapper.
avoidHighlightFirstOptionboolfalseBased on flag first option will get highlight whenever optionlist open.
hidePlaceholderboolfalseFor true, placeholder will be hidden if there is any selected values in multiselect
disableboolfalseFor true, dropdown will be disabled
onSearchfunctionfuncCallback function invoked on search in multiselect, helpful to make api call to load data from api based on search.
loadingboolfalseIf options is fetching from api, in the meantime, we can show loading... message in the list.
loadingMessageany''Custom loading message, it can be string or component.
showArrowboolfalseFor multiselect dropdown by default arrow wont show at the end, If required based on flag we can display
customArrowanyundefinedFor multiselect dropdown custom arrow option
keepSearchTermboolfalseWhether or not to keep the search value after selecting or removing an item
customCloseIconReactNode or stringundefinedCustom close icon and can be string or react component(Check demo for reference)
selectedValueDecorator(string) => ReactNode | stringv => vA function that can be used to modify the representation selected value
optionValueDecorator(string) => stringv => vA function that can be used to modify the representation the available options
----

5. Ref as a prop πŸ“Œ

By using React.createRef() or useRef(), able to access below methods to get or reset selected values

Method NameDescription
resetSelectedValuesProgramatically reset selected values and returns promise
getSelectedItemsGet all selected items
getSelectedItemsCountGet selected items count
constructor() {
  this.multiselectRef = React.createRef();
}

resetValues() {
  // By calling the belowe method will reset the selected values programatically
  this.multiselectRef.current.resetSelectedValues();
}

<Multiselect
options={this.state.options} // Options to display in the dropdown
ref={this.multiselectRef}
/>


6. CSS Customization 🌈

{
  multiselectContainer: { // To change css for multiselect (Width,height,etc..)
	....
  },
  searchBox: { // To change search box element look
	border: none;
	font-size: 10px;
	min-height: 50px;
  },
  inputField: { // To change input field position or margin
      margin: 5px;
  },
  chips: { // To change css chips(Selected options)
	background: red;
  },
  optionContainer: { // To change css for option container 
	border: 2px solid;
  }
  option: { // To change css for dropdown options
	color: blue;
  },
  groupHeading: { // To chanage group heading style
	....
  }
}

7. Close Icons ❌

NameImage
circleClose Icon
circle2Close Icon
cancelClose Icon
closeClose Icon

8. Licence πŸ“œ

MIT