2025-05-17 00:27:49 +02:00
|
|
|
// Preact
|
2025-05-16 13:12:03 +02:00
|
|
|
import { h } from 'preact';
|
2025-05-17 00:27:49 +02:00
|
|
|
import { useState, useRef, useEffect } from 'preact/hooks';
|
|
|
|
|
|
|
|
// Images
|
|
|
|
import SearchIcon from '../../assets/search.svg'
|
|
|
|
|
|
|
|
// Styles
|
|
|
|
import styles from './search.module.css'; // Optional: CSS Modules
|
|
|
|
|
|
|
|
|
2025-05-25 23:21:15 +02:00
|
|
|
function SearchBar({ onSearch, timeout }) {
|
2025-05-17 00:27:49 +02:00
|
|
|
|
|
|
|
const [search_input, setSearchInput] = useState('');
|
2025-05-25 23:21:15 +02:00
|
|
|
const timeout_id = useRef(null);
|
|
|
|
timeout = timeout | 0;
|
2025-05-17 00:27:49 +02:00
|
|
|
|
|
|
|
const handleInputChange = (event) => {
|
|
|
|
|
|
|
|
const new_search_input = event.target.value;
|
2025-05-17 16:33:49 +02:00
|
|
|
setSearchInput(new_search_input);
|
2025-05-17 00:27:49 +02:00
|
|
|
|
|
|
|
if (timeout_id.current) {
|
|
|
|
clearTimeout(timeout_id.current);
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout_id.current = setTimeout(() => {
|
2025-05-17 16:33:49 +02:00
|
|
|
onSearch(new_search_input);
|
2025-05-25 23:21:15 +02:00
|
|
|
}, timeout);
|
2025-05-17 00:27:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
|
|
|
if (timeout_id.current) {
|
|
|
|
clearTimeout(timeout_id.current);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.searchBarContainer}>
|
|
|
|
<img src={SearchIcon} className={styles.searchIcon}></img>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
placeholder="Search"
|
|
|
|
value={search_input}
|
2025-05-17 16:33:49 +02:00
|
|
|
onInput={handleInputChange}
|
2025-05-17 00:27:49 +02:00
|
|
|
className={styles.searchBarInput}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2025-05-16 13:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default SearchBar;
|