fix: country code selector

This commit is contained in:
Koosha Lahouti
2025-09-25 13:17:48 +03:30
parent 5dc2ee585b
commit 4884a5942f
2 changed files with 20 additions and 14 deletions

View File

@@ -76,10 +76,10 @@ export function CountryCodeSelector({
[searchTerm, t], [searchTerm, t],
); );
const initialIndex = Math.max( const initialIndex = useMemo(() => {
0, const index = filteredCountries.findIndex((c) => c.phone === value);
filteredCountries.findIndex((c) => c.phone === value), return Math.max(0, index);
); }, [filteredCountries, value]);
return ( return (
<InputAdornment <InputAdornment
@@ -97,7 +97,7 @@ export function CountryCodeSelector({
}), }),
overflow: 'hidden', overflow: 'hidden',
whiteSpace: 'nowrap', whiteSpace: 'nowrap',
height: '100%', height: '200%',
display: 'flex', display: 'flex',
alignItems: 'center', alignItems: 'center',
gap: 0.25, gap: 0.25,
@@ -132,11 +132,11 @@ export function CountryCodeSelector({
open={open} open={open}
onClose={handleClose} onClose={handleClose}
slotProps={{ slotProps={{
list: { paper: {
sx: { sx: {
width: menuWidth, width: menuWidth,
height: '18.75rem', maxHeight: '20rem',
p: 0, overflow: 'hidden',
}, },
}, },
transition: { onEntered: handleMenuEntered }, transition: { onEntered: handleMenuEntered },
@@ -163,8 +163,7 @@ export function CountryCodeSelector({
/> />
</Box> </Box>
{/* Virtualized country list */} <Box sx={{ width: '100%', height: '16rem' }}>
<Box sx={{ width: '100%', height: '14.75rem' }}>
{filteredCountries.length === 0 ? ( {filteredCountries.length === 0 ? (
<MenuItem disabled> <MenuItem disabled>
<ListItem> <ListItem>
@@ -177,12 +176,18 @@ export function CountryCodeSelector({
<Virtuoso <Virtuoso
style={{ height: '100%', width: '100%' }} style={{ height: '100%', width: '100%' }}
data={filteredCountries} data={filteredCountries}
initialTopMostItemIndex={initialIndex} initialTopMostItemIndex={searchTerm ? 0 : initialIndex}
itemContent={(_, country) => ( itemContent={(_, country) => (
<MenuItem <MenuItem
key={country.code} key={country.code}
selected={country.phone === value} selected={country.phone === value}
onClick={() => handleSelect(country)} onClick={() => handleSelect(country)}
sx={{
minHeight: '48px',
'&.Mui-selected': {
backgroundColor: 'action.selected',
},
}}
> >
<ListItemIcon> <ListItemIcon>
<ReactCountryFlag <ReactCountryFlag
@@ -199,7 +204,7 @@ export function CountryCodeSelector({
</Typography> </Typography>
</MenuItem> </MenuItem>
)} )}
computeItemKey={(_, c) => c.code} computeItemKey={(_, country) => country.code}
/> />
)} )}
</Box> </Box>

View File

@@ -1,4 +1,5 @@
import type { TFunction } from 'i18next'; import type { TFunction } from 'i18next';
import type { CountryCode } from '@/types/commonTypes';
export enum Gender { export enum Gender {
Male = 2, Male = 2,
@@ -107,8 +108,8 @@ export interface PhoneDisplayProps {
export interface PhoneEditFormProps { export interface PhoneEditFormProps {
phoneNumber: string; phoneNumber: string;
setPhoneNumber: (v: string) => void; setPhoneNumber: (v: string) => void;
countryCode: string; countryCode: CountryCode;
setCountryCode: (v: string) => void; setCountryCode: (v: CountryCode) => void;
verificationCode: string; verificationCode: string;
setVerificationCode: (v: string) => void; setVerificationCode: (v: string) => void;
isVerified: boolean; isVerified: boolean;