|
| 1 | +import { FC, FocusEvent } from 'react' |
| 2 | +import { MultiValue, StylesConfig } from 'react-select' |
| 3 | +// tslint:disable-next-line: no-submodule-imports |
| 4 | +import AsyncSelect from 'react-select/async' |
| 5 | + |
| 6 | +import { InputWrapper } from '../form/form-groups/form-input/input-wrapper' |
| 7 | + |
| 8 | +import { membersAutocompete, MembersAutocompeteResult } from './input-handle-functions' |
| 9 | +import styles from './InputHandleAutocomplete.module.scss' |
| 10 | + |
| 11 | +export interface InputHandleAutocompleteProps { |
| 12 | + readonly className?: string |
| 13 | + readonly dirty?: boolean |
| 14 | + readonly disabled?: boolean |
| 15 | + readonly error?: string |
| 16 | + readonly hideInlineErrors?: boolean |
| 17 | + readonly hint?: string |
| 18 | + readonly label?: string | JSX.Element |
| 19 | + readonly name: string |
| 20 | + readonly onBlur?: (event: FocusEvent<HTMLInputElement>) => void |
| 21 | + readonly onChange: (newValue: Array<MembersAutocompeteResult>) => void |
| 22 | + readonly placeholder?: string |
| 23 | + readonly tabIndex: number |
| 24 | + readonly value?: Array<MembersAutocompeteResult> |
| 25 | +} |
| 26 | + |
| 27 | +const InputHandleAutocomplete: FC<InputHandleAutocompleteProps> = (props: InputHandleAutocompleteProps) => { |
| 28 | + const customStyles: StylesConfig<any> = { |
| 29 | + control: (provided) => ({ |
| 30 | + ...provided, |
| 31 | + border: 'none', |
| 32 | + }), |
| 33 | + input: (provided) => ({ |
| 34 | + ...provided, |
| 35 | + color: 'inherit', |
| 36 | + fontSize: 16, |
| 37 | + }), |
| 38 | + multiValue: (provided) => ({ |
| 39 | + ...provided, |
| 40 | + borderRadius: 50, |
| 41 | + }), |
| 42 | + multiValueLabel: (provided) => ({ |
| 43 | + ...provided, |
| 44 | + fontSize: 12, |
| 45 | + }), |
| 46 | + option: (provided) => ({ |
| 47 | + ...provided, |
| 48 | + borderBottom: '1px solid #E9E9E9', |
| 49 | + color: 'inherit', |
| 50 | + fontSize: 16, |
| 51 | + fontWeight: 400, |
| 52 | + padding: 16, |
| 53 | + }), |
| 54 | + placeholder: (provided) => ({ |
| 55 | + ...provided, |
| 56 | + color: 'inherit', |
| 57 | + fontSize: 16, |
| 58 | + fontWeight: 400, |
| 59 | + }), |
| 60 | + valueContainer: (provided) => ({ |
| 61 | + ...provided, |
| 62 | + padding: 0, |
| 63 | + }), |
| 64 | + } |
| 65 | + |
| 66 | + return ( |
| 67 | + <InputWrapper |
| 68 | + {...props} |
| 69 | + dirty={!!props.dirty} |
| 70 | + disabled={!!props.disabled} |
| 71 | + label={props.label || props.name} |
| 72 | + hideInlineErrors={props.hideInlineErrors} |
| 73 | + type='text' |
| 74 | + > |
| 75 | + <AsyncSelect |
| 76 | + className={styles.memberSelect} |
| 77 | + cacheOptions |
| 78 | + getOptionLabel={({ handle }) => handle} |
| 79 | + getOptionValue={({ userId }) => userId} |
| 80 | + isMulti |
| 81 | + key={props.value?.length} |
| 82 | + loadOptions={membersAutocompete} |
| 83 | + styles={customStyles} |
| 84 | + placeholder={props.placeholder} |
| 85 | + onBlur={props.onBlur} |
| 86 | + onChange={(newValue: MultiValue<MembersAutocompeteResult>) => props.onChange(newValue as Array<MembersAutocompeteResult>)} |
| 87 | + value={props.value} |
| 88 | + isDisabled={props.disabled} |
| 89 | + /> |
| 90 | + </InputWrapper> |
| 91 | + ) |
| 92 | +} |
| 93 | + |
| 94 | +export default InputHandleAutocomplete |
0 commit comments