diff --git a/src/Common/static/countryPhoneAndFlags.json b/src/Common/static/countryPhoneAndFlags.json index 87d963444b5..98a43e4e043 100644 --- a/src/Common/static/countryPhoneAndFlags.json +++ b/src/Common/static/countryPhoneAndFlags.json @@ -57,11 +57,7 @@ "DJ": { "flag": "🇩🇯", "name": "Djibouti", "code": "253" }, "DK": { "flag": "🇩🇰", "name": "Denmark", "code": "45" }, "DM": { "flag": "🇩🇲", "name": "Dominica", "code": "1-767" }, - "DO": { - "flag": "🇩🇴", - "name": "Dominican Republic", - "code": "1" - }, + "DO": { "flag": "🇩🇴", "name": "Dominican Republic", "code": "1" }, "DZ": { "flag": "🇩🇿", "name": "Algeria", "code": "213" }, "EC": { "flag": "🇪🇨", "name": "Ecuador", "code": "593" }, "EE": { "flag": "🇪🇪", "name": "Estonia", "code": "372" }, @@ -105,11 +101,7 @@ "IL": { "flag": "🇮🇱", "name": "Israel", "code": "972" }, "IM": { "flag": "🇮🇲", "name": "Isle of Man", "code": "44-1624" }, "IN": { "flag": "🇮🇳", "name": "India", "code": "91" }, - "IO": { - "flag": "🇮🇴", - "name": "British Indian Ocean Territory", - "code": "246" - }, + "IO": { "flag": "🇮🇴", "name": "British Indian Ocean Territory", "code": "246" }, "IQ": { "flag": "🇮🇶", "name": "Iraq", "code": "964" }, "IR": { "flag": "🇮🇷", "name": "Iran", "code": "98" }, "IS": { "flag": "🇮🇸", "name": "Iceland", "code": "354" }, diff --git a/src/Components/Form/FormFields/PhoneNumberFormField.tsx b/src/Components/Form/FormFields/PhoneNumberFormField.tsx index b6af2bfaa24..ffa91614d2d 100644 --- a/src/Components/Form/FormFields/PhoneNumberFormField.tsx +++ b/src/Components/Form/FormFields/PhoneNumberFormField.tsx @@ -1,6 +1,6 @@ import { FormFieldBaseProps, useFormFieldPropsResolver } from "./Utils"; import FormField from "./FormField"; -import { useEffect, useMemo, useState } from "react"; +import { useCallback, useEffect, useMemo, useState } from "react"; import { classNames, parsePhoneNumber, @@ -27,7 +27,13 @@ interface Props extends FormFieldBaseProps { export default function PhoneNumberFormField(props: Props) { const field = useFormFieldPropsResolver(props as any); - const [error, setError] = useState(); + const [error, setError] = useState(); + const [country, setCountry] = useState({ + flag: "🇮🇳", + name: "India", + code: "91", + }); + const [isOpen, setIsOpen] = useState(false); const validator = useMemo( () => PhoneNumberValidator(props.types), @@ -51,19 +57,44 @@ export default function PhoneNumberFormField(props: Props) { [props.disableValidation] ); - const setValue = (value: string) => { - value = value.replaceAll(/[^0-9+]/g, ""); - if (value.length > 12 && value.startsWith("+910")) { - value = "+91" + value.slice(4); - } + const setValue = useCallback( + (value: string) => { + value = value.replaceAll(/[^0-9+]/g, ""); + if (value.length > 12 && value.startsWith("+910")) { + value = "+91" + value.slice(4); + } - const error = validate(value, "change"); - field.handleChange(value); + const error = validate(value, "change"); + field.handleChange(value); - setError(error); + setError(error); + }, + [field, validate] + ); + + const handleCountryChange = (value: CountryData): void => { + setCountry(value); + setValue(conditionPhoneCode(value.code)); + setIsOpen(false); }; - useEffect(() => setValue(field.value || "+91"), []); + useEffect(() => { + if (field.value && field.value.length > 0) { + if (field.value.startsWith("1800")) { + setCountry({ flag: "📞", name: "Support", code: "1800" }); + return; + } + if (field.value === "+") { + setCountry({ flag: "🌍", name: "Other", code: "+" }); + return; + } + setCountry(phoneCodes[getCountryCode(field.value)!]); + } + }, [setValue]); + + useEffect(() => { + setValue(field.value || "+91"); + }, []); return (
+
setIsOpen(!isOpen)} + > + + {country?.flag ?? "🇮🇳"} + + {isOpen ? ( + + ) : ( + + )} +
+ setError(validate(field.value, "blur"))} /> -
- - -
+ {isOpen && ( + + )}
); @@ -170,3 +189,78 @@ const formatPhoneNumber = (value: string, types: PhoneNumberType[]) => { const phoneNumber = parsePhoneNumber(value); return phoneNumber ? formatPhoneNumberUtil(phoneNumber) : value; }; + +const CountryCodesList = ({ handleCountryChange }: any) => { + const [searchValue, setSearchValue] = useState(""); + + return ( +
+
+ + setSearchValue(e.target.value)} + /> +
+ +
    + {Object.entries(phoneCodes) + .filter(([country, { flag, name, code }]) => { + if (searchValue === "") { + return true; + } + return ( + name.toLowerCase().includes(searchValue.toLowerCase()) || + code.includes(searchValue) || + country.toLowerCase().includes(searchValue.toLowerCase()) || + flag.includes(searchValue) + ); + }) + .map(([country, { flag, name, code }]) => ( +
  • { + handleCountryChange({ flag, name, code }); + }} + > + {flag} + {name} + + {" "} + ({conditionPhoneCode(code)}) + +
  • + ))} +
  • { + handleCountryChange({ flag: "📞", name: "Support", code: "1800" }); + }} + > + 📞 + Support + (1800) +
  • +
  • { + handleCountryChange({ flag: "🌍", name: "Other", code: "+" }); + }} + > + 🌍 + Other + (+) +
  • +
+
+ ); +};