fix(profile): add country search bar with dial codes and clear phone on country change #135

Merged
amit merged 1 commits from fix/edit-profile-country-picker into main 2026-11-27 04:48:38 +00:00
1 changed files with 145 additions and 56 deletions

View File

@ -1,10 +1,11 @@
import React, { useState, useEffect } from "react";
import React, { useState, useEffect, useMemo } from "react";
import {
Modal,
Pressable,
ScrollView,
StyleSheet,
Text,
TextInput,
View,
} from "react-native";
import { Ionicons } from "@expo/vector-icons";
@ -20,8 +21,8 @@ import FieldLabel from "@/components/forms/FieldLabel";
import PrimaryButton from "@/components/buttons/PrimaryButton";
import { Colors } from "@/theme";
import {
SORTED_COUNTRY_CODES,
getCountryRuleByName,
updatePhoneDialCode,
validatePhoneNumber,
isValidPhoneNumber,
parsePhoneNumber,
@ -38,25 +39,6 @@ interface EditProfileFormProps {
isLoading?: boolean;
}
const COMMON_COUNTRIES = [
"Australia",
"Canada",
"France",
"Germany",
"India",
"Ireland",
"Malaysia",
"Netherlands",
"New Zealand",
"Singapore",
"South Africa",
"Spain",
"Switzerland",
"United Arab Emirates",
"United Kingdom",
"United States",
];
export default function EditProfileForm({
initialName = "User Name",
initialEmail = "user@example.com",
@ -71,9 +53,21 @@ export default function EditProfileForm({
const [nameError, setNameError] = useState("");
const [phoneError, setPhoneError] = useState("");
const [showCountryModal, setShowCountryModal] = useState(false);
const [countrySearch, setCountrySearch] = useState("");
const activeRule = getCountryRuleByName(country);
const filteredCountries = useMemo(() => {
const q = countrySearch.trim().toLowerCase();
if (!q) return SORTED_COUNTRY_CODES;
return SORTED_COUNTRY_CODES.filter(
(c) =>
c.country.toLowerCase().includes(q) ||
c.code.toLowerCase().includes(q) ||
c.label.toLowerCase().includes(q)
);
}, [countrySearch]);
useEffect(() => {
setFullName(initialName);
}, [initialName]);
@ -105,12 +99,12 @@ export default function EditProfileForm({
const handleCountrySelect = (newCountry: string) => {
if (newCountry !== country) {
const updatedPhone = updatePhoneDialCode(phone, country, newCountry);
setPhone(updatedPhone);
setPhone("");
setCountry(newCountry);
if (phoneError) setPhoneError("");
}
setShowCountryModal(false);
setCountrySearch("");
};
const handleSave = () => {
@ -220,18 +214,27 @@ export default function EditProfileForm({
visible={showCountryModal}
transparent
animationType="slide"
onRequestClose={() => setShowCountryModal(false)}
onRequestClose={() => {
setShowCountryModal(false);
setCountrySearch("");
}}
>
<Pressable
style={styles.modalOverlay}
onPress={() => setShowCountryModal(false)}
onPress={() => {
setShowCountryModal(false);
setCountrySearch("");
}}
>
<View style={styles.modalSheet}>
<Pressable style={styles.modalSheet} onPress={() => {}}>
<View style={styles.modalHeader}>
<Text style={styles.modalTitle}>Select Country</Text>
<Pressable
hitSlop={8}
onPress={() => setShowCountryModal(false)}
onPress={() => {
setShowCountryModal(false);
setCountrySearch("");
}}
>
<Ionicons
name="close"
@ -241,42 +244,82 @@ export default function EditProfileForm({
</Pressable>
</View>
{/* Country Search Bar */}
<View style={styles.modalSearchContainer}>
<Ionicons
name="search-outline"
size={moderateScale(17)}
color={Colors.textSecondary}
/>
<TextInput
value={countrySearch}
onChangeText={setCountrySearch}
placeholder="Search country or code..."
placeholderTextColor={Colors.textSecondary}
style={styles.modalSearchInput}
autoCapitalize="none"
autoCorrect={false}
/>
{countrySearch.length > 0 && (
<Pressable
hitSlop={8}
onPress={() => setCountrySearch("")}
>
<Ionicons
name="close-circle"
size={moderateScale(17)}
color={Colors.textSecondary}
/>
</Pressable>
)}
</View>
<ScrollView
showsVerticalScrollIndicator={false}
contentContainerStyle={styles.countryList}
keyboardShouldPersistTaps="handled"
>
{COMMON_COUNTRIES.map((c) => {
const isSelected = country === c;
return (
<Pressable
key={c}
style={({ pressed }) => [
styles.countryItem,
isSelected && styles.countryItemSelected,
pressed && styles.countryItemPressed,
]}
onPress={() => handleCountrySelect(c)}
>
<Text
style={[
styles.countryName,
isSelected && styles.countryNameSelected,
{filteredCountries.length === 0 ? (
<View style={styles.emptyContainer}>
<Text style={styles.emptyText}>No countries found</Text>
</View>
) : (
filteredCountries.map((c) => {
const isSelected = country === c.country;
return (
<Pressable
key={c.iso2 + c.country}
style={({ pressed }) => [
styles.countryItem,
isSelected && styles.countryItemSelected,
pressed && styles.countryItemPressed,
]}
onPress={() => handleCountrySelect(c.country)}
>
{c}
</Text>
{isSelected && (
<Ionicons
name="checkmark"
size={moderateScale(18)}
color={Colors.primary}
/>
)}
</Pressable>
);
})}
<View style={styles.countryRow}>
<Text style={styles.countryDialCode}>{c.code}</Text>
<Text
style={[
styles.countryName,
isSelected && styles.countryNameSelected,
]}
>
{c.country}
</Text>
</View>
{isSelected && (
<Ionicons
name="checkmark"
size={moderateScale(18)}
color={Colors.primary}
/>
)}
</Pressable>
);
})
)}
</ScrollView>
</View>
</Pressable>
</Pressable>
</Modal>
</View>
@ -334,6 +377,52 @@ const styles = StyleSheet.create({
color: Colors.textPrimary,
},
modalSearchContainer: {
flexDirection: "row",
alignItems: "center",
marginHorizontal: scale(16),
marginTop: verticalScale(10),
marginBottom: verticalScale(4),
paddingHorizontal: scale(12),
paddingVertical: verticalScale(8),
backgroundColor: "#F8FAFC",
borderRadius: moderateScale(10),
borderWidth: 1,
borderColor: "#E2E8F0",
gap: scale(8),
},
modalSearchInput: {
flex: 1,
fontSize: moderateScale(14),
color: Colors.textPrimary,
padding: 0,
},
emptyContainer: {
paddingVertical: verticalScale(24),
alignItems: "center",
},
emptyText: {
fontSize: moderateScale(14),
color: Colors.textSecondary,
},
countryRow: {
flexDirection: "row",
alignItems: "center",
gap: scale(10),
flex: 1,
},
countryDialCode: {
fontSize: moderateScale(13),
fontWeight: "700",
color: Colors.textSecondary,
minWidth: scale(46),
},
countryList: {
paddingHorizontal: scale(16),
paddingTop: verticalScale(8),