@@ -5,7 +5,7 @@ import { useFieldApi } from '@data-driven-forms/react-form-renderer';
55import DataDrivenSelect from '@data-driven-forms/common/select' ;
66import fnToString from '@data-driven-forms/common/utils/fn-to-string' ;
77
8- import { Select as CarbonSelect , MultiSelect , SelectItem , ComboBox } from 'carbon-components-react' ;
8+ import { Select as CarbonSelect , MultiSelect , SelectItem , ComboBox , SelectItemGroup } from 'carbon-components-react' ;
99import prepareProps from '../prepare-props' ;
1010
1111const onChangeWrapper = ( onChange ) => ( { selectedItem, selectedItems } ) => onChange ( selectedItems || selectedItem ) ;
@@ -15,6 +15,21 @@ export const getMultiValue = (value, options) =>
1515 typeof item === 'object' ? item : options . find ( ( { value } ) => value === item )
1616 ) ;
1717
18+ const renderOptions = ( options ) =>
19+ options . map ( ( option , index ) => {
20+ const { options, ...rest } = option ;
21+
22+ if ( options ) {
23+ return (
24+ < SelectItemGroup key = { rest . value || index } text = { rest . label } { ...rest } >
25+ { renderOptions ( options ) }
26+ </ SelectItemGroup >
27+ ) ;
28+ }
29+
30+ return < SelectItem key = { rest . value || index } text = { rest . label } { ...rest } /> ;
31+ } ) ;
32+
1833const ClearedMultiSelectFilterable = ( {
1934 invalidText,
2035 hideSelectedOptions,
@@ -147,9 +162,7 @@ const ClearedSelect = ({
147162 invalidText = { invalidText }
148163 >
149164 { isFetching && < SelectItem text = { placeholder } value = { '' } /> }
150- { options . map ( ( option , index ) => (
151- < SelectItem key = { option . value || index } text = { option . label } { ...option } />
152- ) ) }
165+ { renderOptions ( options ) }
153166 </ CarbonSelect >
154167) ;
155168
@@ -252,8 +265,15 @@ const Select = (props) => {
252265 } , [ loadOptionsStr ] ) ;
253266 const isSearchClear = isSearchable || isClearable ;
254267
255- const Component =
256- isMulti && isSearchClear ? ClearedMultiSelectFilterable : isMulti ? ClearedMultiSelect : isSearchClear ? ClearedSelectSearchable : ClearedSelect ;
268+ let Component = ClearedSelect ;
269+
270+ if ( isMulti && isSearchClear ) {
271+ Component = ClearedMultiSelectFilterable ;
272+ } else if ( isMulti ) {
273+ Component = ClearedMultiSelect ;
274+ } else if ( isSearchClear ) {
275+ Component = ClearedSelectSearchable ;
276+ }
257277
258278 const invalidText = ( ( meta . touched || validateOnMount ) && ( meta . error || meta . submitError ) ) || '' ;
259279 const text = ( ( meta . touched || validateOnMount ) && meta . warning ) || helperText ;
0 commit comments