|
| 1 | +import { useState } from 'react' |
| 2 | +import { useSelector } from 'react-redux' |
| 3 | + |
| 4 | +import { Popper, FormControl, TextField, Box, Typography } from '@mui/material' |
| 5 | +import Autocomplete, { autocompleteClasses } from '@mui/material/Autocomplete' |
| 6 | +import { styled } from '@mui/material/styles' |
| 7 | +import PropTypes from 'prop-types' |
| 8 | + |
| 9 | +const StyledPopper = styled(Popper)({ |
| 10 | + boxShadow: '0px 8px 10px -5px rgb(0 0 0 / 20%), 0px 16px 24px 2px rgb(0 0 0 / 14%), 0px 6px 30px 5px rgb(0 0 0 / 12%)', |
| 11 | + borderRadius: '10px', |
| 12 | + [`& .${autocompleteClasses.listbox}`]: { |
| 13 | + boxSizing: 'border-box', |
| 14 | + '& ul': { |
| 15 | + padding: 10, |
| 16 | + margin: 10 |
| 17 | + } |
| 18 | + } |
| 19 | +}) |
| 20 | + |
| 21 | +export const MultiDropdown = ({ name, value, options, onSelect, disabled = false, disableClearable = false }) => { |
| 22 | + const customization = useSelector((state) => state.customization) |
| 23 | + const findMatchingOptions = (options = [], internalValue) => { |
| 24 | + let values = [] |
| 25 | + if (internalValue && typeof internalValue === 'string') values = JSON.parse(internalValue) |
| 26 | + else values = internalValue |
| 27 | + return options.filter((option) => values.includes(option.name)) |
| 28 | + } |
| 29 | + const getDefaultOptionValue = () => [] |
| 30 | + let [internalValue, setInternalValue] = useState(value ?? []) |
| 31 | + |
| 32 | + return ( |
| 33 | + <FormControl sx={{ mt: 1, width: '100%' }} size='small'> |
| 34 | + <Autocomplete |
| 35 | + id={name} |
| 36 | + disabled={disabled} |
| 37 | + disableClearable={disableClearable} |
| 38 | + size='small' |
| 39 | + multiple |
| 40 | + filterSelectedOptions |
| 41 | + options={options || []} |
| 42 | + value={findMatchingOptions(options, internalValue) || getDefaultOptionValue()} |
| 43 | + onChange={(e, selections) => { |
| 44 | + let value = '' |
| 45 | + if (selections.length) { |
| 46 | + const selectionNames = [] |
| 47 | + for (let i = 0; i < selections.length; i += 1) { |
| 48 | + selectionNames.push(selections[i].name) |
| 49 | + } |
| 50 | + value = JSON.stringify(selectionNames) |
| 51 | + } |
| 52 | + setInternalValue(value) |
| 53 | + onSelect(value) |
| 54 | + }} |
| 55 | + PopperComponent={StyledPopper} |
| 56 | + renderInput={(params) => <TextField {...params} value={internalValue} />} |
| 57 | + renderOption={(props, option) => ( |
| 58 | + <Box component='li' {...props}> |
| 59 | + <div style={{ display: 'flex', flexDirection: 'column' }}> |
| 60 | + <Typography variant='h5'>{option.label}</Typography> |
| 61 | + {option.description && ( |
| 62 | + <Typography sx={{ color: customization.isDarkMode ? '#9e9e9e' : '' }}>{option.description}</Typography> |
| 63 | + )} |
| 64 | + </div> |
| 65 | + </Box> |
| 66 | + )} |
| 67 | + /> |
| 68 | + </FormControl> |
| 69 | + ) |
| 70 | +} |
| 71 | + |
| 72 | +MultiDropdown.propTypes = { |
| 73 | + name: PropTypes.string, |
| 74 | + value: PropTypes.string, |
| 75 | + options: PropTypes.array, |
| 76 | + onSelect: PropTypes.func, |
| 77 | + disabled: PropTypes.bool, |
| 78 | + disableClearable: PropTypes.bool |
| 79 | +} |
0 commit comments