Skip to content

Commit b507261

Browse files
committed
add live search
1 parent 90a4171 commit b507261

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

src/components/Navbar.jsx

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import logo from '../assets/images/logo.png';
33
import { Link, useNavigate } from 'react-router-dom'; // Import Link and useNavigate from react-router-dom
44

@@ -11,15 +11,13 @@ function Navbar({ loggedin }) {
1111
setSearchTerm(event.target.value);
1212
};
1313

14-
// Handle search submission
15-
const handleSearchSubmit = (event) => {
16-
event.preventDefault(); // Prevent page refresh
14+
// Use effect to trigger navigation whenever the search term changes
15+
useEffect(() => {
1716
if (searchTerm) {
18-
// Redirect to the search results page with the search term
17+
// Navigate to search page as the user types
1918
navigate(`/search?query=${encodeURIComponent(searchTerm)}`);
20-
// setSearchTerm(''); // Clear the input after submission
2119
}
22-
};
20+
}, [searchTerm, navigate]); // Trigger effect when searchTerm or navigate changes
2321

2422
return (
2523
<nav className="navbar navbar-expand-lg">
@@ -54,29 +52,26 @@ function Navbar({ loggedin }) {
5452
</li>
5553
</ul>
5654
<div className="d-flex align-items-center flex-column flex-lg-row">
57-
<form className="me-2 mb-2 mb-lg-0" onSubmit={handleSearchSubmit}>
58-
<div class="input-group">
59-
<span class="input-group-text" id="basic-addon1">
60-
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-search" viewBox="0 0 16 16">
55+
<div className="me-2 mb-2 mb-lg-0">
56+
<div className="input-group">
57+
<span className="input-group-text" id="basic-addon1">
58+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" className="bi bi-search" viewBox="0 0 16 16">
6159
<path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001q.044.06.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1 1 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0"></path>
6260
</svg>
6361
</span>
64-
{/* <input type="text" class="form-control" placeholder="Input group example" /> */}
65-
66-
<input
67-
type="text"
68-
className="form-control"
69-
// aria-label="Input group example"
70-
// aria-describedby="basic-addon1"
71-
placeholder="Search"
72-
value={searchTerm}
73-
onChange={handleSearchChange}
74-
/> </div>
75-
</form>
62+
<input
63+
type="text"
64+
className="form-control"
65+
placeholder="Search"
66+
value={searchTerm}
67+
onChange={handleSearchChange}
68+
/>
69+
</div>
70+
</div>
7671
{loggedin === 'true' ? (
77-
<Link className="btn btn-primary clk" to="/profile">Profile </Link>
72+
<Link className="btn btn-primary clk" to="/profile">Profile</Link>
7873
) : (
79-
<Link className="btn btn-primary clk" to="/login">Sign up </Link>
74+
<Link className="btn btn-primary clk" to="/login">Sign up</Link>
8075
)}
8176
</div>
8277
</div>

0 commit comments

Comments
 (0)