From 5b5bee72e13fa2c5177e20d91879c4b04378e9f9 Mon Sep 17 00:00:00 2001 From: ShivaniKhaire Date: Thu, 15 Oct 2026 16:43:17 +0530 Subject: [PATCH] issue 44: dynamic country and phone code selection --- package-lock.json | 7 + package.json | 1 + src/components/forms/BaseTextInput.tsx | 8 +- src/features/auth/components/SignUpForm.tsx | 283 ++++++++++++++++++-- src/utils/phone.ts | 65 +++++ 5 files changed, 344 insertions(+), 20 deletions(-) create mode 100644 src/utils/phone.ts diff --git a/package-lock.json b/package-lock.json index 591990c..77fd717 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32,6 +32,7 @@ "expo-symbols": "~1.0.7", "expo-system-ui": "~6.0.9", "expo-web-browser": "~15.0.11", + "libphonenumber-js": "^1.13.15", "react": "19.1.0", "react-dom": "19.1.0", "react-native": "0.81.5", @@ -6185,6 +6186,12 @@ "node": ">=6" } }, + "node_modules/libphonenumber-js": { + "version": "1.13.15", + "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.13.15.tgz", + "integrity": "sha512-AHdhEQeDFEJ4GKDAQ9AwxChy+v2kUILUB3TGQRpkXMkWePjXTU/WQEzWVQoIx/Gz8fE73Wr5SEuZCy2vk8jrMQ==", + "license": "MIT" + }, "node_modules/lighthouse-logger": { "version": "1.4.2", "resolved": "https://registry.npmjs.org/lighthouse-logger/-/lighthouse-logger-1.4.2.tgz", diff --git a/package.json b/package.json index f89215f..df00c28 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "expo-symbols": "~1.0.7", "expo-system-ui": "~6.0.9", "expo-web-browser": "~15.0.11", + "libphonenumber-js": "^1.13.15", "react": "19.1.0", "react-dom": "19.1.0", "react-native": "0.81.5", diff --git a/src/components/forms/BaseTextInput.tsx b/src/components/forms/BaseTextInput.tsx index 4805a96..e585c13 100644 --- a/src/components/forms/BaseTextInput.tsx +++ b/src/components/forms/BaseTextInput.tsx @@ -19,6 +19,7 @@ interface BaseTextInputProps extends TextInputProps { label?: string; error?: string; leftIcon?: keyof typeof Ionicons.glyphMap; + leftElement?: React.ReactNode; rightElement?: React.ReactNode; } @@ -28,6 +29,7 @@ const BaseTextInput = forwardRef( label, error, leftIcon, + leftElement, rightElement, style, ...props @@ -48,14 +50,16 @@ const BaseTextInput = forwardRef( error && styles.errorBorder, ]} > - {leftIcon && ( + {leftElement ? ( + leftElement + ) : leftIcon ? ( - )} + ) : null} state.setRegisteredUser); + // ITU डेटाबेसमधून सर्व देश (Zero Hardcoding) + const countries = useMemo(() => getAllCountries(), []); + const [fullName, setFullName] = useState(""); const [email, setEmail] = useState(""); + const [selectedCountry, setSelectedCountry] = useState(getDefaultCountry); const [phone, setPhone] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); + const [showCountryModal, setShowCountryModal] = useState(false); + const [searchQuery, setSearchQuery] = useState(""); + const [fullNameError, setFullNameError] = useState(""); const [emailError, setEmailError] = useState(""); const [phoneError, setPhoneError] = useState(""); const [passwordError, setPasswordError] = useState(""); const [confirmPasswordError, setConfirmPasswordError] = useState(""); + // युझर फक्त आकडे टाईप करेल (+91 नाही) const handlePhoneChange = (text: string) => { const digits = text.replace(/\D/g, ""); - setPhone(digits.slice(0, 10)); + setPhone(digits.slice(0, 15)); + if (phoneError) setPhoneError(""); }; + // सर्च बार फिल्टर + const filteredCountries = useMemo(() => { + if (!searchQuery.trim()) return countries; + const query = searchQuery.toLowerCase().trim(); + return countries.filter( + (c) => + c.name.toLowerCase().includes(query) || + c.dialCode.includes(query) || + c.iso.toLowerCase().includes(query) + ); + }, [countries, searchQuery]); + const handleSignUp = async () => { let hasError = false; setFullNameError(""); @@ -52,9 +94,13 @@ export default function SignUpForm() { hasError = true; } - if (phone.trim() && phone.length < 10) { - setPhoneError("Phone number must be 10 digits"); - hasError = true; + // ITU लायब्ररीद्वारे डायनॅमिक फोन व्हॅलिडेशन + if (phone.trim()) { + const isValid = validatePhoneByCountry(phone.trim(), selectedCountry.iso); + if (!isValid) { + setPhoneError(`Please enter a valid ${selectedCountry.name} phone number`); + hasError = true; + } } if (!password) { @@ -87,23 +133,29 @@ export default function SignUpForm() { if (hasError) return; + // पूर्ण आंतरराष्ट्रीय नंबर तयार करणे + const fullPhoneNumber = phone.trim() + ? `${selectedCountry.dialCode} ${phone.trim()}` + : undefined; + try { const response = await register({ email: email.trim().toLowerCase(), password, full_name: fullName.trim(), - phone_number: phone.trim() ? phone.trim() : undefined, + phone_number: fullPhoneNumber, + country: selectedCountry.name, // DB मध्ये सेव्ह होणारा देश }); const { user } = response; - setRegisteredUser({ id: user.id, - name: user.display_name, + name: user.display_name || fullName.trim(), email: user.email, + phone_number: user.phone_number || undefined, + country: user.country || selectedCountry.name, }); - // Redirect directly to login screen without alert block router.replace("/login"); } catch (err: any) { let errMsg = "Something went wrong. Please try again."; @@ -141,17 +193,35 @@ export default function SignUpForm() { error={emailError} /> + {/* Direct Phone Number Input with In-Line Country Prefix Badge */} setShowCountryModal(true)} + accessibilityRole="button" + accessibilityLabel="Select country code" + > + {selectedCountry.flag} + {selectedCountry.dialCode} + + + + } /> - + + + {/* Searchable Country Picker Modal */} + setShowCountryModal(false)} + > + setShowCountryModal(false)} + > + true}> + + Select Country + setShowCountryModal(false)}> + + + + + {/* २४०+ देशांमधून शोधण्यासाठी Search Box */} + + + + {searchQuery ? ( + setSearchQuery("")}> + + + ) : null} + + + + {filteredCountries.map((item) => { + const isSelected = selectedCountry.iso === item.iso; + return ( + [ + styles.countryItem, + isSelected && styles.countryItemSelected, + pressed && styles.countryItemPressed, + ]} + onPress={() => { + setSelectedCountry(item); + setShowCountryModal(false); + setSearchQuery(""); + }} + > + + {item.flag} {item.name} + + + {item.dialCode} + {isSelected && ( + + )} + + + ); + })} + + + + ); } @@ -185,4 +323,113 @@ const styles = StyleSheet.create({ buttonSpacing: { marginTop: verticalScale(10), }, + + // Country Prefix Badge + countryBadge: { + flexDirection: "row", + alignItems: "center", + paddingRight: scale(8), + }, + countryBadgeFlag: { + fontSize: moderateScale(16), + marginRight: scale(4), + }, + countryBadgeCode: { + fontSize: moderateScale(14), + fontWeight: "600", + color: AuthColors.textPrimary, + }, + countryBadgeArrow: { + marginLeft: scale(3), + }, + divider: { + width: 1, + height: verticalScale(20), + backgroundColor: AuthColors.border, + marginLeft: scale(8), + }, + + // Modal Styles + modalOverlay: { + flex: 1, + backgroundColor: "rgba(0, 0, 0, 0.4)", + justifyContent: "flex-end", + }, + modalSheet: { + backgroundColor: "#FFFFFF", + borderTopLeftRadius: moderateScale(20), + borderTopRightRadius: moderateScale(20), + maxHeight: "75%", + paddingTop: verticalScale(16), + paddingBottom: verticalScale(32), + }, + modalHeader: { + flexDirection: "row", + alignItems: "center", + justifyContent: "space-between", + paddingHorizontal: scale(20), + paddingBottom: verticalScale(12), + borderBottomWidth: 1, + borderBottomColor: "#F1F5F9", + }, + modalTitle: { + fontSize: moderateScale(17), + fontWeight: "700", + color: Colors.textPrimary, + }, + searchBox: { + flexDirection: "row", + alignItems: "center", + backgroundColor: AuthColors.inputBackground, + marginHorizontal: scale(16), + marginVertical: verticalScale(10), + paddingHorizontal: scale(12), + borderRadius: moderateScale(12), + height: verticalScale(42), + borderWidth: 1, + borderColor: AuthColors.border, + }, + searchInput: { + flex: 1, + marginLeft: scale(8), + fontSize: moderateScale(14), + color: AuthColors.textPrimary, + }, + countryList: { + paddingHorizontal: scale(16), + paddingTop: verticalScale(4), + }, + countryItem: { + flexDirection: "row", + alignItems: "center", + justifyContent: "space-between", + paddingVertical: verticalScale(12), + paddingHorizontal: scale(12), + borderRadius: moderateScale(10), + }, + countryItemSelected: { + backgroundColor: "#DCFCE7", + }, + countryItemPressed: { + backgroundColor: "#F1F5F9", + }, + countryName: { + fontSize: moderateScale(14), + color: Colors.textPrimary, + fontWeight: "500", + }, + countryNameSelected: { + fontWeight: "700", + color: Colors.primaryDark, + }, + codeRow: { + flexDirection: "row", + alignItems: "center", + gap: scale(8), + }, + countryCodeText: { + fontSize: moderateScale(13), + color: Colors.textSecondary, + fontWeight: "600", + }, }); diff --git a/src/utils/phone.ts b/src/utils/phone.ts new file mode 100644 index 0000000..d96cbf5 --- /dev/null +++ b/src/utils/phone.ts @@ -0,0 +1,65 @@ +import { + CountryCode, + getCountries, + getCountryCallingCode, + isValidPhoneNumber as checkValidPhone, +} from "libphonenumber-js"; + +export interface CountryInfo { + iso: CountryCode; + name: string; + dialCode: string; + flag: string; +} + +const displayNames = new Intl.DisplayNames(["en"], { type: "region" }); + +export const getFlagEmoji = (iso: string): string => { + const upper = iso.toUpperCase(); + if (upper.length !== 2) return "🌐"; + return String.fromCodePoint( + 127397 + upper.charCodeAt(0), + 127397 + upper.charCodeAt(1) + ); +}; + +export const getAllCountries = (): CountryInfo[] => { + return getCountries() + .map((iso) => { + let dialCode = ""; + try { + dialCode = `+${getCountryCallingCode(iso)}`; + } catch { + dialCode = ""; + } + return { + iso, + name: displayNames.of(iso) || iso, + dialCode, + flag: getFlagEmoji(iso), + }; + }) + .filter((c) => c.dialCode !== "") + .sort((a, b) => a.name.localeCompare(b.name)); +}; + +export const getDefaultCountry = (): CountryInfo => { + return { + iso: "IN", + name: "India", + dialCode: "+91", + flag: getFlagEmoji("IN"), + }; +}; + +export const validatePhoneByCountry = ( + phoneNumber: string, + countryIso: CountryCode +): boolean => { + if (!phoneNumber || !phoneNumber.trim()) return true; + try { + return checkValidPhone(phoneNumber, countryIso); + } catch { + return false; + } +};