Skip to content

Commit 18dd69d

Browse files
committed
refactor: common out the props
1 parent 9a81381 commit 18dd69d

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

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

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import ReactSelect, {
2222
OptionProps,
2323
SelectInstance,
2424
ValueContainerProps,
25+
MenuPlacement,
2526
} from 'react-select'
2627
import CreatableSelect from 'react-select/creatable'
2728
import { MutableRefObject, ReactElement, useCallback, useMemo } from 'react'
@@ -293,6 +294,37 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
293294
</Tippy>
294295
)
295296

297+
const commonProps = useMemo(
298+
() => ({
299+
name: name || inputId,
300+
classNamePrefix: classNamePrefix || inputId,
301+
isSearchable: isSelectSearchable,
302+
placeholder,
303+
styles: selectStyles,
304+
menuPlacement: 'auto' as MenuPlacement,
305+
menuPosition,
306+
menuShouldScrollIntoView: true,
307+
backspaceRemovesValue: isMulti,
308+
'aria-errormessage': errorElementId,
309+
'aria-invalid': !!error,
310+
'aria-labelledby': labelId,
311+
hideSelectedOptions: false,
312+
}),
313+
[
314+
name,
315+
inputId,
316+
classNamePrefix,
317+
isSelectSearchable,
318+
placeholder,
319+
selectStyles,
320+
menuPosition,
321+
errorElementId,
322+
error,
323+
labelId,
324+
isMulti,
325+
],
326+
)
327+
296328
return (
297329
<div className="flex column left top dc__gap-4">
298330
{/* Note: Common out for fields */}
@@ -319,12 +351,9 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
319351
{isMulti ? (
320352
<CreatableSelect
321353
{...props}
322-
ref={selectRef as SelectPickerProps<OptionValue, true>['selectRef']}
354+
{...commonProps}
323355
isMulti
324-
name={name || inputId}
325-
classNamePrefix={classNamePrefix || inputId}
326-
isSearchable={isSelectSearchable}
327-
placeholder={placeholder}
356+
ref={selectRef as SelectPickerProps<OptionValue, true>['selectRef']}
328357
components={{
329358
IndicatorSeparator: null,
330359
LoadingIndicator: SelectPickerLoadingIndicator,
@@ -338,29 +367,17 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
338367
MultiValueRemove: SelectPickerMultiValueRemove,
339368
GroupHeading: renderGroupHeading,
340369
}}
341-
styles={selectStyles}
342-
menuPlacement="auto"
343-
menuPosition={menuPosition}
344-
menuShouldScrollIntoView
345-
backspaceRemovesValue
346-
aria-errormessage={errorElementId}
347-
aria-invalid={!!error}
348-
aria-labelledby={labelId}
349370
closeMenuOnSelect={false}
350371
allowCreateWhileLoading={false}
351-
hideSelectedOptions={false}
352372
isValidNewOption={isValidNewOption}
353373
createOptionPosition="first"
354374
onCreateOption={onCreateOption}
355375
/>
356376
) : (
357377
<ReactSelect
358378
{...props}
379+
{...commonProps}
359380
ref={selectRef as MutableRefObject<SelectInstance<SelectPickerOptionType<OptionValue>>>}
360-
name={name || inputId}
361-
classNamePrefix={classNamePrefix || inputId}
362-
isSearchable={isSelectSearchable}
363-
placeholder={placeholder}
364381
components={{
365382
IndicatorSeparator: null,
366383
LoadingIndicator: SelectPickerLoadingIndicator,
@@ -371,15 +388,6 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
371388
ClearIndicator: SelectPickerClearIndicator,
372389
ValueContainer: renderValueContainer,
373390
}}
374-
styles={selectStyles}
375-
menuPlacement="auto"
376-
menuPosition={menuPosition}
377-
menuShouldScrollIntoView
378-
backspaceRemovesValue={false}
379-
aria-errormessage={errorElementId}
380-
aria-invalid={!!error}
381-
aria-labelledby={labelId}
382-
hideSelectedOptions={false}
383391
/>
384392
)}
385393
</div>

0 commit comments

Comments
 (0)