/* eslint-disable @typescript-eslint/no-explicit-any */ import { DefaultSetupValues } from "@/Constant/enums"; import { ErrorMessage, useFormikContext } from "formik"; import React, { useState } from "react"; import PhoneInput from "react-phone-number-input"; import { getCountryCallingCode } from "react-phone-number-input/input"; import { getCountries } from "react-phone-number-input/input"; import en from "react-phone-number-input/locale/en.json"; import type { CountryCode } from "libphonenumber-js/core"; import Select from "react-select"; import type { Props as SelectProps } from "react-select"; // import "./PhoneInputCustom.css"; interface FormValues { first_name: string; last_name: string; email: string; phone: string; password: string; confirm_password: string; user_type: string; // Corporate-only fields user_type_id: string; nationality: string; city: string; road: string; block: string; building: string; address_line1: string; address_line2: string; organization_name: string; organization_reg_no: string; vat_number: string; first_name_ar: string; last_name_ar: string; description: string; } interface PhoneInputFieldProps { fieldName?: string; value?: string; onChange?: (value: string) => void; placeholder?: string; error?: string; className?: string; isFormik?: boolean; country?: string; // Optional country ISO2 code onCountryChange?: (country: string) => void; // Optional handler for country change } const customLabels = getCountries().reduce( (acc, country) => { const countryName = en[country]; const callingCode = getCountryCallingCode(country); if (countryName && callingCode) { acc[country] = `${countryName} (+${callingCode})`; } return acc; }, { ...en } as Record ); // Add a mapping from country code to flag emoji const countryFlagEmoji: Record = { AF: "🇦🇫", AX: "🇦🇽", AL: "🇦🇱", DZ: "🇩🇿", AS: "🇦🇸", AD: "🇦🇩", AO: "🇦🇴", AI: "🇦🇮", AQ: "🇦🇶", AG: "🇦🇬", AR: "🇦🇷", AM: "🇦🇲", AW: "🇦🇼", AU: "🇦🇺", AT: "🇦🇹", AZ: "🇦🇿", BS: "🇧🇸", BH: "🇧🇭", BD: "🇧🇩", BB: "🇧🇧", BY: "🇧🇾", BE: "🇧🇪", BZ: "🇧🇿", BJ: "🇧🇯", BM: "🇧🇲", BT: "🇧🇹", BO: "🇧🇴", BA: "🇧🇦", BW: "🇧🇼", BR: "🇧🇷", IO: "🇮🇴", BN: "🇧🇳", BG: "🇧🇬", BF: "🇧🇫", BI: "🇧🇮", KH: "🇰🇭", CM: "🇨🇲", CA: "🇨🇦", CV: "🇨🇻", KY: "🇰🇾", CF: "🇨🇫", TD: "🇹🇩", CL: "🇨🇱", CN: "🇨🇳", CX: "🇨🇽", CC: "🇨🇨", CO: "🇨🇴", KM: "🇰🇲", CG: "🇨🇬", CD: "🇨🇩", CK: "🇨🇰", CR: "🇨🇷", CI: "🇨🇮", HR: "🇭🇷", CU: "🇨🇺", CY: "🇨🇾", CZ: "🇨🇿", DK: "🇩🇰", DJ: "🇩🇯", DM: "🇩🇲", DO: "🇩🇴", EC: "🇪🇨", EG: "🇪🇬", SV: "🇸🇻", GQ: "🇬🇶", ER: "🇪🇷", EE: "🇪🇪", ET: "🇪🇹", FK: "🇫🇰", FO: "🇫🇴", FJ: "🇫🇯", FI: "🇫🇮", FR: "🇫🇷", GF: "🇬🇫", PF: "🇵🇫", GA: "🇬🇦", GM: "🇬🇲", GE: "🇬🇪", DE: "🇩🇪", GH: "🇬🇭", GI: "🇬🇮", GR: "🇬🇷", GL: "🇬🇱", GD: "🇬🇩", GP: "🇬🇵", GU: "🇬🇺", GT: "🇬🇹", GG: "🇬🇬", GN: "🇬🇳", GW: "🇬🇼", GY: "🇬🇾", HT: "🇭🇹", VA: "🇻🇦", HN: "🇭🇳", HK: "🇭🇰", HU: "🇭🇺", IS: "🇮🇸", IN: "🇮🇳", ID: "🇮🇩", IR: "🇮🇷", IQ: "🇮🇶", IE: "🇮🇪", IM: "🇮🇲", IL: "🇮🇱", IT: "🇮🇹", JM: "🇯🇲", JP: "🇯🇵", JE: "🇯🇪", JO: "🇯🇴", KZ: "🇰🇿", KE: "🇰🇪", KI: "🇰🇮", KP: "🇰🇵", KR: "🇰🇷", KW: "🇰🇼", KG: "🇰🇬", LA: "🇱🇦", LV: "🇱🇻", LB: "🇱🇧", LS: "🇱🇸", LR: "🇱🇷", LY: "🇱🇾", LI: "🇱🇮", LT: "🇱🇹", LU: "🇱🇺", MO: "🇲🇴", MK: "🇲🇰", MG: "🇲🇬", MW: "🇲🇼", MY: "🇲🇾", MV: "🇲🇻", ML: "🇲🇱", MT: "🇲🇹", MH: "🇲🇭", MQ: "🇲🇶", MR: "🇲🇷", MU: "🇲🇺", YT: "🇾🇹", MX: "🇲🇽", FM: "🇫🇲", MD: "🇲🇩", MC: "🇲🇨", MN: "🇲🇳", ME: "🇲🇪", MS: "🇲🇸", MA: "🇲🇦", MZ: "🇲🇿", MM: "🇲🇲", NA: "🇳🇦", NR: "🇳🇷", NP: "🇳🇵", NL: "🇳🇱", NC: "🇳🇨", NZ: "🇳🇿", NI: "🇳🇮", NE: "🇳🇪", NG: "🇳🇬", NU: "🇳🇺", NF: "🇳🇫", MP: "🇲🇵", NO: "🇳🇴", OM: "🇴🇲", PK: "🇵🇰", PW: "🇵🇼", PS: "🇵🇸", PA: "🇵🇦", PG: "🇵🇬", PY: "🇵🇾", PE: "🇵🇪", PH: "🇵🇭", PN: "🇵🇳", PL: "🇵🇱", PT: "🇵🇹", PR: "🇵🇷", QA: "🇶🇦", RO: "🇷🇴", RU: "🇷🇺", RW: "🇷🇼", RE: "🇷🇪", BL: "🇧🇱", SH: "🇸🇭", KN: "🇰🇳", LC: "🇱🇨", MF: "🇲🇫", PM: "🇵🇲", VC: "🇻🇨", WS: "🇼🇸", SM: "🇸🇲", ST: "🇸🇹", SA: "🇸🇦", SN: "🇸🇳", RS: "🇷🇸", SC: "🇸🇨", SL: "🇸🇱", SG: "🇸🇬", SK: "🇸🇰", SI: "🇸🇮", SB: "🇸🇧", SO: "🇸🇴", ZA: "🇿🇦", SS: "🇸🇸", GS: "🇬🇸", ES: "🇪🇸", LK: "🇱🇰", SD: "🇸🇩", SR: "🇸🇷", SJ: "🇸🇯", SZ: "🇸🇿", SE: "🇸🇪", CH: "🇨🇭", SY: "🇸🇾", TW: "🇹🇼", TJ: "🇹🇯", TZ: "🇹🇿", TH: "🇹🇭", TL: "🇹🇱", TG: "🇹🇬", TK: "🇹🇰", TO: "🇹🇴", TT: "🇹🇹", TN: "🇹🇳", TR: "🇹🇷", TM: "🇹🇲", TC: "🇹🇨", TV: "🇹🇻", UG: "🇺🇬", UA: "🇺🇦", AE: "🇦🇪", GB: "🇬🇧", US: "🇺🇸", UY: "🇺🇾", UZ: "🇺🇿", VU: "🇻🇺", VE: "🇻🇪", VN: "🇻🇳", VG: "🇻🇬", VI: "🇻🇮", WF: "🇼🇫", YE: "🇾🇪", ZM: "🇿🇲", ZW: "🇿🇼" }; // 1. Custom country select component interface CountrySelectOption { value: string | undefined; label: string; } interface CustomCountrySelectProps extends Omit, "value" | "onChange" | "options"> { value?: string; onChange: (value?: string) => void; options: CountrySelectOption[]; } const CustomCountrySelect: React.FC = ({ value, onChange, options, ...rest }) => { // Find the selected option object const selectedOption = options.find(opt => opt.value === value); // Custom styles for react-select const customStyles = { container: (provided: any) => ({ ...provided, width: "36px", height: "36px", minWidth: "36px", minHeight: "36px", }), control: (provided: any) => ({ ...provided, minHeight: "36px", height: "36px", }), indicatorsContainer: (provided: any) => ({ ...provided, height: "36px", }), valueContainer: (provided: any) => ({ ...provided, height: "36px", padding: "0 6px", }), option: (provided: any, state: any) => ({ ...provided, padding: '4px 8px', fontSize: '0.75rem', backgroundColor: state.isFocused ? '#f0f0f0' : '#fff', color: '#222', }), menu: (provided: any) => ({ ...provided, width: "100%", minWidth: 150, }), }; // Custom SingleValue component to show only the flag const SingleValue = (props: any) => { const countryCode = props.data.value; return (
{countryCode && countryFlagEmoji[countryCode] ? countryFlagEmoji[countryCode] : null}
); }; return (