Skip to content

Commit 33f0043

Browse files
committed
code refactoring
1 parent bc2de0b commit 33f0043

File tree

1 file changed

+33
-82
lines changed

1 file changed

+33
-82
lines changed

src/Shared/Components/SelectPicker/SelectPicker.component.tsx

Lines changed: 33 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
import ReactSelect, {
17+
import {
1818
ControlProps,
1919
GroupHeadingProps,
2020
MultiValueProps,
2121
OptionProps,
22-
SelectInstance,
2322
ValueContainerProps,
2423
MenuPlacement,
2524
} from 'react-select'
2625
import CreatableSelect from 'react-select/creatable'
27-
import { MutableRefObject, ReactElement, useCallback, useMemo } from 'react'
26+
import { ReactElement, useCallback, useMemo } from 'react'
2827

2928
import { ReactComponent as ErrorIcon } from '@Icons/ic-warning.svg'
3029
import { ReactComponent as ICInfoFilledOverride } from '@Icons/ic-info-filled-override.svg'
@@ -209,11 +208,11 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
209208
fullWidth = false,
210209
customSelectedOptionsCount = null,
211210
renderMenuListFooter,
212-
isCreatableSingleSelect = false,
213211
inputValue,
214212
onInputChange,
215213
isCreatable = false,
216214
onCreateOption,
215+
closeMenuOnSelect = false,
217216
...props
218217
}: SelectPickerProps<OptionValue, IsMulti>) => {
219218
const { inputId, required, isDisabled, controlShouldRenderValue = true, value, options } = props
@@ -309,9 +308,9 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
309308
)
310309

311310
const handleCreateOption: SelectPickerProps<OptionValue, boolean>['onCreateOption'] = (
312-
inputValue: string,
311+
_inputValue: string,
313312
): void => {
314-
const trimmedInputValue = inputValue?.trim()
313+
const trimmedInputValue = _inputValue?.trim()
315314
if (trimmedInputValue) {
316315
onCreateOption(trimmedInputValue)
317316
}
@@ -350,50 +349,6 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
350349
],
351350
)
352351

353-
const renderSingleSelectPicker = () =>
354-
isCreatableSingleSelect ? (
355-
<CreatableSelect
356-
{...props}
357-
{...commonProps}
358-
ref={selectRef as SelectPickerProps<OptionValue, false>['selectRef']}
359-
components={{
360-
IndicatorSeparator: null,
361-
LoadingIndicator: SelectPickerLoadingIndicator,
362-
DropdownIndicator: SelectPickerDropdownIndicator,
363-
Control: renderControl,
364-
Option: renderOption,
365-
MenuList: SelectPickerMenuList,
366-
ClearIndicator: SelectPickerClearIndicator,
367-
ValueContainer: renderValueContainer,
368-
GroupHeading: renderGroupHeading,
369-
NoOptionsMessage: renderNoOptionsMessage,
370-
}}
371-
allowCreateWhileLoading={false}
372-
isValidNewOption={isValidNewOption}
373-
createOptionPosition="first"
374-
onCreateOption={handleCreateOption}
375-
renderMenuListFooter={!optionListError && renderMenuListFooter}
376-
/>
377-
) : (
378-
<ReactSelect
379-
{...props}
380-
{...commonProps}
381-
ref={selectRef as MutableRefObject<SelectInstance<SelectPickerOptionType<OptionValue>>>}
382-
components={{
383-
IndicatorSeparator: null,
384-
LoadingIndicator: SelectPickerLoadingIndicator,
385-
DropdownIndicator: SelectPickerDropdownIndicator,
386-
Control: renderControl,
387-
Option: renderOption,
388-
MenuList: SelectPickerMenuList,
389-
ClearIndicator: SelectPickerClearIndicator,
390-
ValueContainer: renderValueContainer,
391-
NoOptionsMessage: renderNoOptionsMessage,
392-
}}
393-
renderMenuListFooter={!optionListError && renderMenuListFooter}
394-
/>
395-
)
396-
397352
return (
398353
<div className={`flex column left top dc__gap-4 ${fullWidth ? 'w-100' : ''}`}>
399354
{/* Note: Common out for fields */}
@@ -417,38 +372,34 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
417372
)}
418373
<ConditionalWrap condition={isDisabled && !!disabledTippyContent} wrap={renderDisabledTippy}>
419374
<div className="w-100">
420-
{isMulti ? (
421-
<CreatableSelect
422-
{...props}
423-
{...commonProps}
424-
isMulti
425-
ref={selectRef as SelectPickerProps<OptionValue, true>['selectRef']}
426-
components={{
427-
IndicatorSeparator: null,
428-
LoadingIndicator: SelectPickerLoadingIndicator,
429-
DropdownIndicator: SelectPickerDropdownIndicator,
430-
Control: renderControl,
431-
Option: renderOption,
432-
MenuList: SelectPickerMenuList,
433-
ClearIndicator: SelectPickerClearIndicator,
434-
ValueContainer: renderValueContainer,
435-
MultiValueLabel: renderMultiValueLabel,
436-
MultiValueRemove: SelectPickerMultiValueRemove,
437-
GroupHeading: renderGroupHeading,
438-
NoOptionsMessage: renderNoOptionsMessage,
439-
}}
440-
closeMenuOnSelect={false}
441-
allowCreateWhileLoading={false}
442-
isValidNewOption={isValidNewOption}
443-
createOptionPosition="first"
444-
onCreateOption={handleCreateOption}
445-
renderMenuListFooter={!optionListError && renderMenuListFooter}
446-
inputValue={inputValue}
447-
onInputChange={onInputChange}
448-
/>
449-
) : (
450-
renderSingleSelectPicker()
451-
)}
375+
<CreatableSelect
376+
{...props}
377+
{...commonProps}
378+
isMulti={isMulti}
379+
ref={selectRef}
380+
components={{
381+
IndicatorSeparator: null,
382+
LoadingIndicator: SelectPickerLoadingIndicator,
383+
DropdownIndicator: SelectPickerDropdownIndicator,
384+
Control: renderControl,
385+
Option: renderOption,
386+
MenuList: SelectPickerMenuList,
387+
ClearIndicator: SelectPickerClearIndicator,
388+
ValueContainer: renderValueContainer,
389+
MultiValueLabel: renderMultiValueLabel,
390+
MultiValueRemove: SelectPickerMultiValueRemove,
391+
GroupHeading: renderGroupHeading,
392+
NoOptionsMessage: renderNoOptionsMessage,
393+
}}
394+
closeMenuOnSelect={closeMenuOnSelect}
395+
allowCreateWhileLoading={false}
396+
isValidNewOption={isValidNewOption}
397+
createOptionPosition="first"
398+
onCreateOption={handleCreateOption}
399+
renderMenuListFooter={!optionListError && renderMenuListFooter}
400+
inputValue={inputValue}
401+
onInputChange={onInputChange}
402+
/>
452403
</div>
453404
</ConditionalWrap>
454405
</div>

0 commit comments

Comments
 (0)