Code Monkey home page Code Monkey logo

multiselect-react-dropdown's Introduction

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 πŸ’¬

Prop Type Default Description
options array [] Dropdown options
onSelect function func Callback function will invoked on select event. Params are selectedList & selectedItem
onRemove function func Callback function will invoked on remove event. Params are selectedList & removedItem
singleSelect boolean false Make it true to behave like a normal dropdown(single select dropdown)
selectedValues array [] Preselected value to persist in dropdown
showCheckbox bool false To display checkbox option in the dropdown
selectionLimit number -1 You can limit the number of items that can be selected in a dropdown
placeholder string Select Placeholder text
disablePreSelectedValues bool false Prevent to deselect the preselected values
isObject bool true Make it false to display flat array of string or number Ex. ['Test1',1]
displayValue string value Property name in the object to display in the dropdown. Refer Basic Usage section
emptyRecordMsg string No options available Message to display when no records found
groupBy string '' Group the popup list items with the corresponding category by the property name in the object
closeIcon string circle Option to select close icon instead of default. Refer Close Icon section
style object {} CSS Customization for multiselect. Refer below object for css customization.
caseSensitiveSearch bool false Enables case sensitivity on the search field.
closeOnSelect bool true Dropdown get closed on select/remove item from options.
id string '' Id for the multiselect container and input field(In input field it will append '{id}_input').
className string '' Class for the multiselect container wrapper.
avoidHighlightFirstOption bool false Based on flag first option will get highlight whenever optionlist open.
hidePlaceholder bool false For true, placeholder will be hidden if there is any selected values in multiselect
disable bool false For true, dropdown will be disabled
onSearch function func Callback function invoked on search in multiselect, helpful to make api call to load data from api based on search.
loading bool false If options is fetching from api, in the meantime, we can show loading... message in the list.
loadingMessage any '' Custom loading message, it can be string or component.
showArrow bool false For multiselect dropdown by default arrow wont show at the end, If required based on flag we can display
customArrow any undefined For multiselect dropdown custom arrow option
keepSearchTerm bool false Whether or not to keep the search value after selecting or removing an item
customCloseIcon ReactNode or string undefined Custom close icon and can be string or react component(Check demo for reference)
selectedValueDecorator (string) => ReactNode | string v => v A function that can be used to modify the representation selected value
optionValueDecorator (string) => string v => v A 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 Name Description
resetSelectedValues Programatically reset selected values and returns promise
getSelectedItems Get all selected items
getSelectedItemsCount Get 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 ❌

Name Image
circle Close Icon
circle2 Close Icon
cancel Close Icon
close Close Icon

8. Licence πŸ“œ

MIT

multiselect-react-dropdown's People

Contributors

anothercodeartist avatar cristibp avatar dependabot[bot] avatar epmichaelmiller avatar fiware-austria avatar jackeysharath avatar jmsaucier avatar johannchopin avatar k-timoshenko avatar limmor1 avatar liqmix avatar mrpeker avatar omt66 avatar rfdlp avatar rself-sf avatar shellylane avatar srigar avatar vkostin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

multiselect-react-dropdown's Issues

Props changed by reference when onRemoveItem is called

The following line modifies the selectedValues in place thus causing the props to be changed by reference and potentially affecting the calling code. If splice is needed, then a shallow copy of the variable should be made before.

This is likely because componentDidUpdate does not make a shallow copy when assigning the value to the local state, while the constructor does.

Constructor:

selectedValues: Object.assign([], props.selectedValues),

ComponentDidUpdate:

this.setState({ selectedValues, preSelectedValues: selectedValues }, this.initialSetValue);

SSR fails in Gatsby build

Hi,

I'm trying to use your component, but it is failing during build in Gatsby.

error "window" is not available during server side rendering.
ReferenceError: window is not defined

Should I use lib such as @loadable/component or is there something to make module working in SSR?

Is there a support for TypeScript ? if so how to handle reset and on change Events

Can you please help on this ?

//constructor code

private multiselectRef: React.RefObject
constructor(props:MyProps) {
super(props);
this.multiselectRef= React.createRef();
}

//Method for
getSelectedValues=()=>{
const a : HTMLSelectElement = this.multiselectRef.current as HTMLSelectElement ;
a.resetSelectedValues();
}

//component calling

Dropdown multi-select with checkbox dismisses the options

Hi Srigar,

When you choose the multi select with checkbox mode and click on an option's checkbox, it hides the list immediately and adds the chip. It could be better if you let the user select all items without having to click on the drop down field to re-show the options.

Makes sense? Let me know how I can help.

~~
Manoj.

Each child in a list should have a unique "key" prop

Hello,
I get this warning when using this library:

Warning: Each child in a list should have a unique "key" prop

I added a unique key to each item I'm rendering in the dropdown, but this warning doesn’t go away. I also followed the examples you have, but this issue is still there. Are you providing a unique key for each item in the dropdown list in the source code?

Add Ability To Hide Placeholder Text on Select

Would you consider adding a prop to allow the placeholder text to be hidden if there are any values selected? I'd like to just be able to show the text if there are currently no options selected, it's a pretty common feature.

Doesn't work on API data

When i fetch data from an api and set the state and use that state in multiselect comopnent it show me no option available i also correct the format but it only work on static data???? any idea???

Option to pass 'id' for the input filed

Hi,

For eslint and w3c validation it would be a great feature if we could pass along an 'id' for the input filed.
Something like this:

                    <label htmlFor="my-id" className="form__row">
                        <span className="form__label visually-hidden">
                          My list
                        </span>
                        <Multiselect
                          id="my-id"
                          name="my-dropdown"
                          placeholder="Dropdown"
                          options={list}
                          closeIcon="close"
                          displayValue="displayText"
                        />
                    </label>

So the generated input filed would have and id="my-id"

<input type="text" id="my-id" class="searchBox" placeholder="Portfolio" value="">
Thank you!

Selection doesn't occur on single click?

Hi,
I am using the dropdown with checkboxes, but for some reason I am unable to select/deselect items by single click either on text or checkbox. Works only on double click. Is it by design?

searchWrapper removeEventListener()

I'm getting error on searchWrapper.current.removeEventListener() : Failed to execute 'removeEventListener' on 'EventTarget': 2 arguments required, but only 0 present.

Issue with search in Multiselect with checkbox

The issue happens in the example 5. Multiselect with checkbox.

  1. Open the list and type "3"
  2. Click on "Option 3"
  3. Open the list again and find just "Option 3" as available option (bug)
  4. Type any character and delete it to show all the options again.

How to use in JSX

can you please provide a sample?
We have below mentioned structure :)

`import React, { useEffect, useState, useCallback, useRef } from 'react';
import { hot } from 'react-hot-loader';
import { Multiselect } from "multiselect-react-dropdown";

const DropDown = () => {

useEffect(() => {
}, []);
return (

Hello

);
}

export default hot(module)(DropDown)`

Different colours of chips based on index and colour array

Hi,

Is there anyway to have the chips be of different colours based on the position in the selected list and an array of colours as in input?

I want this dropdown to be show the selected series on a chart and I need the chip background colour to be the same as the series graph

Programmatically deselect selections

Is there a way to programmatically deselect manually set options?
In addition, is there a way to programmatically select options AFTER the component has been loaded (selectedValues will still be empty).

If not, how and when can this be added?

Issues with onBlur functionality

If we have a target element, say a Multiselect Dropdown, when we click into the element and then if we click outside of this element or any other element, the focus shift to recently clicked element but the previous dropdown remains open.

MultiSelect showing me warning: Failed prop type: You provided a `checked` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultChecked`. Otherwise, set either `onChange` or `readOnly`.

I am using this library for multiselect dropdown with check box. I am able to see dropdown and can select multiple options too.. And at the same time, I am getting the warning that Failed prop type: You provided a checked prop to a form field without an onChange handler. This will render a read-only field. If the field should be mutable use defaultChecked. Otherwise, set either onChange or readOnly.

This is my code:

<Multiselect 
	options={OPTIONS_DATA}
	displayValue="key"
	showCheckbox={true}
	onSelect={this.onSelect}
/>

where OPTIONS_DATA is my array object and onSelect is my method..
Please help me out.. How can I fix this!!!!

Input field scrolls to top of screen when search starts

When I type into the input field (to search), the screen scrolls such that the input field is at the very top of the screen. Any way to prevent this from happening?

(The same behavior happens in the demo code for the very first dropdown: as soon as you type a letter into the input field, the screen scrolls so that the input field is at the very top of the screen)

image

Duplicate id when using multiple Multiselects

There is an id-attribute in the Multiselect-component, which is hard-coded and cannot be changed:

id="multiselectContainerReact"

So if you have two Multiselect-components on a page, then you also have two duplicate id:s on the page. This should be fixed since id:s have to be unique.

Not working for keyboard

It is not working for keyboard sir. if i enter a key from keyboard it is not working rather it sent me to the bottom of the page any solution if u have kindly provide ASAP. Thanku

Need ability to disable all user input

I need the ability to disable user input. A simple property named disabled could be sent that prevents any user interaction with the control. Right now, I am approximating this behavior by setting the options equal to the selectedValues and disablePreSelectedValues to true.

However, the dropdown still appears on click so I set the emptyRecordMsg to "". Unfortunately, it still appears but it is blank.

Ability to customize checkbox

Hopefully there would be a way to customize checkbox.
Maybe even encapsulating the text within a label tag would make it possible already

not able get id value for options

normally every option in a select dropdown would have a value and a text currently, with the 'displayValue' prop I am only getting the name which is correct since the options should only display the name and not the value but in the backend, I would actually need the value of the option selected. I am not seeing any prop which would get me the value of the option

Single select with preselected values

Hi Guys,

Thanks for awesome multiselect.

I have found one case when I put options ['A','B','C'] with preselected value 'A' and single select flag true. Once the dropdown shows up with preselect value 'A', the dropdown only shows ['B', 'C'] which is expected. However, when you select 'B' or 'C', the dropdown never show 'A' in the options. Also, 'B' or 'C' are still there.

Is this expected or not?

Preselected Not working

I am using the version 1.3.0 as suggested by you but in that version the preselected attribute is not working can you plz guide me on this???

Unselect values without clicking on close icon

Hi, thank you for this helpful package.
I'm using the multiselect-react-dropdown as a catalog filter, and I wanted to know if there is a way to unselect values, not by clicking on the close icon of the selected value, but by clicking on my own filter tag.
Below is my example:
I have selected the 'Auto' value from the dropdown options.

dropdown

I want to unselect it by clicking on my own filter tag:

dropdown1

Thank you !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.