Skip to content

Commit

Permalink
Implement advanced country filtering (GH-39)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtyomVancyan authored Jul 4, 2024
2 parents 9893ecd + a1ff3e5 commit 4037245
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
1 change: 0 additions & 1 deletion development/src/AntDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const AntDemo = () => {
<Form>
<FormItem name="phone" rules={[{validator}]}>
<PhoneInput
enableArrow
enableSearch
onChange={(e) => setValue(e as any)}
/>
Expand Down
1 change: 0 additions & 1 deletion development/src/MuiDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const Demo = () => {
)}
<form noValidate autoComplete="off" onSubmit={e => e.preventDefault()}>
<PhoneInput
enableArrow
enableSearch
error={error}
variant="filled"
Expand Down
12 changes: 5 additions & 7 deletions development/src/phone-hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,15 @@ export const usePhone = ({

const countriesOnly = useMemo(() => {
const allowList = onlyCountries.length > 0 ? onlyCountries : countries.map(([iso]) => iso);
return countries.map(([iso]) => iso).filter((iso) => {
return allowList.includes(iso) && !excludeCountries.includes(iso);
return countries.filter(([iso, _1, dial]) => {
return (allowList.includes(iso) || allowList.includes(dial)) && !excludeCountries.includes(iso) && !excludeCountries.includes(dial);
});
}, [onlyCountries, excludeCountries])

const countriesList = useMemo(() => {
const filteredCountries = countries.filter(([iso, name, _1, dial]) => {
return countriesOnly.includes(iso) && (
name.toLowerCase().startsWith(query.toLowerCase()) || dial.includes(query)
);
});
const filteredCountries = countriesOnly.filter(([_1, name, dial, mask]) => (
name.toLowerCase().startsWith(query.toLowerCase()) || dial.includes(query) || mask.includes(query)
));
return [
...filteredCountries.filter(([iso]) => preferredCountries.includes(iso)),
...filteredCountries.filter(([iso]) => !preferredCountries.includes(iso)),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.6",
"version": "0.1.7",
"name": "react-phone-hooks",
"description": "React hooks and utility functions for parsing and validating phone numbers.",
"keywords": [
Expand Down
12 changes: 5 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,15 @@ export const usePhone = ({

const countriesOnly = useMemo(() => {
const allowList = onlyCountries.length > 0 ? onlyCountries : countries.map(([iso]) => iso);
return countries.map(([iso]) => iso).filter((iso) => {
return allowList.includes(iso) && !excludeCountries.includes(iso);
return countries.filter(([iso, _1, dial]) => {
return (allowList.includes(iso) || allowList.includes(dial)) && !excludeCountries.includes(iso) && !excludeCountries.includes(dial);
});
}, [onlyCountries, excludeCountries])

const countriesList = useMemo(() => {
const filteredCountries = countries.filter(([iso, name, _1, dial]) => {
return countriesOnly.includes(iso) && (
name.toLowerCase().startsWith(query.toLowerCase()) || dial.includes(query)
);
});
const filteredCountries = countriesOnly.filter(([_1, name, dial, mask]) => (
name.toLowerCase().startsWith(query.toLowerCase()) || dial.includes(query) || mask.includes(query)
));
return [
...filteredCountries.filter(([iso]) => preferredCountries.includes(iso)),
...filteredCountries.filter(([iso]) => !preferredCountries.includes(iso)),
Expand Down
17 changes: 17 additions & 0 deletions tests/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ describe("Verifying the functionality of hooks", () => {
expect((result.current.metadata as any)[0]).toBe("am");
})

it("Check usePhone for advanced country filtering", () => {
const {result} = renderHook(usePhoneTester, {
initialProps: {
onlyCountries: ["ae", "gb", "us"] as any,
excludeCountries: ["1907", "1205", "1251"] as any,
}
});

expect(result.current.countriesList.map(c => c[2]).includes("1"));
expect(result.current.countriesList.map(c => c[2]).includes("44"));
expect(result.current.countriesList.map(c => c[2]).includes("971"));

expect(!result.current.countriesList.map(c => c[2]).includes("1907"));
expect(!result.current.countriesList.map(c => c[2]).includes("1205"));
expect(!result.current.countriesList.map(c => c[2]).includes("1251"));
})

it("Check usePhone without parentheses", () => {
const {result} = renderHook(usePhoneTester, {
initialProps: {
Expand Down

0 comments on commit 4037245

Please sign in to comment.