fleet/frontend/components/forms/fields/SearchField/SearchField.tsx
Ian Littman 2891904f31
🤖 Switch InputField + InputFieldWithIcon JSX components to TS, add more test coverage, fix Storybook build (#43307)
Zed + Opus 4.6; prompt: Convert the InputField JSX component to
TypeScript and remove the ts-ignore directives that we no longer need
after doing so.

- [x] Changes file added
- [x] Automated tests updated
2026-04-09 08:41:48 -05:00

70 lines
1.7 KiB
TypeScript

import React, { useState } from "react";
import { useDebouncedCallback } from "use-debounce";
import { IconNames } from "components/icons";
import TooltipWrapper from "components/TooltipWrapper";
// @ts-ignore
import InputFieldWithIcon from "../InputFieldWithIcon";
const baseClass = "search-field";
export interface ISearchFieldProps {
placeholder: string;
defaultValue?: string;
onChange: (value: string) => void;
onClick?: (e: React.MouseEvent) => void;
clearButton?: boolean;
icon?: IconNames;
tooltip?: React.ReactNode;
disabled?: boolean;
}
const SearchField = ({
placeholder,
defaultValue = "",
onChange,
clearButton,
onClick,
icon = "search",
tooltip,
disabled,
}: ISearchFieldProps): JSX.Element => {
const [searchQueryInput, setSearchQueryInput] = useState(defaultValue);
const debouncedOnChange = useDebouncedCallback((newValue: string) => {
onChange(newValue);
}, 500);
const onInputChange = (newValue: string): void => {
setSearchQueryInput(newValue);
debouncedOnChange(newValue);
};
return (
<>
<TooltipWrapper
disableTooltip={!tooltip}
tipContent={tooltip}
position="top"
showArrow
underline={false}
tooltipClass={`${baseClass}__tooltip-text`}
className={`${baseClass}__tooltip-container`}
>
<InputFieldWithIcon
name={icon}
placeholder={placeholder}
value={searchQueryInput}
onChange={onInputChange}
onClick={onClick}
clearButton={clearButton}
iconSvg={icon}
disabled={disabled}
/>
</TooltipWrapper>
</>
);
};
export default SearchField;