|
| 1 | +import { useCallback, useMemo, useRef } from 'react' |
| 2 | +import { SelectInstance, ValueContainerProps } from 'react-select' |
| 3 | +import AsyncSelect from 'react-select/async' |
| 4 | +import { |
| 5 | + SelectPickerClearIndicator, |
| 6 | + SelectPickerControl, |
| 7 | + SelectPickerDropdownIndicator, |
| 8 | + SelectPickerInput, |
| 9 | + SelectPickerLoadingIndicator, |
| 10 | + SelectPickerMenuList, |
| 11 | + SelectPickerMultiValueRemove, |
| 12 | + SelectPickerOption, |
| 13 | + SelectPickerValueContainer, |
| 14 | +} from './common' |
| 15 | +import { AsyncSelectProps, SelectPickerOptionType } from './type' |
| 16 | +import { FormFieldWrapper } from '../FormFieldWrapper' |
| 17 | +import { getCommonSelectStyle } from './utils' |
| 18 | + |
| 19 | +export const AsyncSelectPicker = ({ |
| 20 | + defaultOptions, |
| 21 | + loadOptions, |
| 22 | + noOptionsMessage, |
| 23 | + onChange, |
| 24 | + error, |
| 25 | + size, |
| 26 | + menuSize, |
| 27 | + variant, |
| 28 | + getIsOptionValid, |
| 29 | + isGroupHeadingSelectable, |
| 30 | + shouldMenuAlignRight, |
| 31 | + inputId, |
| 32 | + layout, |
| 33 | + label, |
| 34 | + helperText, |
| 35 | + warningText, |
| 36 | + required, |
| 37 | + fullWidth, |
| 38 | + ariaLabel, |
| 39 | + borderConfig, |
| 40 | + borderRadiusConfig, |
| 41 | + labelTippyCustomizedConfig, |
| 42 | + labelTooltipConfig, |
| 43 | +}: AsyncSelectProps) => { |
| 44 | + const selectRef = useRef<SelectInstance>(null) |
| 45 | + |
| 46 | + // Option disabled, group null state, checkbox hover, create option visibility (scroll reset on search) |
| 47 | + const selectStyles = useMemo( |
| 48 | + () => |
| 49 | + getCommonSelectStyle({ |
| 50 | + error, |
| 51 | + size, |
| 52 | + menuSize, |
| 53 | + variant, |
| 54 | + getIsOptionValid, |
| 55 | + isGroupHeadingSelectable, |
| 56 | + shouldMenuAlignRight, |
| 57 | + }), |
| 58 | + [error, size, menuSize, variant, isGroupHeadingSelectable, shouldMenuAlignRight], |
| 59 | + ) |
| 60 | + |
| 61 | + const handleOnKeyDown = (event) => { |
| 62 | + if (event.key === 'Escape') { |
| 63 | + selectRef.current?.inputRef.blur() |
| 64 | + } |
| 65 | + } |
| 66 | + type OptionValue = string | number |
| 67 | + |
| 68 | + const renderValueContainer = useCallback( |
| 69 | + (valueContainerProps: ValueContainerProps<SelectPickerOptionType<OptionValue>>) => ( |
| 70 | + <SelectPickerValueContainer {...valueContainerProps} /> |
| 71 | + ), |
| 72 | + [], |
| 73 | + ) |
| 74 | + |
| 75 | + return ( |
| 76 | + <FormFieldWrapper |
| 77 | + inputId={inputId} |
| 78 | + layout={layout} |
| 79 | + label={label} |
| 80 | + error={error} |
| 81 | + helperText={helperText} |
| 82 | + warningText={warningText} |
| 83 | + required={required} |
| 84 | + fullWidth={fullWidth} |
| 85 | + ariaLabel={ariaLabel} |
| 86 | + borderConfig={borderConfig} |
| 87 | + borderRadiusConfig={borderRadiusConfig} |
| 88 | + labelTippyCustomizedConfig={labelTippyCustomizedConfig} |
| 89 | + labelTooltipConfig={labelTooltipConfig} |
| 90 | + > |
| 91 | + <div className="w-100"> |
| 92 | + <AsyncSelect |
| 93 | + ref={selectRef} |
| 94 | + blurInputOnSelect |
| 95 | + onKeyDown={handleOnKeyDown} |
| 96 | + defaultOptions={defaultOptions} |
| 97 | + loadOptions={loadOptions} |
| 98 | + noOptionsMessage={noOptionsMessage} |
| 99 | + onChange={onChange} |
| 100 | + components={{ |
| 101 | + IndicatorSeparator: null, |
| 102 | + LoadingIndicator: SelectPickerLoadingIndicator, |
| 103 | + DropdownIndicator: SelectPickerDropdownIndicator, |
| 104 | + Control: SelectPickerControl, |
| 105 | + Option: SelectPickerOption, |
| 106 | + MenuList: SelectPickerMenuList, |
| 107 | + ClearIndicator: SelectPickerClearIndicator, |
| 108 | + ValueContainer: renderValueContainer, |
| 109 | + // MultiValueLabel: renderMultiValueLabel, |
| 110 | + MultiValueRemove: SelectPickerMultiValueRemove, |
| 111 | + // GroupHeading: renderGroupHeading, |
| 112 | + // NoOptionsMessage: renderNoOptionsMessage, |
| 113 | + Input: SelectPickerInput, |
| 114 | + }} |
| 115 | + value={defaultOptions?.[0]} |
| 116 | + styles={selectStyles} |
| 117 | + // aria-describedby={getFormHelperTextElementId('app-selector')} |
| 118 | + /> |
| 119 | + </div> |
| 120 | + </FormFieldWrapper> |
| 121 | + ) |
| 122 | +} |
0 commit comments