-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathPhoneCountrySelectorModal.tsx
More file actions
144 lines (131 loc) · 4.13 KB
/
Copy pathPhoneCountrySelectorModal.tsx
File metadata and controls
144 lines (131 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import React, { useCallback, useMemo, useRef } from 'react';
import { ListRenderItem } from 'react-native';
import Fuse from 'fuse.js';
import {
BottomSheetRef,
FontWeight,
ListItemSelect,
Text,
TextColor,
TextVariant,
} from '@metamask/design-system-react-native';
import type { Country } from '@metamask/ramps-controller';
import {
createNavigationDetails,
useParams,
} from '../../../../../../util/navigation/navUtils';
import Routes from '../../../../../../constants/navigation/Routes';
import { strings } from '../../../../../../../locales/i18n';
import SearchableSelectorBottomSheet from '../../../components/SearchableSelectorBottomSheet';
const MAX_COUNTRY_RESULTS = 20;
function getCountrySearchResultItem(
result: Country | { item?: Country },
): Country {
return 'item' in result && result.item ? result.item : (result as Country);
}
export interface PhoneCountrySelectorModalParams {
countries: Country[];
selectedCountry?: Country | null;
onCountrySelect: (country: Country) => void;
}
export const createPhoneCountrySelectorModalNavigationDetails =
createNavigationDetails<PhoneCountrySelectorModalParams>(
Routes.RAMP.MODALS.ID,
Routes.RAMP.MODALS.PHONE_COUNTRY_SELECTOR,
);
function PhoneCountrySelectorModal() {
const sheetRef = useRef<BottomSheetRef>(null);
const { countries, selectedCountry, onCountrySelect } =
useParams<PhoneCountrySelectorModalParams>();
const countriesWithPhoneData = useMemo(
() => countries.filter((country) => country.phone),
[countries],
);
const fuseData = useMemo(
() =>
new Fuse(countriesWithPhoneData, {
shouldSort: true,
threshold: 0.2,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: ['name', 'isoCode', 'phone.prefix'],
}),
[countriesWithPhoneData],
);
const getResults = useCallback(
(searchString: string) => {
if (searchString.length > 0) {
return fuseData
.search(searchString)
.slice(0, MAX_COUNTRY_RESULTS)
.map(getCountrySearchResultItem);
}
return [...countriesWithPhoneData].sort((a, b) => {
const aRecommended = a.recommended ?? false;
const bRecommended = b.recommended ?? false;
if (aRecommended && !bRecommended) return -1;
if (!aRecommended && bRecommended) return 1;
return a.name.localeCompare(b.name);
});
},
[fuseData, countriesWithPhoneData],
);
const handleCountryPress = useCallback(
(country: Country) => {
onCountrySelect(country);
sheetRef.current?.onCloseBottomSheet();
},
[onCountrySelect],
);
const renderCountryItem = useCallback<ListRenderItem<Country>>(
({ item }) => (
<ListItemSelect
isSelected={selectedCountry?.isoCode === item.isoCode}
onPress={() => handleCountryPress(item)}
accessibilityRole="button"
accessible
startAccessory={
item.flag ? (
<Text
variant={TextVariant.BodyLg}
fontWeight={FontWeight.Medium}
color={TextColor.TextDefault}
>
{item.flag}
</Text>
) : undefined
}
title={item.name}
titleProps={{ variant: TextVariant.BodyLg }}
description={item.phone?.prefix}
descriptionProps={{
variant: TextVariant.BodyMd,
fontWeight: FontWeight.Regular,
color: TextColor.TextMuted,
}}
/>
),
[handleCountryPress, selectedCountry?.isoCode],
);
return (
<SearchableSelectorBottomSheet<Country>
sheetRef={sheetRef}
title={strings('deposit.phone_country_modal.select_phone_country')}
searchPlaceholder={strings(
'deposit.phone_country_modal.search_by_country',
)}
noResultsText={(searchString) =>
strings('deposit.phone_country_modal.no_countries_found', {
searchString,
})
}
getResults={getResults}
renderItem={renderCountryItem}
keyExtractor={(item) => item.isoCode}
extraData={selectedCountry?.isoCode}
/>
);
}
export default PhoneCountrySelectorModal;