import React from "react"; import { TestableComponent } from "../../components/interfaces"; import { CLASSPREFIX as eccgui } from "../../configuration/constants"; import Icon from "../Icon/Icon"; import IconButton from "../Icon/IconButton"; import TextField, { TextFieldProps } from "./TextField"; export interface SearchFieldProps extends TestableComponent, Omit { /** * Placeholder text for search box. */ emptySearchInputMessage?: string; /** * Event handler to reset search input. * If set then `rightElement` is automatically set with an action button to trigger the handler. */ onClearanceHandler?: () => void; /** * Tooltip to show for the clear button. */ onClearanceText?: string; } /** * Special `TextField` element for search term inputs. */ export const SearchField = ({ className = "", emptySearchInputMessage = "Enter search term", onClearanceHandler, onClearanceText = "Clear current search term", onChange, leftIcon = , rightElement, ...otherProps }: SearchFieldProps) => { const [value, setValue] = React.useState(""); const clearanceButton = onClearanceHandler && value ? ( { setValue(""); onClearanceHandler(); }} /> ) : undefined; const changeHandlerProcess = (e: React.ChangeEvent) => { setValue(e.target.value); if (onChange) { onChange(e); } }; React.useEffect(() => { setValue(otherProps.value ?? otherProps.defaultValue ?? ""); }, [otherProps.value, otherProps.defaultValue]); return ( {rightElement} {clearanceButton} ) } onChange={changeHandlerProcess} {...otherProps} value={value} type={"search"} leftIcon={leftIcon} round={true} /> ); }; export default SearchField;