fix(search): sync search states on navigation
All checks were successful
Release to Staging / build_and_release (push) Successful in 1m7s
All checks were successful
Release to Staging / build_and_release (push) Successful in 1m7s
This commit is contained in:
parent
ccc5de0006
commit
a8361c2d7e
@ -3,10 +3,11 @@ import { forwardRef } from 'react'
|
|||||||
interface SearchInputProps {
|
interface SearchInputProps {
|
||||||
handleKeyDown: (event: React.KeyboardEvent<HTMLInputElement>) => void
|
handleKeyDown: (event: React.KeyboardEvent<HTMLInputElement>) => void
|
||||||
handleSearch: () => void
|
handleSearch: () => void
|
||||||
|
handleChange?: (event: React.ChangeEvent<HTMLInputElement>) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SearchInput = forwardRef<HTMLInputElement, SearchInputProps>(
|
export const SearchInput = forwardRef<HTMLInputElement, SearchInputProps>(
|
||||||
({ handleKeyDown, handleSearch }, ref) => (
|
({ handleKeyDown, handleSearch, handleChange }, ref) => (
|
||||||
<div className='SearchMain'>
|
<div className='SearchMain'>
|
||||||
<div className='SearchMainInside'>
|
<div className='SearchMainInside'>
|
||||||
<div className='SearchMainInsideWrapper'>
|
<div className='SearchMainInsideWrapper'>
|
||||||
@ -15,6 +16,7 @@ export const SearchInput = forwardRef<HTMLInputElement, SearchInputProps>(
|
|||||||
className='SMIWInput'
|
className='SMIWInput'
|
||||||
ref={ref}
|
ref={ref}
|
||||||
onKeyDown={handleKeyDown}
|
onKeyDown={handleKeyDown}
|
||||||
|
onChange={handleChange}
|
||||||
placeholder='Enter search term'
|
placeholder='Enter search term'
|
||||||
/>
|
/>
|
||||||
<button
|
<button
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { useMemo, useRef, useState } from 'react'
|
import { useEffect, useMemo, useRef, useState } from 'react'
|
||||||
import { useLoaderData, useNavigation, useSearchParams } from 'react-router-dom'
|
import { useLoaderData, useNavigation, useSearchParams } from 'react-router-dom'
|
||||||
import { useLocalStorage } from 'hooks'
|
import { useLocalStorage } from 'hooks'
|
||||||
import { BlogCardDetails, NSFWFilter, SortBy } from 'types'
|
import { BlogCardDetails, NSFWFilter, SortBy } from 'types'
|
||||||
@ -28,9 +28,15 @@ export const BlogsPage = () => {
|
|||||||
// Search
|
// Search
|
||||||
const searchTermRef = useRef<HTMLInputElement>(null)
|
const searchTermRef = useRef<HTMLInputElement>(null)
|
||||||
const [searchParams, setSearchParams] = useSearchParams()
|
const [searchParams, setSearchParams] = useSearchParams()
|
||||||
const [searchTerm, setSearchTerm] = useState(searchParams.get('q') || '')
|
useEffect(() => {
|
||||||
|
// Keep the states synced with the URL
|
||||||
|
const q = searchParams.get('q')
|
||||||
|
if (searchTermRef.current) searchTermRef.current.value = q ?? ''
|
||||||
|
setSearchTerm(q ?? '')
|
||||||
|
}, [searchParams])
|
||||||
|
const [searchTerm, setSearchTerm] = useState('')
|
||||||
const handleSearch = () => {
|
const handleSearch = () => {
|
||||||
const value = searchTermRef.current?.value || '' // Access the input value from the ref
|
const value = searchTermRef.current?.value ?? '' // Access the input value from the ref
|
||||||
setSearchTerm(value)
|
setSearchTerm(value)
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
@ -43,6 +49,17 @@ export const BlogsPage = () => {
|
|||||||
replace: true
|
replace: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
// Reset search automatically when deleting last entry
|
||||||
|
const handleChange = () => {
|
||||||
|
const value = searchTermRef.current?.value ?? '' // Access the input value from the ref
|
||||||
|
if (!value && searchParams.get('q')) {
|
||||||
|
searchParams.delete('q')
|
||||||
|
setSearchParams(searchParams, {
|
||||||
|
replace: true
|
||||||
|
})
|
||||||
|
handleSearch()
|
||||||
|
}
|
||||||
|
}
|
||||||
const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
|
const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
|
||||||
if (event.key === 'Enter') {
|
if (event.key === 'Enter') {
|
||||||
handleSearch()
|
handleSearch()
|
||||||
@ -126,6 +143,7 @@ export const BlogsPage = () => {
|
|||||||
ref={searchTermRef}
|
ref={searchTermRef}
|
||||||
handleKeyDown={handleKeyDown}
|
handleKeyDown={handleKeyDown}
|
||||||
handleSearch={handleSearch}
|
handleSearch={handleSearch}
|
||||||
|
handleChange={handleChange}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user