Skip to content

Commit 277eda9

Browse files
Implement distinct option for countries list (GH-67)
2 parents 549be87 + 5434b16 commit 277eda9

File tree

12 files changed

+26
-6
lines changed

12 files changed

+26
-6
lines changed

development/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"antd": "^5.8.3",
1414
"react": "^18.2.0",
1515
"react-dom": "^18.2.0",
16-
"react-phone-hooks": "0.1.0-beta.1",
16+
"react-phone-hooks": "^0.1.12",
1717
"react-scripts": "^5.0.1",
1818
"typescript": "^4.9.5"
1919
},

development/src/ant-phone/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {PhoneInputProps, PhoneNumber} from "./types";
3939
const PhoneInput = forwardRef(({
4040
value: initialValue = "",
4141
country = getDefaultISO2Code(),
42+
distinct = false,
4243
disabled = false,
4344
enableArrow = false,
4445
enableSearch = false,
@@ -86,6 +87,7 @@ const PhoneInput = forwardRef(({
8687
} = usePhone({
8788
query,
8889
country,
90+
distinct,
8991
countryCode,
9092
initialValue,
9193
onlyCountries,

development/src/ant-phone/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export interface PhoneInputProps extends Omit<InputProps, "value" | "onChange">
1111

1212
country?: string;
1313

14+
distinct?: boolean;
15+
1416
enableArrow?: boolean;
1517

1618
enableSearch?: boolean;

development/src/mui-phone/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const PhoneInput = forwardRef(({
2727
variant = undefined,
2828
searchVariant = undefined,
2929
country = getDefaultISO2Code(),
30+
distinct = false,
3031
disabled = false,
3132
enableArrow = false,
3233
enableSearch = false,
@@ -69,6 +70,7 @@ const PhoneInput = forwardRef(({
6970
query,
7071
locale,
7172
country,
73+
distinct,
7274
countryCode,
7375
initialValue,
7476
onlyCountries,

development/src/mui-phone/joy/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const PhoneInput = forwardRef(({
2727
variant = undefined,
2828
searchVariant = undefined,
2929
country = getDefaultISO2Code(),
30+
distinct = false,
3031
disabled = false,
3132
enableArrow = false,
3233
enableSearch = false,
@@ -69,6 +70,7 @@ const PhoneInput = forwardRef(({
6970
query,
7071
locale,
7172
country,
73+
distinct,
7274
countryCode,
7375
initialValue,
7476
onlyCountries,

development/src/mui-phone/joy/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export interface PhoneInputProps extends Omit<InputProps, "value" | "onChange">
1313

1414
country?: string;
1515

16+
distinct?: boolean;
17+
1618
enableArrow?: boolean;
1719

1820
enableSearch?: boolean;

development/src/mui-phone/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export interface PhoneInputProps extends Omit<TextFieldProps, "onChange"> {
1313

1414
country?: string;
1515

16+
distinct?: boolean;
17+
1618
enableArrow?: boolean;
1719

1820
enableSearch?: boolean;

development/src/phone-hooks/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export const usePhone = ({
116116
query = "",
117117
locale = "",
118118
country = "",
119+
distinct = false,
119120
countryCode = "",
120121
initialValue = "",
121122
onlyCountries = [],
@@ -143,11 +144,13 @@ export const usePhone = ({
143144
const localized = countries && (countries[name] || "").toLowerCase();
144145
return [localized, name.toLowerCase(), dial, mask].some(component => component.includes(q));
145146
});
146-
return [
147+
const seen = new Set();
148+
const whitelistCountries = [
147149
...filteredCountries.filter(([iso]) => preferredCountries.includes(iso)),
148150
...filteredCountries.filter(([iso]) => !preferredCountries.includes(iso)),
149151
];
150-
}, [countriesOnly, preferredCountries, locale, query])
152+
return whitelistCountries.filter(([iso]) => !seen.has(iso) && seen.add(iso));
153+
}, [countriesOnly, preferredCountries, distinct, locale, query])
151154

152155
const metadata = useMemo(() => {
153156
const calculatedMetadata = getMetadata(getRawValue(value), countriesList, countryCode);

development/src/phone-hooks/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface usePhoneOptions {
1313
query?: string;
1414
locale?: string;
1515
country?: string;
16+
distinct?: boolean;
1617
countryCode?: string;
1718
onlyCountries?: string[];
1819
excludeCountries?: string[];

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.1.12",
2+
"version": "0.1.13",
33
"name": "react-phone-hooks",
44
"description": "React hooks and utility functions for parsing and validating phone numbers.",
55
"keywords": [

src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export const usePhone = ({
116116
query = "",
117117
locale = "",
118118
country = "",
119+
distinct = false,
119120
countryCode = "",
120121
initialValue = "",
121122
onlyCountries = [],
@@ -143,11 +144,13 @@ export const usePhone = ({
143144
const localized = countries && (countries[name] || "").toLowerCase();
144145
return [localized, name.toLowerCase(), dial, mask].some(component => component.includes(q));
145146
});
146-
return [
147+
const seen = new Set();
148+
const whitelistCountries = [
147149
...filteredCountries.filter(([iso]) => preferredCountries.includes(iso)),
148150
...filteredCountries.filter(([iso]) => !preferredCountries.includes(iso)),
149151
];
150-
}, [countriesOnly, preferredCountries, locale, query])
152+
return whitelistCountries.filter(([iso]) => !seen.has(iso) && seen.add(iso));
153+
}, [countriesOnly, preferredCountries, distinct, locale, query])
151154

152155
const metadata = useMemo(() => {
153156
const calculatedMetadata = getMetadata(getRawValue(value), countriesList, countryCode);

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface usePhoneOptions {
1313
query?: string;
1414
locale?: string;
1515
country?: string;
16+
distinct?: boolean;
1617
countryCode?: string;
1718
onlyCountries?: string[];
1819
excludeCountries?: string[];

0 commit comments

Comments
 (0)