-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
232 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,64 @@ | ||
import type { ChangeEvent } from 'react'; | ||
import { useCallback, useMemo, useEffect, useState } from 'react'; | ||
import { TextField, Box, Button } from '@mui/material'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { debounce } from 'es-toolkit/compat'; | ||
|
||
const sanitizeQuery = (query: string): string => { | ||
return query.replace(/[^\s\w]/gi, '').trim(); | ||
}; | ||
|
||
interface SearchBarProps { | ||
searchQuery: string; | ||
setSearchQuery: (query: string) => void; | ||
onSearchChange: (query: string) => void; | ||
} | ||
|
||
const SearchBar: React.FC<SearchBarProps> = ({ | ||
searchQuery, | ||
setSearchQuery, | ||
}) => ( | ||
<Box | ||
display='flex' | ||
alignItems='center' | ||
mt={3} | ||
mx={2} | ||
> | ||
<TextField | ||
label='Search organizations...' | ||
variant='outlined' | ||
fullWidth | ||
sx={{ marginRight: 1 }} | ||
value={searchQuery} | ||
onChange={(e) => setSearchQuery(e.target.value)} | ||
/> | ||
<Button variant='contained'>Recommend</Button> | ||
</Box> | ||
); | ||
const SearchBar = ({ onSearchChange }: SearchBarProps) => { | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
const [query, setQuery] = useState(searchParams.get('search') || ''); | ||
|
||
const debouncedUpdate = useMemo( | ||
() => | ||
debounce((value: string) => { | ||
setSearchParams({ search: value || '' }, { replace: true }); | ||
onSearchChange(value); | ||
}, 300), | ||
[setSearchParams, onSearchChange] | ||
); | ||
|
||
const handleInputChange = useCallback( | ||
(e: ChangeEvent<HTMLInputElement>) => { | ||
const sanitizedQuery = sanitizeQuery(e.target.value); | ||
setQuery(sanitizedQuery); | ||
debouncedUpdate(sanitizedQuery); | ||
}, | ||
[debouncedUpdate] | ||
); | ||
|
||
// Initialize the search query from URL on mount | ||
useEffect(() => { | ||
const initialQuery = searchParams.get('search') || ''; | ||
setQuery(initialQuery); | ||
onSearchChange(initialQuery); | ||
}, [searchParams, onSearchChange]); | ||
|
||
return ( | ||
<Box | ||
display='flex' | ||
alignItems='center' | ||
mt={3} | ||
mx={2} | ||
> | ||
<TextField | ||
label='Search organizations...' | ||
variant='outlined' | ||
fullWidth | ||
sx={{ marginRight: 1 }} | ||
value={query} | ||
onChange={handleInputChange} | ||
/> | ||
<Button variant='contained'>Recommend</Button> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default SearchBar; |
Oops, something went wrong.