Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ui): Break phone number utils and data into proper folders #3892

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/stale-lemons-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,70 @@ import { cx } from 'cva';
import * as React from 'react';
import { Button, Dialog, DialogTrigger, Popover } from 'react-aria-components';

import { type CountryIso, IsoToCountryMap } from '~/constants/phone-number';
import { useLocalizations } from '~/hooks/use-localizations';
import * as Field from '~/primitives/field';
import * as Icon from '~/primitives/icon';
import { mergeRefs } from '~/utils/merge-refs';
import { extractDigits, formatPhoneNumber, parsePhoneString } from '~/utils/phone-number';

import { IsoToCountryMap } from './data';
import { useFormattedPhoneNumber } from './useFormattedPhoneNumber';
type UseFormattedPhoneNumberProps = {
initPhoneWithCode: string;
locationBasedCountryIso?: CountryIso;
};

const format = (str: string, iso: CountryIso) => {
if (!str) {
return '';
}
const country = IsoToCountryMap.get(iso);
return formatPhoneNumber(str, country?.pattern, country?.code);
};

const useFormattedPhoneNumber = (props: UseFormattedPhoneNumberProps) => {
const [number, setNumber] = React.useState(() => {
const { number } = parsePhoneString(props.initPhoneWithCode || '');
return number;
});

const [iso, setIso] = React.useState(
parsePhoneString(props.initPhoneWithCode || '').number
? parsePhoneString(props.initPhoneWithCode || '').iso
: props.locationBasedCountryIso || 'us',
);

React.useEffect(() => {
setNumber(extractDigits(number));
}, [iso, number]);

const numberWithCode = React.useMemo(() => {
if (!number) {
return '';
}
const dialCode = IsoToCountryMap.get(iso)?.code || '1';
return '+' + extractDigits(`${dialCode}${number}`);
}, [iso, number]);

const formattedNumber = React.useMemo(() => {
return format(number, iso);
}, [iso, number]);

const setNumberAndIso = React.useCallback((str: string) => {
const { iso, number } = parsePhoneString(str);
setNumber(number);
setIso(iso);
}, []);

return {
setNumber,
setIso,
setNumberAndIso,
iso,
number,
numberWithCode,
formattedNumber,
};
};

const countryOptions = Array.from(IsoToCountryMap.values()).map(country => {
return {
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion packages/ui/src/components/sign-up/steps/verifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as SignUp from '@clerk/elements/sign-up';

import { GlobalError } from '~/common/global-error';
import { OTPField } from '~/common/otp-field';
import { parsePhoneString } from '~/common/phone-number-field/utils';
import { useDisplayConfig } from '~/hooks/use-display-config';
import { useEnvironment } from '~/hooks/use-environment';
import { useLocalizations } from '~/hooks/use-localizations';
Expand All @@ -13,6 +12,7 @@ import * as Card from '~/primitives/card';
import * as Icon from '~/primitives/icon';
import { LinkButton } from '~/primitives/link';
import type { RequireExactlyOne } from '~/types/utils';
import { parsePhoneString } from '~/utils/phone-number';

/* Internal
============================================ */
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/utils/format-safe-identifier.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stringToFormattedPhoneString } from '~/common/phone-number-field/utils';
import { stringToFormattedPhoneString } from '~/utils/phone-number';

export const isMaskedIdentifier = (str: string | undefined | null) => str && str.includes('**');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CountryEntry, CountryIso } from './data';
import { CodeToCountriesMap, IsoToCountryMap, SubAreaCodeSets } from './data';
import type { CountryEntry, CountryIso } from '~/constants/phone-number';
import { CodeToCountriesMap, IsoToCountryMap, SubAreaCodeSets } from '~/constants/phone-number';

// offset between uppercase ascii and regional indicator symbols
const OFFSET = 127397;
Expand Down
Loading