Skip to content

Commit

Permalink
feat(ui): Render email or phone number during verification step (#3694)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcarpenter authored Jul 11, 2024
1 parent 1de2cfb commit e2782ed
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .changeset/thirty-lizards-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
4 changes: 3 additions & 1 deletion packages/ui/src/common/phone-number-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ export const PhoneNumberField = React.forwardRef(function PhoneNumberField(
label = 'Phone number',
name = 'phoneNumber',
hintText = 'Optional',
initPhoneWithCode = '',
locationBasedCountryIso,
onChange,
...props
}: React.InputHTMLAttributes<HTMLInputElement> & {
alternativeFieldTrigger?: React.ReactNode;
label?: React.ReactNode;
hintText?: string;
initPhoneWithCode?: string;
locationBasedCountryIso: CountryIso;
},
forwardedRef: React.ForwardedRef<HTMLInputElement>,
Expand All @@ -42,7 +44,7 @@ export const PhoneNumberField = React.forwardRef(function PhoneNumberField(
const commandInputRef = React.useRef<HTMLInputElement>(null);
const contentWidth = containerRef.current?.clientWidth || 0;
const { setNumber, setIso, setNumberAndIso, numberWithCode, formattedNumber, iso } = useFormattedPhoneNumber({
initPhoneWithCode: selectedCountry.iso,
initPhoneWithCode,
locationBasedCountryIso,
});

Expand Down
26 changes: 26 additions & 0 deletions packages/ui/src/components/sign-up/indentifiers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useClerk } from '@clerk/clerk-react';

import { parsePhoneString } from '~/common/phone-number-field/utils';
import type { RequireExactlyOne } from '~/types/utils';

type Identifiers = {
emailAddress: boolean;
phoneNumber: boolean;
};

type Identifier = RequireExactlyOne<Identifiers>;

export function SignUpIdentifier({ emailAddress, phoneNumber }: Identifier) {
const { client } = useClerk();

if (emailAddress) {
return <span>{client.signUp.emailAddress}</span>;
}

if (phoneNumber) {
const { formattedNumberWithCode } = parsePhoneString(client.signUp.phoneNumber || '');
return <span>{formattedNumberWithCode}</span>;
}

return null;
}
15 changes: 5 additions & 10 deletions packages/ui/src/components/sign-up/sign-up.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import * as Icon from '~/primitives/icon';
import { LinkButton } from '~/primitives/link-button';
import { Seperator } from '~/primitives/seperator';

import { SignUpIdentifier } from './indentifiers';

export function SignUpComponent() {
return (
<SignUp.Root>
Expand Down Expand Up @@ -115,6 +117,7 @@ function SignUpComponentLoaded() {
hintText={t('formFieldHintText__optional')}
required={phoneNumberRequired}
disabled={isGlobalLoading}
initPhoneWithCode={clerk.client.signUp.phoneNumber || ''}
locationBasedCountryIso={locationBasedCountryIso}
/>
) : null}
Expand Down Expand Up @@ -171,11 +174,7 @@ function SignUpComponentLoaded() {
<Card.Description>{t('signUp.phoneCode.subtitle')}</Card.Description>
<Card.Description>
<span className='flex items-center justify-center gap-2'>
{/* TODO: elements work
1. https://linear.app/clerk/issue/SDK-1830/add-signup-elements-for-accessing-email-address-and-phone-number
2. https://linear.app/clerk/issue/SDK-1831/pre-populate-emailphone-number-fields-when-navigating-back-to-the
*/}
+1 (424) 424-4242{' '}
<SignUpIdentifier phoneNumber />
<SignUp.Action
navigate='start'
asChild
Expand Down Expand Up @@ -244,11 +243,7 @@ function SignUpComponentLoaded() {
<Card.Description>{t('signUp.emailCode.subtitle')}</Card.Description>
<Card.Description>
<span className='flex items-center justify-center gap-2'>
{/* TODO: elements work
1. https://linear.app/clerk/issue/SDK-1830/add-signup-elements-for-accessing-email-address-and-phone-number
2. https://linear.app/clerk/issue/SDK-1831/pre-populate-emailphone-number-fields-when-navigating-back-to-the
*/}
[email protected]{' '}
<SignUpIdentifier emailAddress />
<SignUp.Action
navigate='start'
asChild
Expand Down
5 changes: 5 additions & 0 deletions packages/ui/src/types/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// https://github.com/sindresorhus/type-fest/blob/main/source/require-exactly-one.d.ts
export type RequireExactlyOne<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = {
[Key in KeysType]: Required<Pick<ObjectType, Key>> & Partial<Record<Exclude<KeysType, Key>, never>>;
}[KeysType] &
Omit<ObjectType, KeysType>;

0 comments on commit e2782ed

Please sign in to comment.