Merge pull request 'fix(profile): add country search bar with dial codes and clear phone on country change' (#135) from fix/edit-profile-country-picker into main

Reviewed-on: https://git.zedobject.local/AcuPedia/acupedia-mobile-ui/pulls/135
Reviewed-by: Amit Salunke <amit@noreply.localhost>
This commit is contained in:
Amit Salunke 2026-11-27 04:48:37 +00:00
commit e153668f06
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 { import {
Modal, Modal,
Pressable, Pressable,
ScrollView, ScrollView,
StyleSheet, StyleSheet,
Text, Text,
TextInput,
View, View,
} from "react-native"; } from "react-native";
import { Ionicons } from "@expo/vector-icons"; import { Ionicons } from "@expo/vector-icons";
@ -20,8 +21,8 @@ import FieldLabel from "@/components/forms/FieldLabel";
import PrimaryButton from "@/components/buttons/PrimaryButton"; import PrimaryButton from "@/components/buttons/PrimaryButton";
import { Colors } from "@/theme"; import { Colors } from "@/theme";
import { import {
SORTED_COUNTRY_CODES,
getCountryRuleByName, getCountryRuleByName,
updatePhoneDialCode,
validatePhoneNumber, validatePhoneNumber,
isValidPhoneNumber, isValidPhoneNumber,
parsePhoneNumber, parsePhoneNumber,
@ -38,25 +39,6 @@ interface EditProfileFormProps {
isLoading?: boolean; 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({ export default function EditProfileForm({
initialName = "User Name", initialName = "User Name",
initialEmail = "user@example.com", initialEmail = "user@example.com",
@ -71,9 +53,21 @@ export default function EditProfileForm({
const [nameError, setNameError] = useState(""); const [nameError, setNameError] = useState("");
const [phoneError, setPhoneError] = useState(""); const [phoneError, setPhoneError] = useState("");
const [showCountryModal, setShowCountryModal] = useState(false); const [showCountryModal, setShowCountryModal] = useState(false);
const [countrySearch, setCountrySearch] = useState("");
const activeRule = getCountryRuleByName(country); 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(() => { useEffect(() => {
setFullName(initialName); setFullName(initialName);
}, [initialName]); }, [initialName]);
@ -105,12 +99,12 @@ export default function EditProfileForm({
const handleCountrySelect = (newCountry: string) => { const handleCountrySelect = (newCountry: string) => {
if (newCountry !== country) { if (newCountry !== country) {
const updatedPhone = updatePhoneDialCode(phone, country, newCountry); setPhone("");
setPhone(updatedPhone);
setCountry(newCountry); setCountry(newCountry);
if (phoneError) setPhoneError(""); if (phoneError) setPhoneError("");
} }
setShowCountryModal(false); setShowCountryModal(false);
setCountrySearch("");
}; };
const handleSave = () => { const handleSave = () => {
@ -220,18 +214,27 @@ export default function EditProfileForm({
visible={showCountryModal} visible={showCountryModal}
transparent transparent
animationType="slide" animationType="slide"
onRequestClose={() => setShowCountryModal(false)} onRequestClose={() => {
setShowCountryModal(false);
setCountrySearch("");
}}
> >
<Pressable <Pressable
style={styles.modalOverlay} style={styles.modalOverlay}
onPress={() => setShowCountryModal(false)} onPress={() => {
setShowCountryModal(false);
setCountrySearch("");
}}
> >
<View style={styles.modalSheet}> <Pressable style={styles.modalSheet} onPress={() => {}}>
<View style={styles.modalHeader}> <View style={styles.modalHeader}>
<Text style={styles.modalTitle}>Select Country</Text> <Text style={styles.modalTitle}>Select Country</Text>
<Pressable <Pressable
hitSlop={8} hitSlop={8}
onPress={() => setShowCountryModal(false)} onPress={() => {
setShowCountryModal(false);
setCountrySearch("");
}}
> >
<Ionicons <Ionicons
name="close" name="close"
@ -241,42 +244,82 @@ export default function EditProfileForm({
</Pressable> </Pressable>
</View> </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 <ScrollView
showsVerticalScrollIndicator={false} showsVerticalScrollIndicator={false}
contentContainerStyle={styles.countryList} contentContainerStyle={styles.countryList}
keyboardShouldPersistTaps="handled"
> >
{COMMON_COUNTRIES.map((c) => { {filteredCountries.length === 0 ? (
const isSelected = country === c; <View style={styles.emptyContainer}>
return ( <Text style={styles.emptyText}>No countries found</Text>
<Pressable </View>
key={c} ) : (
style={({ pressed }) => [ filteredCountries.map((c) => {
styles.countryItem, const isSelected = country === c.country;
isSelected && styles.countryItemSelected, return (
pressed && styles.countryItemPressed, <Pressable
]} key={c.iso2 + c.country}
onPress={() => handleCountrySelect(c)} style={({ pressed }) => [
> styles.countryItem,
<Text isSelected && styles.countryItemSelected,
style={[ pressed && styles.countryItemPressed,
styles.countryName,
isSelected && styles.countryNameSelected,
]} ]}
onPress={() => handleCountrySelect(c.country)}
> >
{c} <View style={styles.countryRow}>
</Text> <Text style={styles.countryDialCode}>{c.code}</Text>
{isSelected && ( <Text
<Ionicons style={[
name="checkmark" styles.countryName,
size={moderateScale(18)} isSelected && styles.countryNameSelected,
color={Colors.primary} ]}
/> >
)} {c.country}
</Pressable> </Text>
); </View>
})} {isSelected && (
<Ionicons
name="checkmark"
size={moderateScale(18)}
color={Colors.primary}
/>
)}
</Pressable>
);
})
)}
</ScrollView> </ScrollView>
</View> </Pressable>
</Pressable> </Pressable>
</Modal> </Modal>
</View> </View>
@ -334,6 +377,52 @@ const styles = StyleSheet.create({
color: Colors.textPrimary, 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: { countryList: {
paddingHorizontal: scale(16), paddingHorizontal: scale(16),
paddingTop: verticalScale(8), paddingTop: verticalScale(8),