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
1 change: 0 additions & 1 deletion src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
Expand Down
35 changes: 8 additions & 27 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
import './App.css';
import Input from './components/Input';

function App() {
const [count, setCount] = useState(0)
const placeholder = "Type something to search...";

const handleSelectItem = (value: any) => {
alert(`Search result: ${value}`);
};

return (
<>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</>
<Input placeholder={placeholder} onSelectItem={handleSelectItem} />
)
}

Expand Down
61 changes: 56 additions & 5 deletions src/components/Input/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,72 @@
import "./input.scss";
import { fetchData } from "../../utils/fetch-data";
import { debounce } from "../../utils/deboucne";
import { debounce } from "../../utils/debounce";
import Loader from "../Loader";
import { useEffect, useState } from "react";

export interface InputProps {
/** Placeholder of the input */
placeholder?: string;
/** On click item handler */
onSelectItem: (item: string) => void;
}

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

// Your code start here
return <input></input>
const DEBOUNCE_PERIOD = 100;

const [result, setResult] = useState<string[]>([]);
const [errMessage, setErrMessage] = useState('');
const [searchText, setSearchText] = useState('');
const [isLoading, setIsLoading] = useState(false);

useEffect(() => {
handleFetchingData();
console.log(result);
}, [searchText]);

const handleFetchingData = async () => {
setIsLoading(true);

try {
const response = await fetchData(searchText);
setSearchText(searchText);
setResult(response);
setErrMessage('');
} catch (error) {
setErrMessage(error as string);
setResult([]);
} finally {
setIsLoading(false);
}
};

const handleChangeInput = debounce((e: any) => {
setSearchText(e.target.value)
}, DEBOUNCE_PERIOD);

return (
<div className="search__panel">
<input
className="search__box"
placeholder={placeholder}
onChange={handleChangeInput}
/>
{isLoading && <Loader />}
{
result
? searchText && <ul className="search__results">
{result.map((item, index) => (
<li className={'search__item'} key={index} onClick={() => onSelectItem(item)}>{item}</li>
))}
</ul>
: <div className="search__message">No results</div>

}
{errMessage && <div className="search__error">{errMessage}</div>}
</div>
)
// Your code end here
};

Expand Down
43 changes: 41 additions & 2 deletions src/components/Input/input.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
* {
box-sizing: border-box;
}
html{
font-size: 16px;
}

.search {
&__panel {
max-width: 456px;
margin: 60px auto;
}

&__box {
width: 100%;
padding: 12px 20px;
}

&__results {
margin: 0;
padding: 0;
list-style-type: none;
max-height: 480px;
overflow: auto;
}

&__item {
padding: 12px 20px;
background-color: #fafafa;
cursor: pointer;
transition: all .3s;

&:hover {
background-color: #efefef;
}
}

&__message, &__error {
padding: 12px 20px;
background-color: #fafafa;
}

&__error {
color: rgba(255, 0, 0, 1);
background: rgba(255, 0, 0, 0.1);
}
}
4 changes: 0 additions & 4 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ a:hover {

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
Expand Down
File renamed without changes.