@@ -32,6 +32,7 @@ function SignupForm() {
3232
3333 const [ fetchedCountries , setFetchedCountries ] = useState < Country [ ] > ( [ ] ) ;
3434 const [ filteredCountries , setFilteredCountries ] = useState < Country [ ] > ( [ ] ) ;
35+ const [ selectedCountry , setSelectedCountry ] = useState < string > ( "" ) ;
3536
3637 const handleCountrySearch = ( event : React . ChangeEvent < HTMLSelectElement > ) => {
3738 const searchTerm = event . target . value . toLowerCase ( ) ;
@@ -53,10 +54,32 @@ function SignupForm() {
5354 fetchCountriesData ( ) ;
5455 } , [ ] ) ;
5556
57+
58+ const handleCountryChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
59+ const value = event . target . value ;
60+ setSelectedCountry ( value ) ;
61+
62+ // Filter countries based on the input
63+ const filtered = fetchedCountries . filter ( ( country ) =>
64+ country . name . toLowerCase ( ) . startsWith ( value . toLowerCase ( ) )
65+ ) ;
66+ setFilteredCountries ( filtered ) ;
67+
68+ // If a valid country is selected, update the country code
69+ const selectedCountryObj = fetchedCountries . find (
70+ ( country ) => country . name . toLowerCase ( ) === value . toLowerCase ( )
71+ ) ;
72+ if ( selectedCountryObj ) {
73+ setValue ( "countryCode" , `${ selectedCountryObj . phone } ${ selectedCountryObj . suffix } ` ) ;
74+ }
75+ } ;
76+
77+
5678 const {
5779 register,
5880 handleSubmit,
5981 formState : { errors } ,
82+ setValue,
6083 } = useForm < formData > ( {
6184 resolver : zodResolver ( registerSchema ) ,
6285 } ) ;
@@ -108,7 +131,7 @@ function SignupForm() {
108131 gender : parsedData . gender ,
109132 email : parsedData . email ,
110133 password : parsedData . password ,
111- telephone : parsedData . phoneNumber
134+ telephone : ` ${ parsedData . countryCode } ${ parsedData . phoneNumber } ` ,
112135 } ,
113136 } ,
114137 } ) ;
@@ -243,19 +266,21 @@ function SignupForm() {
243266 </ div >
244267 < div className = "flex items-center w-[25vw] gap-10 sm:w-5/6 lg:w-[25vw] justify-between" >
245268 < div className = "w-[50%]" >
246- < InputField
247- placeholder = "Country"
248- type = "text"
249- { ...register ( "country" ) }
250- list = "country"
251- error = { errors ?. country }
252- />
253- < Datalist
254- id = "country"
255- options = { filteredCountries . map ( ( country ) => ( {
256- value : country . name ,
257- } ) ) }
269+ < InputField
270+ placeholder = "Country"
271+ list = "countries"
272+ className = "w-full rounded-md px-2 py-3 border border-white placeholder:text-gray-400 text-white sm:text-[12px] outline-none autofill:bg-transparent autofill:text-white bg-[#1F2A37]"
273+ { ...register ( "country" , {
274+ onChange : ( e ) => handleCountryChange ( e ) ,
275+ } ) }
276+ error = { errors ?. country }
258277 />
278+ < datalist id = "countries" >
279+ { filteredCountries . map ( ( country ) => (
280+ < option key = { country . code } value = { country . name } />
281+ ) ) }
282+ </ datalist >
283+
259284 </ div >
260285 < div className = "w-[50%] " >
261286 < InputField
@@ -278,20 +303,13 @@ function SignupForm() {
278303
279304 < div className = "flex items-center w-[25vw] sm:w-5/6 lg:w-[25vw] justify-between" >
280305 < div className = "w-[20%] " >
281- < InputField
282- type = "text"
283- placeholder = "+250"
284- { ...register ( "countryCode" ) }
285- className = "w-full rounded-md px-2 py-3 border border-white placeholder:text-gray-400 text-white sm:text-[12px] outline-none autofill:bg-transparent autofill:text-white bg-[#1F2A37]"
286- list = "countryCodes"
287- error = { errors ?. countryCode }
288- />
289- < Datalist
290- id = "countryCodes" options = { filteredCountries . map ( ( country ) => ( {
291- value : country . name
292- } ) ) }
293- style = { { cursor : "pointer" } }
294- />
306+ < InputField
307+ placeholder = "Country Code"
308+ type = "text"
309+ className = "w-full rounded-md px-2 py-3 border border-white placeholder:text-gray-400 text-white sm:text-[12px] outline-none autofill:bg-transparent autofill:text-white bg-[#1F2A37]"
310+ { ...register ( "countryCode" ) }
311+ error = { errors ?. countryCode }
312+ />
295313 </ div >
296314 < div className = " w-[65%]" >
297315 < InputField
0 commit comments