项目作者: bhushan90

项目描述 :
Simple searchable single/ multi-select dropdown for react material.
高级语言: JavaScript
项目地址: git://github.com/bhushan90/react-mui-multiselect-dropdown.git


react-mui-multiselect-dropdown

It is simple and light weight single/multiselect searchable dropdown library.
This package is built using popular react UI library which is material UI.

Dependencies

react: v-16.13.1,
material-ui/core: v-4.11.0,
material-ui/icons: v-4.9.1,
classnames: “^2.2.6”

Install

  1. npm install --save react-mui-multiselect-dropdown

Demo

here

Usage

class Component

  1. import React, { Component } from 'react'
  2. import Dropdown from 'react-mui-multiselect-dropdown'
  3. class Example extends Component {
  4. state = {
  5. employees: [],
  6. selectedEmployees: []
  7. }
  8. componentDidMount() {
  9. const employeesData = [
  10. { id: 1, name: 'John' },
  11. { id: 2, name: 'Roy' },
  12. { id: 3, name: 'Albert' }
  13. ]
  14. this.setState({
  15. employees: employeesData
  16. });
  17. }
  18. render(){
  19. return (
  20. <Dropdown
  21. data={this.state.employees}
  22. fullWidth
  23. searchable
  24. searchPlaceHolder='search employee...'
  25. itemId='id'
  26. itemLabel='name'
  27. simpleValue
  28. searchByValue='name'
  29. itemValue='id'
  30. selectedValues={this.state.selectedEmployees}
  31. errorText='error'
  32. onItemClick={(records) => {
  33. this.setState({
  34. selectedEmployees: records
  35. })
  36. }}
  37. onDeleteItem={(deleted) => {
  38. console.log('deleted', deleted)
  39. }}
  40. />
  41. )
  42. }
  43. }
  44. export default Example;

Functional Component

Single select

  1. import React, { useState, useEffect } from 'react'
  2. import Dropdown from 'react-mui-multiselect-dropdown'
  3. function Example() {
  4. const [selectedEmployees, setSelectedEmployees] = useState([])
  5. const [employees, setEmployees] = useState([])
  6. const populateData = () => {
  7. const employeesData = [
  8. { id: 1, name: 'Klaus' },
  9. { id: 2, name: 'Elijah' },
  10. { id: 3, name: 'Marcel' }
  11. ]
  12. setEmployees(employeesData)
  13. }
  14. useEffect(() => {
  15. populateData()
  16. }, [])
  17. return (
  18. <Dropdown
  19. data={employees}
  20. fullWidth
  21. searchable
  22. searchPlaceHolder='search employee...'
  23. itemId='id'
  24. itemLabel='name'
  25. simpleValue
  26. searchByValue='name'
  27. itemValue='id'
  28. selectedValues={selectedEmployees}
  29. errorText='error'
  30. onItemClick={(emp) => {
  31. setSelectedEmployees(emp)
  32. }}
  33. onDeleteItem={(deleted) => {
  34. console.log('deleted', deleted)
  35. }}
  36. />
  37. )
  38. }
  39. export default Example

Multi select

  1. import React, { useState, useEffect } from 'react'
  2. import Dropdown from 'react-mui-multiselect-dropdown'
  3. import { makeStyles } from '@material-ui/core'
  4. import purple from '@material-ui/core/colors/purple'
  5. const useStyles = makeStyles((theme) => ({
  6. error: {
  7. color: theme.palette.error.dark,
  8. fontSize: '1em'
  9. },
  10. checkBox: {
  11. color: purple['700']
  12. }
  13. }))
  14. function Example() {
  15. const [skills, setSkills] = useState([])
  16. const [selectedSkills, setSelectedSkills] = useState([])
  17. const classes = useStyles()
  18. const populateData = () => {
  19. const skillsData = [
  20. { id: 1, name: 'React Js' },
  21. { id: 2, name: 'Angular' },
  22. { id: 3, name: 'Node JS' }
  23. ]
  24. setSkills(skillsData)
  25. }
  26. useEffect(() => {
  27. populateData()
  28. }, [])
  29. return (
  30. <Dropdown
  31. data={skills}
  32. fullWidth
  33. searchable
  34. searchPlaceHolder='search...'
  35. itemId='id'
  36. itemLabel='name'
  37. multiple
  38. simpleValue
  39. searchByValue='name'
  40. itemValue='id'
  41. selectedValues={selectedSkills}
  42. customStyles={{
  43. error: classes.error,
  44. checkBox: classes.checkBox
  45. }}
  46. errorText='error'
  47. onItemClick={(skill) => {
  48. setSelectedSkills(skill)
  49. }}
  50. onDeleteItem={(deleted) => {
  51. console.log('deleted', deleted)
  52. }}
  53. />
  54. )
  55. }
  56. export default Example

Dropdown with images

  1. import React, { useState, useEffect } from 'react'
  2. import Dropdown from 'react-mui-multiselect-dropdown'
  3. function Example() {
  4. const [cities, setCities] = useState([])
  5. const [selectedCities, setSelectedCities] = useState([])
  6. const populateData = () => {
  7. const cities = [
  8. { id: 1, city: 'MUMBAI' },
  9. { id: 2, city: 'PUNE' },
  10. { id: 3, city: 'NAGPUR' }
  11. ]
  12. cities.forEach((v, i) => {
  13. v['path'] = `https://source.unsplash.com/random/${i}`
  14. })
  15. setCities(cities)
  16. }
  17. useEffect(() => {
  18. populateData()
  19. }, [])
  20. return (
  21. <Dropdown
  22. data={cities}
  23. fullWidth
  24. searchable
  25. searchPlaceHolder='search cities...'
  26. itemId='id'
  27. itemLabel='city'
  28. imageLabel='path'
  29. title='Select City'
  30. searchByValue='city'
  31. showImage
  32. selectedValues={selectedCities}
  33. errorText='error'
  34. onItemClick={(city) => {
  35. setSelectedCities(city)
  36. }}
  37. onDeleteItem={(deleted) => {
  38. console.log('deleted', deleted)
  39. }}
  40. />
  41. )
  42. }
  43. export default Example

Props

Attribute Type Is Required Description Default Value
data array Yes List of options to display
selectedValues array No List of pre selected options. This can be use while edit functionality
searchable boolean No By setting this to true dropdown will be searchable. false
searchPlaceHolder string No Placeholder for the search input Search result
searchByValue string No The parameter from the data you want to saerch with. for instance your data has name and id fields and you want to search data with id then set it as id name
itemLabel string Yes Label of the dropdown values eg. name
itemId number/ string Yes Unique id from to data which will use as unique key for the list it ca be id from the list of data.
simpleValue boolean No If true then provided itemValue will be return in respose from the selected options else whole selected object will return. eg: If item value is ‘id’ and simple value is true then we will get selected option as list of id from the data.Else will get whole object.
itemValue number/string No It will only work if the simple value is true it can be any property from the data for instance id you will get list of id’s on item select.
multiple boolean No If true then you can select multiple options false
showAllButton boolean No If true there there is option “All” to select/deselect all option o one click. true
showImage boolean No You can add imgae at the start of all options, the image url should be in the data list for each object. Name of the parameter should be url if you have diffenet name then specify in the imageLabel prop. false
imageLabel string No Parameter use for the image url it can be image parameter from the data list. url
title string No Title display above dropdown Dropdown
notFoundText string No Text to display when no item found on search No records found
disabled boolean No Disabled the dropdown if true False
error boolean No Display the error when true False
errorText string No Text to display when there is an error(error message) Error
customStyles makeStyle Instance No Custom styles for the checkbox below there is list of custom classes which you can modified. {}

Events

Event Return Description
onItemClick SeletedItem(s) Once you click the option in dropdown this method get called It will return the slected item(s). If the simple value is true then it will return the itemId else whole object or array of object if multiple.
onDeleteItem Deleted item It will get trigger when you deSelect the selected item.return value is itemId in case of simple value else whole object.

Custom styles

You can modified the styles of the checkbox or error message
You only need to do something like this

  1. customStyles={{
  2. error: classes.error,
  3. checkBox: classes.checkBox
  4. }}

classes.error and classses.checkbox is your predefined style which you can add in makeStyles.