@@ -59,15 +59,27 @@ export const AsyncDropdown = ({
5959 credentialNames = [ ] ,
6060 disabled = false ,
6161 freeSolo = false ,
62- disableClearable = false
62+ disableClearable = false ,
63+ multiple = false
6364} ) => {
6465 const customization = useSelector ( ( state ) => state . customization )
6566
6667 const [ open , setOpen ] = useState ( false )
6768 const [ options , setOptions ] = useState ( [ ] )
6869 const [ loading , setLoading ] = useState ( false )
69- const findMatchingOptions = ( options = [ ] , value ) => options . find ( ( option ) => option . name === value )
70- const getDefaultOptionValue = ( ) => ''
70+ const findMatchingOptions = ( options = [ ] , value ) => {
71+ if ( multiple ) {
72+ let values = [ ]
73+ if ( 'choose an option' !== value && value && typeof value === 'string' ) {
74+ values = JSON . parse ( value )
75+ } else {
76+ values = value
77+ }
78+ return options . filter ( ( option ) => values . includes ( option . name ) )
79+ }
80+ return options . find ( ( option ) => option . name === value )
81+ }
82+ const getDefaultOptionValue = ( ) => ( multiple ? [ ] : '' )
7183 const addNewOption = [ { label : '- Create New -' , name : '-create-' } ]
7284 let [ internalValue , setInternalValue ] = useState ( value ?? 'choose an option' )
7385
@@ -118,6 +130,8 @@ export const AsyncDropdown = ({
118130 freeSolo = { freeSolo }
119131 disabled = { disabled }
120132 disableClearable = { disableClearable }
133+ multiple = { multiple }
134+ filterSelectedOptions = { multiple }
121135 size = 'small'
122136 sx = { { mt : 1 , width : '100%' } }
123137 open = { open }
@@ -130,12 +144,22 @@ export const AsyncDropdown = ({
130144 options = { options }
131145 value = { findMatchingOptions ( options , internalValue ) || getDefaultOptionValue ( ) }
132146 onChange = { ( e , selection ) => {
133- const value = selection ? selection . name : ''
134- if ( isCreateNewOption && value === '-create-' ) {
135- onCreateNew ( )
136- } else {
147+ if ( multiple ) {
148+ let value = ''
149+ if ( selection . length ) {
150+ const selectionNames = selection . map ( ( item ) => item . name )
151+ value = JSON . stringify ( selectionNames )
152+ }
137153 setInternalValue ( value )
138154 onSelect ( value )
155+ } else {
156+ const value = selection ? selection . name : ''
157+ if ( isCreateNewOption && value === '-create-' ) {
158+ onCreateNew ( )
159+ } else {
160+ setInternalValue ( value )
161+ onSelect ( value )
162+ }
139163 }
140164 } }
141165 PopperComponent = { StyledPopper }
@@ -181,5 +205,6 @@ AsyncDropdown.propTypes = {
181205 freeSolo : PropTypes . bool ,
182206 credentialNames : PropTypes . array ,
183207 disableClearable : PropTypes . bool ,
184- isCreateNewOption : PropTypes . bool
208+ isCreateNewOption : PropTypes . bool ,
209+ multiple : PropTypes . bool
185210}
0 commit comments