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

feat(ui): Add support for showOptionalFields layout prop #4198

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/quiet-readers-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
32 changes: 20 additions & 12 deletions packages/ui/src/components/sign-up/steps/start.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { PhoneNumberField } from '~/common/phone-number-field';
import { UsernameField } from '~/common/username-field';
import { LOCALIZATION_NEEDED } from '~/constants/localizations';
import { useAppearance } from '~/contexts';
import { useAttributes } from '~/hooks/use-attributes';
import { useSignUpAttributes } from '~/hooks/use-attributes';
import { useCard } from '~/hooks/use-card';
import { useDevModeWarning } from '~/hooks/use-dev-mode-warning';
import { useDisplayConfig } from '~/hooks/use-display-config';
Expand All @@ -30,12 +30,20 @@ export function SignUpStart() {
const enabledConnections = useEnabledConnections();
const { userSettings } = useEnvironment();
const { t } = useLocalizations();
const { enabled: firstNameEnabled, required: firstNameRequired } = useAttributes('first_name');
const { enabled: lastNameEnabled, required: lastNameRequired } = useAttributes('last_name');
const { enabled: usernameEnabled, required: usernameRequired } = useAttributes('username');
const { enabled: phoneNumberEnabled, required: phoneNumberRequired } = useAttributes('phone_number');
const { enabled: emailAddressEnabled, required: emailAddressRequired } = useAttributes('email_address');
const { enabled: passwordEnabled, required: passwordRequired } = useAttributes('password');
const { required: firstNameRequired, show: showFirstName } = useSignUpAttributes('first_name');
const { required: lastNameRequired, show: showLastName } = useSignUpAttributes('last_name');
const { enabled: usernameEnabled, required: usernameRequired, show: showUserName } = useSignUpAttributes('username');
const {
enabled: phoneNumberEnabled,
required: phoneNumberRequired,
show: showPhoneNumber,
} = useSignUpAttributes('phone_number');
const {
enabled: emailAddressEnabled,
required: emailAddressRequired,
show: showEmailAddress,
} = useSignUpAttributes('email_address');
const { required: passwordRequired, show: showPassword } = useSignUpAttributes('password');
const { applicationName } = useDisplayConfig();

const hasConnection = enabledConnections.length > 0;
Expand Down Expand Up @@ -81,7 +89,7 @@ export function SignUpStart() {

{hasIdentifier ? (
<div className='flex flex-col gap-4'>
{firstNameEnabled && lastNameEnabled ? (
{showFirstName && showLastName ? (
<div className='flex gap-4'>
<FirstNameField
required={firstNameRequired}
Expand All @@ -94,7 +102,7 @@ export function SignUpStart() {
</div>
) : null}

{usernameEnabled ? (
{showUserName ? (
<UsernameField
required={usernameRequired}
disabled={isGlobalLoading}
Expand All @@ -108,9 +116,9 @@ export function SignUpStart() {
/>
) : (
<>
<EmailField disabled={isGlobalLoading} />
{showEmailAddress ? <EmailField disabled={isGlobalLoading} /> : null}

{phoneNumberEnabled ? (
{showPhoneNumber ? (
<PhoneNumberField
required={phoneNumberRequired}
disabled={isGlobalLoading}
Expand All @@ -120,7 +128,7 @@ export function SignUpStart() {
</>
)}

{passwordEnabled && passwordRequired ? (
{showPassword ? (
<PasswordField
validatePassword
label={t('formFieldLabel__password')}
Expand Down
25 changes: 24 additions & 1 deletion packages/ui/src/hooks/use-attributes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
import type { Attribute, AttributeData } from '@clerk/types';

import { useAppearance } from '~/contexts';

import { useEnvironment } from './use-environment';

export function useAttributes(attribute: Attribute): AttributeData {
const environment = useEnvironment();

const userSettingsAttributes = environment.userSettings.attributes;

return userSettingsAttributes[attribute];
}

type SignUpAttributeData = AttributeData & {
/**
* Should the field be visible to the user durning sign up flow.
*/
show: boolean;
};

/**
* Custom attributes for sign up flow that includes whether or not a field should be shown
* based on enabled/required and showOptionalFields layout prop.
*/
export function useSignUpAttributes(attribute: Attribute): SignUpAttributeData {
const attr = useAttributes(attribute);
const { layout } = useAppearance().parsedAppearance;
const { showOptionalFields } = layout;

return {
...attr,
show: (showOptionalFields || attr.required) && attr.enabled,
};
}
Loading