@@ -13,6 +13,7 @@ import { Option } from "components/ui/ComboBox";
13
13
import { FlexContainer } from "components/ui/Flex" ;
14
14
import { IconProps } from "components/ui/Icon" ;
15
15
import { Input } from "components/ui/Input" ;
16
+ import { RadioButton } from "components/ui/RadioButton" ;
16
17
import { Text } from "components/ui/Text" ;
17
18
import { Tooltip } from "components/ui/Tooltip" ;
18
19
@@ -174,69 +175,67 @@ ControlButton.displayName = "ControlButton";
174
175
interface OptionsProps {
175
176
options : Array < Option & { disabled ?: boolean ; disabledReason ?: React . ReactNode } > ;
176
177
filterQuery : string ;
178
+ multiple ?: boolean ;
177
179
}
178
- const Options = React . forwardRef ( ( { options, filterQuery } : OptionsProps , ref : React . Ref < HTMLUListElement > ) => {
179
- const { formatMessage } = useIntl ( ) ;
180
- const filteredOptions =
181
- filterQuery === ""
182
- ? options
183
- : options . filter ( ( option ) => option . value . toLowerCase ( ) . includes ( filterQuery . toLowerCase ( ) ) ) ;
180
+ const Options = React . forwardRef (
181
+ ( { options, filterQuery, multiple } : OptionsProps , ref : React . Ref < HTMLUListElement > ) => {
182
+ const { formatMessage } = useIntl ( ) ;
183
+ const filteredOptions =
184
+ filterQuery === ""
185
+ ? options
186
+ : options . filter ( ( option ) => option . value . toLowerCase ( ) . includes ( filterQuery . toLowerCase ( ) ) ) ;
184
187
185
- return (
186
- < ComboboxOptions ref = { ref } className = { styles . optionsMenu } >
187
- { filteredOptions . length === 0 ? (
188
- < FlexContainer className = { styles . optionValue } alignItems = "center" >
189
- < Text size = "md" > { formatMessage ( { id : "ui.combobox.noOptions" } ) } </ Text >
190
- </ FlexContainer >
191
- ) : (
192
- filteredOptions . map ( ( { disabled, disabledReason, value } ) => (
193
- < ComboboxOption disabled = { disabled } className = { styles . option } key = { value } value = { value } >
194
- { ( { focus, selected } ) => {
195
- const control = (
196
- < FlexContainer
197
- className = { classnames ( styles . optionValue , {
198
- [ styles . focus ] : focus ,
199
- [ styles . selected ] : selected ,
200
- } ) }
201
- alignItems = "center"
202
- >
203
- < CheckBox checkboxSize = "sm" checked = { selected } onClick = { ( e ) => e . preventDefault ( ) } readOnly />
204
- < Text size = "md" color = { disabled ? "grey" : "darkBlue" } >
205
- < TextHighlighter searchWords = { [ filterQuery ] } textToHighlight = { value } />
206
- </ Text >
207
- </ FlexContainer >
208
- ) ;
209
- return (
210
- < Tooltip
211
- placement = "top"
212
- disabled = { ! ( disabled && disabledReason ) }
213
- containerClassName = { styles . block }
214
- control = { control }
215
- >
216
- { disabledReason }
217
- </ Tooltip >
218
- ) ;
219
- } }
220
- </ ComboboxOption >
221
- ) )
222
- ) }
223
- </ ComboboxOptions >
224
- ) ;
225
- } ) ;
188
+ return (
189
+ < ComboboxOptions ref = { ref } className = { styles . optionsMenu } >
190
+ { filteredOptions . length === 0 ? (
191
+ < FlexContainer className = { styles . optionValue } alignItems = "center" >
192
+ < Text size = "md" > { formatMessage ( { id : "ui.combobox.noOptions" } ) } </ Text >
193
+ </ FlexContainer >
194
+ ) : (
195
+ filteredOptions . map ( ( { disabled, disabledReason, value } ) => (
196
+ < ComboboxOption disabled = { disabled } className = { styles . option } key = { value } value = { value } >
197
+ { ( { focus, selected } ) => {
198
+ const control = (
199
+ < FlexContainer
200
+ className = { classnames ( styles . optionValue , {
201
+ [ styles . focus ] : focus ,
202
+ [ styles . selected ] : selected ,
203
+ } ) }
204
+ alignItems = "center"
205
+ >
206
+ { multiple ? (
207
+ < CheckBox checkboxSize = "sm" checked = { selected } onClick = { ( e ) => e . preventDefault ( ) } readOnly />
208
+ ) : (
209
+ < RadioButton checked = { selected } onClick = { ( e ) => e . preventDefault ( ) } readOnly />
210
+ ) }
211
+ < Text size = "md" color = { disabled ? "grey" : "darkBlue" } >
212
+ < TextHighlighter searchWords = { [ filterQuery ] } textToHighlight = { value } />
213
+ </ Text >
214
+ </ FlexContainer >
215
+ ) ;
216
+ return (
217
+ < Tooltip
218
+ placement = "top"
219
+ disabled = { ! ( disabled && disabledReason ) }
220
+ containerClassName = { styles . block }
221
+ control = { control }
222
+ >
223
+ { disabledReason }
224
+ </ Tooltip >
225
+ ) ;
226
+ } }
227
+ </ ComboboxOption >
228
+ ) )
229
+ ) }
230
+ </ ComboboxOptions >
231
+ ) ;
232
+ }
233
+ ) ;
226
234
Options . displayName = "Options" ;
227
235
228
236
export const CatalogComboBox : React . FC < CatalogComboBoxProps > = ( { value, options, onChange, ...restControlProps } ) => {
229
237
const [ filterQuery , setFilterQuery ] = useState ( "" ) ;
230
238
231
- const onChangeHandler = ( newValue : string ) => {
232
- // add ability to unselect the option
233
- if ( value !== newValue ) {
234
- onChange ( newValue ) ;
235
- } else {
236
- onChange ( "" ) ;
237
- }
238
- } ;
239
-
240
239
const onCloseOptionsMenu = ( ) => {
241
240
// reset filter value after closing the options menu
242
241
if ( filterQuery . length ) {
@@ -247,7 +246,7 @@ export const CatalogComboBox: React.FC<CatalogComboBoxProps> = ({ value, options
247
246
return (
248
247
< Combobox
249
248
value = { value }
250
- onChange = { onChangeHandler }
249
+ onChange = { onChange }
251
250
disabled = { restControlProps . disabled }
252
251
onClose = { onCloseOptionsMenu }
253
252
immediate
@@ -303,7 +302,7 @@ export const MultiCatalogComboBox: React.FC<MultiCatalogComboBoxProps> = ({
303
302
{ ( { open } ) => (
304
303
< FloatLayout >
305
304
< ControlButton open = { open } value = { selectedOptions } setFilterQuery = { setFilterQuery } { ...restControlProps } />
306
- < Options options = { options } filterQuery = { filterQuery } />
305
+ < Options options = { options } filterQuery = { filterQuery } multiple />
307
306
</ FloatLayout >
308
307
) }
309
308
</ Combobox >
0 commit comments