import styled from 'styled-components'; import React, { useRef } from 'react'; import type { SelectOption } from '@redocly/theme/core/types'; import { Tag } from '@redocly/theme/components/Tag/Tag'; import { CloseIcon } from '@redocly/theme/icons/CloseIcon/CloseIcon'; import { Button } from '@redocly/theme/components/Button/Button'; type SelectInputProps = { id?: string; listboxId?: string; selectedOptions: SelectOption[]; /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ searchValue: any; /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ stickyValue: any; onlyIcon?: boolean; icon?: React.ReactNode; customIcon?: React.ReactNode; placeholder?: string; multiple?: boolean; searchable?: boolean; clearable?: boolean; inputRef?: React.ForwardedRef; /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ clearHandler?: (value?: any) => void; /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ inputBlurHandler?: (e?: any) => void; /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ inputFocusHandler?: (e?: any) => void; /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ searchHandler?: (e?: any) => void; /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ clickHandler?: (e?: any) => void; }; export function SelectInput(props: SelectInputProps): React.ReactNode { const { id, listboxId, onlyIcon, icon, customIcon, selectedOptions, placeholder, stickyValue, multiple, searchable, clearable, clearHandler, searchHandler, clickHandler, searchValue, inputBlurHandler, inputFocusHandler, } = props; const inputRef = useRef(null); /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ const onChangeHandler = (e: any) => { searchHandler?.(e); inputRef.current?.focus(); }; /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ const onKeyDownHandler = (e: any) => { if (e.key === 'Backspace' && !searchValue && selectedOptions.length) { clearHandler?.(selectedOptions[selectedOptions.length - 1]); inputRef.current?.focus(); } }; const onClickHandler = (e: React.MouseEvent) => { clickHandler?.(e); }; const onFocusHandler = (e: React.FocusEvent) => { inputFocusHandler?.(e); inputRef.current?.focus(); }; const onBlurHandler = (e: React.FocusEvent) => { inputBlurHandler?.(e); }; const onClearAllHandler = (e: React.MouseEvent) => { e.stopPropagation(); clearHandler?.(); }; const selectTags = selectedOptions.map((option, index) => ( { clearHandler?.(option); inputRef.current?.focus(); }} > {option.label || (option.value as string) || option.element} )); const selectInput = ( { if (!input) return; inputRef.current = input; if (!props.inputRef) return; if (typeof props.inputRef === 'function') { props.inputRef(input); } else { props.inputRef.current = input; } }} width={multiple ? (!searchValue && selectedOptions.length ? '10px' : 'auto') : '100%'} tabIndex={0} onFocus={(e) => { e.stopPropagation(); }} onMouseDown={(e) => { e.preventDefault(); }} /> ); const simpleValue = selectedOptions.length ? ( selectedOptions[0].label || selectedOptions[0].element || (selectedOptions[0].value as string) ) : ( {placeholder} ); const multipleValues = selectedOptions.length ? ( selectTags ) : ( {placeholder} ); return ( {!onlyIcon && ( <> {multiple ? ( searchable ? ( <> {selectTags} {selectInput} ) : ( multipleValues ) ) : searchable ? ( selectInput ) : ( simpleValue )} {!!(clearable && selectedOptions.length) && (