Skip to content

Commit 24590ba

Browse files
Merge pull request #201 from massdx/feat/clear-field
feat: Added clear field functionality and searchEmpty check
2 parents bdce96d + f5693d8 commit 24590ba

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

components/Hero.tsx

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FormEventHandler, useState } from 'react';
1+
import { FormEventHandler, MouseEventHandler, useRef, useState } from 'react';
22
import Link from 'next/link';
33

44
import languages from 'assets/languages.json';
@@ -11,7 +11,16 @@ const { main: mainLanguages, others: otherLanguages } = languages;
1111

1212
function Hero() {
1313
const [errorMessage, setErrorMessage] = useState<string | null>(null);
14+
const [isSearchEmpty, setIsSearchEmpty] = useState(true);
15+
const formRef = useRef<HTMLFormElement | null>(null);
1416
const router = useRouter();
17+
const handleClear: MouseEventHandler<HTMLButtonElement> = () => {
18+
if (formRef.current && !isSearchEmpty) {
19+
formRef.current.reset();
20+
setIsSearchEmpty(true);
21+
setErrorMessage(null);
22+
}
23+
};
1524
const handleSubmit: FormEventHandler = e => {
1625
e.preventDefault();
1726
const formData = new FormData(e.target as HTMLFormElement);
@@ -34,16 +43,31 @@ function Hero() {
3443
Search your language
3544
</h1>
3645
<form
46+
ref={formRef}
3747
className="form-control w-full max-w-xs mx-auto items-center mt-10 mb-12"
3848
onSubmit={handleSubmit}
3949
>
4050
<div className="flex w-full">
41-
<input
42-
type="text"
43-
placeholder="Search for your language"
44-
className="input input-bordered w-full text-neutral-100 border-2023-bavarian-gold-2 focus:outline-2023-bavarian-gold-2 max-w-xs rounded-tr-none rounded-br-none bg-transparent"
45-
name="search"
46-
/>
51+
<div className="flex relative w-full ">
52+
<input
53+
type="text"
54+
placeholder="Search for your language"
55+
className="input input-bordered w-full text-neutral-100 border-2023-bavarian-gold-2 focus:outline-2023-bavarian-gold-2 max-w-xs rounded-tr-none rounded-br-none bg-transparent"
56+
name="search"
57+
onChange={e => {
58+
setIsSearchEmpty(e.target.value.trim() === '');
59+
}}
60+
/>
61+
62+
{!isSearchEmpty && (
63+
<button
64+
onClick={handleClear}
65+
className="absolute right-0 top-0 bottom-0 p-2"
66+
>
67+
<ClearIcon />
68+
</button>
69+
)}
70+
</div>
4771
<button
4872
type="submit"
4973
className="group btn btn-square rounded-tl-none rounded-bl-none bg-transparent border-2023-bavarian-gold-2 hover:bg-2023-manga-2 hover:text-2023-void-2 hover:border-2023-manga-2"
@@ -106,5 +130,23 @@ const SearchIcon = () => (
106130
/>
107131
</svg>
108132
);
133+
const ClearIcon = () => {
134+
return (
135+
<svg
136+
xmlns="http://www.w3.org/2000/svg"
137+
fill="none"
138+
viewBox="0 0 24 24"
139+
strokeWidth={1.5}
140+
stroke="currentColor"
141+
className="w-4 h-4 text-white "
142+
>
143+
<path
144+
strokeLinecap="round"
145+
strokeLinejoin="round"
146+
d="M6 18L18 6M6 6l12 12"
147+
/>
148+
</svg>
149+
);
150+
};
109151

110152
export default Hero;

0 commit comments

Comments
 (0)