[go: nahoru, domu]

Skip to content

Commit

Permalink
Ref to Select Search component
Browse files Browse the repository at this point in the history
  • Loading branch information
AmirrMahmoudi committed Jan 6, 2024
1 parent 1c59a2c commit b0ed9b8
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import StarRating from "./StarRating";

const average = (arr) =>
Expand Down Expand Up @@ -35,7 +35,6 @@ export default function App() {

function handleDeleteWatched(id) {
setWatched((watched) => watched.filter((movie) => movie.imdbID !== id));

}

useEffect(
Expand Down Expand Up @@ -162,13 +161,36 @@ const Logo = () => {
};

const Search = ({ query, setQuery }) => {
const inputEl = useRef(null);

useEffect(
function () {
function callback(e) {
if (document.activeElement === inputEl.current) {
return;
}

if (e.code === "Enter") {
inputEl.current.focus();
setQuery("");
}
}
document.addEventListener("keydown", callback);
return () => document.addEventListener("keydown", callback);
},
[setQuery]
);



return (
<input
className="search"
type="text"
placeholder="Search movies..."
value={query}
onChange={(e) => setQuery(e.target.value)}
ref={inputEl}
/>
);
};
Expand Down

0 comments on commit b0ed9b8

Please sign in to comment.