|
| 1 | +import React, { useState } from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import Select from "react-select"; |
| 4 | +import AsyncSelect from "react-select/async"; |
| 5 | +import { makeStyles, useTheme } from "@material-ui/core/styles"; |
| 6 | +import { |
| 7 | + Control, |
| 8 | + Menu, |
| 9 | + MultiValue, |
| 10 | + NoOptionsMessage, |
| 11 | + Option, |
| 12 | + Placeholder, |
| 13 | + ValueContainer |
| 14 | +} from "./helpers"; |
| 15 | + |
| 16 | +const useStyles = makeStyles((theme) => ({ |
| 17 | + root: { |
| 18 | + flexGrow: 1, |
| 19 | + height: 250, |
| 20 | + minWidth: 290 |
| 21 | + }, |
| 22 | + input: { |
| 23 | + display: "flex", |
| 24 | + padding: 0, |
| 25 | + height: "auto" |
| 26 | + }, |
| 27 | + valueContainer: { |
| 28 | + display: "flex", |
| 29 | + alignItems: "center", |
| 30 | + flexWrap: "wrap", |
| 31 | + flex: 1, |
| 32 | + overflow: "hidden", |
| 33 | + paddingLeft: theme.spacing(1), |
| 34 | + paddingRight: theme.spacing(1) |
| 35 | + }, |
| 36 | + chip: { |
| 37 | + margin: theme.spacing(0.5, 0.25) |
| 38 | + }, |
| 39 | + noOptionsMessage: { |
| 40 | + padding: theme.spacing(1, 2) |
| 41 | + }, |
| 42 | + placeholder: { |
| 43 | + position: "absolute", |
| 44 | + left: theme.spacing(1), |
| 45 | + fontSize: theme.typography.fontSize |
| 46 | + }, |
| 47 | + paper: { |
| 48 | + position: "absolute", |
| 49 | + zIndex: 1, |
| 50 | + marginTop: theme.spacing(1), |
| 51 | + left: 0, |
| 52 | + right: 0 |
| 53 | + }, |
| 54 | + divider: { |
| 55 | + height: theme.spacing(2) |
| 56 | + } |
| 57 | +})); |
| 58 | + |
| 59 | +// Rather than pass through all props to react-select, we'll keep a whitelist |
| 60 | +// to better control the usage and appearance of this component. |
| 61 | +const supportedPassthroughProps = [ |
| 62 | + "async", |
| 63 | + "cacheOptions", |
| 64 | + "classes", |
| 65 | + "defaultOptions", |
| 66 | + "loadOptions", |
| 67 | + "placeholder", |
| 68 | + "onSelection", |
| 69 | + "options" |
| 70 | +]; |
| 71 | + |
| 72 | +// Custom components for various aspects of the select |
| 73 | +const components = { |
| 74 | + Control, |
| 75 | + Menu, |
| 76 | + MultiValue, |
| 77 | + NoOptionsMessage, |
| 78 | + Option, |
| 79 | + Placeholder, |
| 80 | + ValueContainer |
| 81 | +}; |
| 82 | + |
| 83 | +/** |
| 84 | + * @name MultiSelect |
| 85 | + * @summary A Select component that supports selecting multiple options, and |
| 86 | + * loading options asynchronously and synchronously. |
| 87 | + * @param {Object} props - component props |
| 88 | + * @returns {React.Component} A React component |
| 89 | + */ |
| 90 | +const MultiSelect = React.forwardRef(function MultiSelect(props, ref) { |
| 91 | + const defaultClasses = useStyles(); |
| 92 | + const theme = useTheme(); |
| 93 | + const [value, setValue] = useState(null); |
| 94 | + |
| 95 | + const passThroughProps = {}; |
| 96 | + supportedPassthroughProps.forEach((supportedProp) => { |
| 97 | + passThroughProps[supportedProp] = props[supportedProp]; |
| 98 | + }); |
| 99 | + |
| 100 | + const { classes, isAsync, onSelection } = props; |
| 101 | + const SelectComponent = isAsync ? AsyncSelect : Select; |
| 102 | + |
| 103 | + /** |
| 104 | + * |
| 105 | + * @param {String} selectedValue The selected value |
| 106 | + * @returns {undefined} nothing |
| 107 | + */ |
| 108 | + function handleChangeMulti(selectedValue) { |
| 109 | + setValue(selectedValue); |
| 110 | + onSelection(selectedValue); |
| 111 | + } |
| 112 | + |
| 113 | + const selectStyles = { |
| 114 | + input: (base) => ({ |
| 115 | + ...base, |
| 116 | + "color": theme.palette.text.primary, |
| 117 | + "& input": { |
| 118 | + font: "inherit" |
| 119 | + } |
| 120 | + }) |
| 121 | + }; |
| 122 | + |
| 123 | + return ( |
| 124 | + <div className={defaultClasses.root}> |
| 125 | + <SelectComponent |
| 126 | + classes={{ ...defaultClasses, ...classes }} |
| 127 | + components={components} |
| 128 | + isMulti={true} |
| 129 | + inputId="react-select-multiple" |
| 130 | + onChange={handleChangeMulti} |
| 131 | + ref={ref} |
| 132 | + styles={selectStyles} |
| 133 | + innerRef={ref} |
| 134 | + TextFieldProps={{ |
| 135 | + InputLabelProps: { |
| 136 | + htmlFor: "react-select-multiple", |
| 137 | + shrink: true |
| 138 | + } |
| 139 | + }} |
| 140 | + value={value} |
| 141 | + {...props} |
| 142 | + /> |
| 143 | + </div> |
| 144 | + ); |
| 145 | +}); |
| 146 | + |
| 147 | +MultiSelect.defaultProps = { |
| 148 | + placeholder: "Select options" |
| 149 | +}; |
| 150 | + |
| 151 | +MultiSelect.propTypes = { |
| 152 | + /** |
| 153 | + * When provided options will be cached |
| 154 | + */ |
| 155 | + cacheOptions: PropTypes.bool, // eslint-disable-line react/boolean-prop-naming |
| 156 | + /** |
| 157 | + * Additional classes to customize the Select component |
| 158 | + */ |
| 159 | + classes: PropTypes.string, |
| 160 | + /** |
| 161 | + * The defaultOptions prop determines "when" your remote request is initially fired. |
| 162 | + * There are two valid values for this property. |
| 163 | + * Providing an option array to this prop will populate the initial set of options |
| 164 | + * used when opening the select, at which point the remote load only occurs |
| 165 | + * when filtering the options (typing in the control). |
| 166 | + * Providing the prop by itself (or with 'true') tells the control to immediately |
| 167 | + * fire the remote request, described by your loadOptions, |
| 168 | + * to get those initial values for the Select. |
| 169 | + */ |
| 170 | + defaultOptions: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.object), PropTypes.bool]), |
| 171 | + /** |
| 172 | + * Set to true if options will be fetched asynchronously. |
| 173 | + */ |
| 174 | + isAsync: PropTypes.bool, |
| 175 | + /** |
| 176 | + * A function that returns a Promise which will load the options |
| 177 | + */ |
| 178 | + loadOptions: PropTypes.func, |
| 179 | + /** |
| 180 | + * Function to call when the selected value changes |
| 181 | + */ |
| 182 | + onSelection: PropTypes.func, |
| 183 | + /** |
| 184 | + * The select options |
| 185 | + */ |
| 186 | + options: PropTypes.arrayOf(PropTypes.object), |
| 187 | + /** |
| 188 | + * The placeholder string |
| 189 | + */ |
| 190 | + placeholder: PropTypes.string |
| 191 | +}; |
| 192 | + |
| 193 | +export default MultiSelect; |
0 commit comments