Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 81 additions & 3 deletions src/components/Input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "./input.scss";
import { fetchData } from "../../utils/fetch-data";
import { debounce } from "../../utils/deboucne";
import Loader from "../Loader";
import { useEffect, useMemo, useState } from "react";

export interface InputProps {
/** Placeholder of the input */
Expand All @@ -12,12 +13,89 @@ export interface InputProps {

const Input = ({ placeholder, onSelectItem }: InputProps) => {
// DO NOT remove this log
console.log('input re-render')
console.log("input re-render");

// Your code start here
return <input></input>
const [searchResults, setSearchResults] = useState([]);
const [inputValue, setInputValue] = useState("");
const [isLoading, setIsLoading] = useState(true);
const [errMessage, setErrMessage] = useState("");

useEffect(() => {
let ignore = false;
setSearchResults([]);
setIsLoading(false);
setErrMessage('');

if (!inputValue) {
return;
}

fetchData(inputValue)
.then((res) => {
console.log("result:", res);
if (!ignore) {
setSearchResults(res as []);
if (!res.length) {
setErrMessage("No results");
}
setIsLoading(true);

}
})
.catch((error) => {
console.log("error");
setIsLoading(true);
setErrMessage(error);
});
return () => {
ignore = true;
}
}, [inputValue]);

const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setInputValue(e.target.value);
};

const debouncedOnChange = debounce(onChange, 500);

const generateSearchResults = useMemo(() => {
return inputValue && (
<div className="search-result">
{!isLoading ? <Loader /> : (
<div className="lists">
<div className="error-message">{errMessage}</div>
{searchResults.map((item, index) => {
return (
<div
key={item + '-' + index}
className="item"
onClick={() => onSelectItem(item)}
>
{item}
</div>
);
})}
</div>
)}

</div>
);
}, [searchResults, isLoading]);

return (
<>
<div>
<input
className="input"
placeholder={placeholder}
onChange={debouncedOnChange}
/>
{generateSearchResults}
</div>
</>
);
// Your code end here
};

export default Input;

117 changes: 114 additions & 3 deletions src/components/Input/input.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,117 @@
* {
box-sizing: border-box;
box-sizing: border-box;
}
html{
font-size: 16px;
html {
font-size: 16px;
}
.input-search-container {
width: 300px;
}
.input {
border-radius: 4px;
line-height: 1.5em;
font-size: 16px;
padding: 0.5em 1em;
width: 100%;
}
.search-result {
width: 100%;
position: relative;
border-radius: 4px;
min-height: 100px;
border: solid 1px #ddd;
margin-top: 4px;
}
.lists {
display: flex;
flex-direction: column;
}
.item {
padding: 1em 2em;
}
.item:hover {
background-color: #eee;
cursor: pointer;
}
.no-result {
font-style: italic;
color: #333;
text-align: center;
}
.error-message {
color: red;
font-style: italic;
padding: 0.5em 1em;
}
.loader-container {
height: 50px;
width: 50px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0.8);
}
.loader {
animation: rotate 1s infinite;
height: 50px;
width: 50px;
position: absolute;
}
.loader:before,
.loader:after {
border-radius: 50%;
content: "";
display: block;
height: 20px;
width: 20px;
}
.loader:before {
animation: ball1 1s infinite;
background-color: #ccc;
box-shadow: 30px 0 #333;
margin-bottom: 10px;
}
.loader:after {
animation: ball2 1s infinite;
background-color: #333;
box-shadow: 30px 0 #ccc;
}
@keyframes rotate {
0% {
transform: rotate(0) scale(0.8);
}
50% {
transform: rotate(360deg) scale(1.2);
}
to {
transform: rotate(720deg) scale(0.8);
}
}
@keyframes ball1 {
0% {
box-shadow: 30px 0 #333;
}
50% {
box-shadow: 0 0 #333;
margin-bottom: 0;
transform: translate(15px, 15px);
}
to {
box-shadow: 30px 0 #333;
margin-bottom: 10px;
}
}
@keyframes ball2 {
0% {
box-shadow: 30px 0 #ccc;
}
50% {
box-shadow: 0 0 #ccc;
margin-top: -20px;
transform: translate(15px, 15px);
}
to {
box-shadow: 30px 0 #ccc;
margin-top: 0;
}
}