1
- import { FormEventHandler , useState } from 'react' ;
1
+ import { FormEventHandler , MouseEventHandler , useRef , useState } from 'react' ;
2
2
import Link from 'next/link' ;
3
3
4
4
import languages from 'assets/languages.json' ;
@@ -11,7 +11,16 @@ const { main: mainLanguages, others: otherLanguages } = languages;
11
11
12
12
function Hero ( ) {
13
13
const [ errorMessage , setErrorMessage ] = useState < string | null > ( null ) ;
14
+ const [ isSearchEmpty , setIsSearchEmpty ] = useState ( true ) ;
15
+ const formRef = useRef < HTMLFormElement | null > ( null ) ;
14
16
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
+ } ;
15
24
const handleSubmit : FormEventHandler = e => {
16
25
e . preventDefault ( ) ;
17
26
const formData = new FormData ( e . target as HTMLFormElement ) ;
@@ -34,16 +43,31 @@ function Hero() {
34
43
Search your language
35
44
</ h1 >
36
45
< form
46
+ ref = { formRef }
37
47
className = "form-control w-full max-w-xs mx-auto items-center mt-10 mb-12"
38
48
onSubmit = { handleSubmit }
39
49
>
40
50
< 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 >
47
71
< button
48
72
type = "submit"
49
73
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 = () => (
106
130
/>
107
131
</ svg >
108
132
) ;
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
+ } ;
109
151
110
152
export default Hero ;
0 commit comments