-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSearch.tsx
More file actions
40 lines (34 loc) · 1.07 KB
/
Copy pathSearch.tsx
File metadata and controls
40 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use client'
import React, { useEffect, useState } from 'react';
import useDebounce from '../libs/useDebounce'
const Search = ({searchTerms} : {searchTerms?: string | undefined}) => {
const [search, setSearch] = useState(searchTerms || '');
// Debouncing taken from
// https://hackernoon.com/how-to-use-debounce-in-nextjs
const debouncedSearch = useDebounce(search, 500)
useEffect(() => {
updateView(debouncedSearch)
}, [debouncedSearch])
const updateView = (inputValue: string) => {
window.history.replaceState({}, '', `/?searchterms=${inputValue}`)
}
return (
<div className="container max-w-5xl mx-auto my-10">
<input
type="text"
placeholder="Search something..."
className="
w-full text-center placeholder-center py-2 focus:outline-none
bg-gray-100
border-b-2 border-gray-100
focus:border-b-2 focus:border-gray-300
text-gray-600
"
value={search}
onChange={(e) => setSearch(e.target.value)}
/>
{/* <button onClick={() => setSearch(search => `${search} NEW`)}>New</button> */}
</div>
);
};
export default Search;