Skip to content

Commit 634fe01

Browse files
massahoudoumassahoudou
authored andcommitted
feat: Added clear field functionality and searchEmpty check
In this update, we've added the ability to clear the search field in the Hero component and introduced a check using the `searchEmpty` variable. Key additions include: - The creation of a handleClear function to reset the form and clear error messages when the "Clear" button is clicked. - The addition of a "Clear" icon component for the clear button. - The use of a form reference (formRef) for easier access to form elements. - Conditional display of the "Clear" button only when the search field is not empty and `searchEmpty` is false. This enhancement improves the user experience by allowing users to reset the search field more conveniently while also considering the `searchEmpty` variable. A new branch has also been created for these changes.
1 parent 9d67f54 commit 634fe01

File tree

2 files changed

+4926
-8
lines changed

2 files changed

+4926
-8
lines changed

components/Hero.tsx

Lines changed: 43 additions & 8 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,13 +11,24 @@ const { main: mainLanguages, others: otherLanguages } = languages;
1111

1212
function Hero() {
1313
const [errorMessage, setErrorMessage] = useState<string | null>(null);
14+
const [searchEmpty, setSearchEmpty] = useState<boolean>(false);
15+
const formRef = useRef<HTMLFormElement | null>(null);
1416
const router = useRouter();
17+
const handleClear: MouseEventHandler<HTMLButtonElement> = () => {
18+
if (formRef.current && !searchEmpty ) {
19+
20+
formRef.current.reset();
21+
setErrorMessage(null);
22+
}
23+
24+
}
1525
const handleSubmit: FormEventHandler = e => {
1626
e.preventDefault();
1727
const formData = new FormData(e.target as HTMLFormElement);
1828
const search = (formData.get('search') as string).trim();
1929
// Check if the input is empty or contains only spaces
2030
if (search === '') {
31+
2132
setErrorMessage('Empty search terms invalid!');
2233
return;
2334
}
@@ -33,22 +44,37 @@ function Hero() {
3344
<h1 className="mb-5 text-2023-manga-3 text-5xl font-bold uppercase">
3445
Search your language
3546
</h1>
36-
<form
47+
<form ref={formRef}
3748
className="form-control w-full max-w-xs mx-auto items-center mt-10 mb-12"
3849
onSubmit={handleSubmit}
3950
>
4051
<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-
/>
52+
<div className='flex relative'>
53+
<input
54+
type="text"
55+
placeholder="Search for your language"
56+
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"
57+
name="search"
58+
onChange={
59+
(e) => {
60+
setSearchEmpty(e.target.value.trim() === '') ;
61+
console.log(searchEmpty);
62+
}
63+
}
64+
/>
65+
66+
{!searchEmpty &&
67+
<button onClick={handleClear} className='absolute right-0 top-0 bottom-0 p-2'>
68+
<ClearIcon />
69+
</button>}
70+
71+
</div>
4772
<button
4873
type="submit"
4974
className="group btn btn-square rounded-tl-none rounded-bl-none bg-transparent border-2023-manga-3 hover:bg-2023-manga-2 hover:text-2023-void-2 hover:border-2023-manga-2"
5075
>
5176
<SearchIcon />
77+
5278
</button>
5379
</div>
5480
</form>
@@ -106,5 +132,14 @@ const SearchIcon = () => (
106132
/>
107133
</svg>
108134
);
135+
const ClearIcon = () => {
136+
return <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-4 h-4 text-white ">
137+
<path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
138+
</svg>
139+
140+
141+
}
109142

110143
export default Hero;
144+
145+

0 commit comments

Comments
 (0)