From ced5849776c3e2518b48f4229fa0206d60178990 Mon Sep 17 00:00:00 2001 From: "tung.dam" <65003120+sonhoang2009@users.noreply.github.com> Date: Sat, 5 Oct 2024 23:20:18 +0700 Subject: [PATCH] no message --- src/components/Input/index.tsx | 68 ++++++++++++++++++++++++++++++++- src/components/Input/input.scss | 18 +++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/src/components/Input/index.tsx b/src/components/Input/index.tsx index 453c742..609ea2d 100644 --- a/src/components/Input/index.tsx +++ b/src/components/Input/index.tsx @@ -1,6 +1,7 @@ import "./input.scss"; import { fetchData } from "../../utils/fetch-data"; import { debounce } from "../../utils/deboucne"; +import { useState,useEffect} from "react"; import Loader from "../Loader"; export interface InputProps { @@ -15,7 +16,72 @@ const Input = ({ placeholder, onSelectItem }: InputProps) => { console.log('input re-render') // Your code start here - return + + const [isLoading, setLoading] = useState(false); + const [results, setResults] = useState(['']); + const [inputValue, setInputValue] = useState(""); + const [message, setMessage] = useState(""); + + useEffect(() => { + + if (!inputValue) return; + let ignore = false; + setLoading(false); + + fetchData(inputValue).then(res => { + if (ignore) return; + setResults(res); + setMessage(""); + }).catch(err => { + if (ignore) return; + setMessage(err); + }) + setLoading(false); + + return () => { + setLoading(false); + ignore = true; + } + }, [inputValue]) + + const handleChangeInput = debounce((event: React.ChangeEvent) => { + setMessage('') + setInputValue(event.target.value) + }, 100) + + const renderResults = () => { + if (isLoading) { + return + } + if(message) + return
+ if (results && results.length > 0) { + return ( +
    + {results.map((value, index) => ( +
  • onSelectItem(value)}> + {value} +
  • + ))} +
+ ); + }else{ + return
+ } + + }; + return
+ + {!!inputValue && +
+ {renderResults()} +
} +
+ // Your code end here }; diff --git a/src/components/Input/input.scss b/src/components/Input/input.scss index 1dafbe7..4515e37 100644 --- a/src/components/Input/input.scss +++ b/src/components/Input/input.scss @@ -4,3 +4,21 @@ html{ font-size: 16px; } +.erorr{ + color: rgb(241, 10, 10); +} +ul { + list-style: none; + } + + ul > li { + padding: 8px 12px; + } + + ul > li:hover { + background-color: rgb(31, 30, 30); + color: #fff; + cursor: pointer; + } + + \ No newline at end of file