import cx from "classnames"; import React, { forwardRef, useCallback, useRef } from "react"; import useMergeRef from "../../hooks/useMergeRef"; import CloseSmallIcon from "../Icon/Icons/components/CloseSmall"; import SearchIcon from "../Icon/Icons/components/Search"; import { ComponentDefaultTestId, getTestId } from "../../tests/test-ids-utils"; import styles from "./Search.module.scss"; import BaseInput from "../BaseInput/BaseInput"; import useDebounceEvent from "../../hooks/useDebounceEvent"; import IconButton from "../IconButton/IconButton"; import Icon from "../Icon/Icon"; import { SearchProps } from "./Search.types"; import Loader from "../Loader/Loader"; const Search = forwardRef( ( { searchIconName = SearchIcon, clearIconName = CloseSmallIcon, clearIconLabel = "Clear", renderAction: RenderAction, hideRenderActionOnInput, value, placeholder, size = "medium", disabled, loading, autoFocus, autoComplete = "off", inputAriaLabel, debounceRate = 400, searchResultsContainerId, currentAriaResultId, onChange, onFocus, onBlur, onClear, onKeyDown, className, ariaExpanded, ariaHasPopup, id, "data-testid": dataTestId }: SearchProps, ref: React.ForwardedRef ) => { const inputRef = useRef(null); const mergedRef = useMergeRef(ref, inputRef); const { inputValue, onEventChanged, clearValue } = useDebounceEvent({ delay: debounceRate, onChange, initialStateValue: value }); const onClearButtonClick = useCallback(() => { if (disabled) return; inputRef.current?.focus?.(); clearValue(); onClear?.(); }, [disabled, clearValue, onClear]); const SearchIcon = ( ); const ClearIcon = ( ); const RenderRight = ( <> {loading && ( )} {inputValue && !disabled && ClearIcon} {!(hideRenderActionOnInput && inputValue) && RenderAction} ); return ( ); } ); export default Search;