fleet/frontend/components/forms/fields/SearchField/SearchField.tsx

47 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-12-29 18:10:54 +00:00
import React, { useState } from "react";
import { useDebouncedCallback } from "use-debounce";
import { IconNames } from "components/icons";
2021-12-29 18:10:54 +00:00
// @ts-ignore
import InputFieldWithIcon from "../InputFieldWithIcon";
2021-12-29 18:10:54 +00:00
export interface ISearchFieldProps {
placeholder: string;
defaultValue?: string;
2021-12-29 18:10:54 +00:00
onChange: (value: string) => void;
onClick?: (e: React.MouseEvent) => void;
icon?: IconNames;
2021-12-29 18:10:54 +00:00
}
const SearchField = ({
placeholder,
defaultValue = "",
2021-12-29 18:10:54 +00:00
onChange,
onClick,
icon = "search",
2021-12-29 18:10:54 +00:00
}: ISearchFieldProps): JSX.Element => {
const [searchQueryInput, setSearchQueryInput] = useState(defaultValue);
2021-12-29 18:10:54 +00:00
const debouncedOnChange = useDebouncedCallback((newValue: string) => {
onChange(newValue);
}, 500);
const onInputChange = (newValue: string): void => {
setSearchQueryInput(newValue);
debouncedOnChange(newValue);
};
return (
<InputFieldWithIcon
name={icon}
2021-12-29 18:10:54 +00:00
placeholder={placeholder}
value={searchQueryInput}
onChange={onInputChange}
onClick={onClick}
iconPosition="start"
iconSvg={icon}
2021-12-29 18:10:54 +00:00
/>
);
};
export default SearchField;