File tree 3 files changed +17
-16
lines changed
3 files changed +17
-16
lines changed Original file line number Diff line number Diff line change 1
1
import styles from '@/styles/SearchBar.module.scss' ;
2
2
import { useRef } from 'react' ;
3
3
4
- const SearchBar = ( { items , setSearchTerm, setSearchResults } ) => {
4
+ const SearchBar = ( { setSearchTerm } ) => {
5
5
const searchInput = useRef ( null ) ;
6
6
const search = ( ) => {
7
- const searchTerm = searchInput . current . value ;
8
- if ( ! searchTerm ) return setSearchResults ( null ) ;
9
- const results = items . filter (
10
- post =>
11
- post . title . toLowerCase ( ) . includes ( searchTerm . toLowerCase ( ) ) ||
12
- post . content . toLowerCase ( ) . includes ( searchTerm . toLowerCase ( ) ) ||
13
- post . tagList . some ( tag =>
14
- tag . toLowerCase ( ) . includes ( searchTerm . toLowerCase ( ) )
15
- )
16
- ) ;
17
- setSearchResults ( results ) ;
18
- setSearchTerm ( searchTerm ) ;
7
+ setSearchTerm ( searchInput . current . value ) ;
19
8
} ;
20
9
return (
21
10
< div className = { styles . searchBar } >
Original file line number Diff line number Diff line change @@ -5,17 +5,18 @@ import Title from '@/components/snippets/Title';
5
5
import styles from '@/styles/Blog.module.scss' ;
6
6
import { blogRevalidate } from '@/utils/config' ;
7
7
import { tagToHeading } from '@/utils/blogCategories' ;
8
+ import { blogSearch } from '@/utils/search' ;
8
9
9
10
export default function Blog ( { posts } ) {
10
11
const [ searchResults , setSearchResults ] = useState ( null ) ;
11
- const [ searchTerm , setSearchTerm ] = useState ( 'temp ' ) ;
12
+ const [ searchTerm , setSearchTerm ] = useState ( '' ) ;
12
13
const getPostsByTag = tag => {
13
14
return posts . filter ( post => post . tagList . includes ( tag ) ) ;
14
15
} ;
15
16
16
17
useEffect ( ( ) => {
17
- console . log ( 'search result' , searchResults ) ;
18
- } , [ searchResults ] ) ;
18
+ setSearchResults ( blogSearch ( posts , searchTerm ) ) ;
19
+ } , [ searchTerm ] ) ;
19
20
20
21
return (
21
22
< >
Original file line number Diff line number Diff line change
1
+ export const blogSearch = ( posts , searchTerm ) => {
2
+ if ( ! searchTerm ) return null ;
3
+ return posts . filter (
4
+ post =>
5
+ post . title . toLowerCase ( ) . includes ( searchTerm . toLowerCase ( ) ) ||
6
+ post . content . toLowerCase ( ) . includes ( searchTerm . toLowerCase ( ) ) ||
7
+ post . tagList . some ( tag =>
8
+ tag . toLowerCase ( ) . includes ( searchTerm . toLowerCase ( ) )
9
+ )
10
+ ) ;
11
+ } ;
You can’t perform that action at this time.
0 commit comments