2022-04-26 12:20:48 -07:00
|
|
|
import React, { ChangeEvent, FC, useEffect } from 'react';
|
2022-01-12 07:20:22 -08:00
|
|
|
import { Input, InputGroup, InputGroupAddon, InputGroupText } from 'reactstrap';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faSearch } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
|
|
|
|
export interface SearchBarProps {
|
2022-04-26 12:20:48 -07:00
|
|
|
handleChange: (e: string) => void;
|
2022-01-12 07:20:22 -08:00
|
|
|
placeholder: string;
|
2022-04-26 12:20:48 -07:00
|
|
|
defaultValue: string;
|
2022-01-12 07:20:22 -08:00
|
|
|
}
|
|
|
|
|
2022-04-26 12:20:48 -07:00
|
|
|
const SearchBar: FC<SearchBarProps> = ({ handleChange, placeholder, defaultValue }) => {
|
2022-01-12 07:20:22 -08:00
|
|
|
let filterTimeout: NodeJS.Timeout;
|
|
|
|
|
|
|
|
const handleSearchChange = (e: ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => {
|
|
|
|
clearTimeout(filterTimeout);
|
|
|
|
filterTimeout = setTimeout(() => {
|
2022-04-26 12:20:48 -07:00
|
|
|
handleChange(e.target.value);
|
2022-01-12 07:20:22 -08:00
|
|
|
}, 300);
|
|
|
|
};
|
|
|
|
|
2022-04-26 12:20:48 -07:00
|
|
|
useEffect(() => {
|
|
|
|
handleChange(defaultValue);
|
|
|
|
}, [defaultValue, handleChange]);
|
|
|
|
|
2022-01-12 07:20:22 -08:00
|
|
|
return (
|
|
|
|
<InputGroup>
|
|
|
|
<InputGroupAddon addonType="prepend">
|
|
|
|
<InputGroupText>{<FontAwesomeIcon icon={faSearch} />}</InputGroupText>
|
|
|
|
</InputGroupAddon>
|
2022-04-26 12:20:48 -07:00
|
|
|
<Input autoFocus onChange={handleSearchChange} placeholder={placeholder} defaultValue={defaultValue} />
|
2022-01-12 07:20:22 -08:00
|
|
|
</InputGroup>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SearchBar;
|