import { UseComboboxReturnValue } from 'downshift'; import React, { useRef, ChangeEvent } from 'react'; import { InputContainer, SelectedItemText } from './Styled'; import { BaseOption } from '../types'; import { SelectProps } from '.'; import { getThemeState } from '../../Input/utils'; import Affix from '../../Input/Affix'; import StyledInput, { InputWrapper } from '../../Input/StyledInput'; import SuffixIcon from '../SuffixIcon'; import { useHover } from '../../../utils/hooks'; type QueryInputComponentProps = { selectedItem: T | undefined; } & Pick, 'getInputProps' | 'isOpen'> & Pick< SelectProps, | 'selectedOptionRenderer' | 'id' | 'autoComplete' | 'clearable' | 'query' | 'onQueryChange' | 'value' | 'onChange' | 'disabled' | 'prefix' | 'loading' | 'invalid' | 'placeholder' | 'size' | 'onFocus' | 'onBlur' >; const getOptionText = ({ option }: { option: T }) => option.text; const QueryInput = ({ selectedItem, getInputProps, isOpen, selectedOptionRenderer = getOptionText, id, autoComplete, clearable, value, onChange, query, onQueryChange, disabled, prefix, loading, invalid, placeholder, size = 'medium', onFocus, onBlur, }: QueryInputComponentProps) => { const inputWrapperRef = useRef(null); const isHoveringInput = useHover(inputWrapperRef); const readonly = onQueryChange === undefined; const removeShown = clearable === true && value !== undefined && isHoveringInput; const selectedItemText = selectedItem !== undefined ? selectedOptionRenderer({ option: selectedItem }) : ''; const isEditing = query !== undefined && query.length > 0; const inputProps = getInputProps({ onChange: (e: ChangeEvent): void => { onQueryChange?.(e.target.value); }, onBlur, onFocus, ref: undefined, }); return ( {isEditing ? '' : selectedItemText} } themeSize={size} themeMarginLeft="auto" /> ); }; export default QueryInput;