Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/bright-trainers-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Internal refactoring of form fields, deprecation of Form.Control and introduction of Form.PlainInput.
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ export const AddDomainPage = withCardStateProvider(() => {
>
<Form.Root onSubmit={onSubmit}>
<Form.ControlRow elementId={nameField.id}>
<Form.Control
<Form.PlainInput
{...nameField.props}
autoFocus
required
isRequired
/>
</Form.ControlRow>
<FormButtons isDisabled={!canSubmit} />
Expand Down
40 changes: 16 additions & 24 deletions packages/clerk-js/src/ui/components/SignUp/SignUpForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,36 @@ export const SignUpForm = (props: SignUpFormProps) => {
{(shouldShow('firstName') || shouldShow('lastName')) && (
<Form.ControlRow elementId='name'>
{shouldShow('firstName') && (
<Form.Control
{...{
...formState.firstName.props,
isRequired: fields.firstName!.required,
isOptional: !fields.firstName!.required,
}}
<Form.PlainInput
{...formState.firstName.props}
isRequired={fields.firstName!.required}
isOptional={!fields.firstName!.required}
/>
)}
{shouldShow('lastName') && (
<Form.Control
{...{
...formState.lastName.props,
isRequired: fields.lastName!.required,
isOptional: !fields.lastName!.required,
}}
<Form.PlainInput
{...formState.lastName.props}
isRequired={fields.lastName!.required}
isOptional={!fields.lastName!.required}
/>
)}
</Form.ControlRow>
)}
{shouldShow('username') && (
<Form.ControlRow elementId='username'>
<Form.Control
{...{
...formState.username.props,
isRequired: fields.username!.required,
isOptional: !fields.username!.required,
}}
<Form.PlainInput
{...formState.username.props}
isRequired={fields.username!.required}
isOptional={!fields.username!.required}
/>
</Form.ControlRow>
)}
{shouldShow('emailAddress') && (
<Form.ControlRow elementId='emailAddress'>
<Form.Control
{...{
...formState.emailAddress.props,
isRequired: fields.emailAddress!.required,
isOptional: !fields.emailAddress!.required,
}}
<Form.PlainInput
{...formState.emailAddress.props}
isRequired={fields.emailAddress!.required}
isOptional={!fields.emailAddress!.required}
actionLabel={canToggleEmailPhone ? 'Use phone instead' : undefined}
onActionClicked={canToggleEmailPhone ? () => handleEmailPhoneToggle('phoneNumber') : undefined}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export const UsernamePage = withCardStateProvider(() => {
>
<Form.Root onSubmit={updatePassword}>
<Form.ControlRow elementId={usernameField.id}>
<Form.Control
<Form.PlainInput
{...usernameField.props}
required
autoFocus
isRequired
/>
</Form.ControlRow>
<FormButtons isDisabled={!canSubmit} />
Expand Down
225 changes: 225 additions & 0 deletions packages/clerk-js/src/ui/elements/FieldControl.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
import type { FieldId } from '@clerk/types';
import type { PropsWithChildren } from 'react';
import React, { forwardRef } from 'react';

import type { LocalizationKey } from '../customizables';
import {
descriptors,
Flex,
FormControl as FormControlPrim,
FormLabel,
Icon as IconCustomizable,
Input,
Link,
localizationKeys,
Text,
useLocalizations,
} from '../customizables';
import { FormFieldContextProvider, sanitizeInputProps, useFormField } from '../primitives/hooks';
import type { PropsOfComponent } from '../styledSystem';
import type { useFormControl as useFormControlUtil } from '../utils';
import { useFormControlFeedback } from '../utils';
import { useCardState } from './contexts';
import type { FormFeedbackProps } from './FormControl';
import { FormFeedback } from './FormControl';

type FormControlProps = Omit<PropsOfComponent<typeof Input>, 'label' | 'placeholder' | 'disabled' | 'required'> &
ReturnType<typeof useFormControlUtil<FieldId>>['props'];

const Root = (props: PropsWithChildren<FormControlProps>) => {
const card = useCardState();
const {
id,
isRequired,
sx,
setError,
setInfo,
setSuccess,
setWarning,
setHasPassedComplexity,
clearFeedback,
feedbackType,
feedback,
isFocused,
} = props;

const { debounced: debouncedState } = useFormControlFeedback({ feedback, feedbackType, isFocused });

const isDisabled = props.isDisabled || card.isLoading;

return (
<FormFieldContextProvider {...{ ...props, isDisabled }}>
{/*Most of our primitives still depend on this provider.*/}
{/*TODO: In follow-up PRs these will be removed*/}
<FormControlPrim
elementDescriptor={descriptors.formField}
elementId={descriptors.formField.setId(id)}
id={id}
hasError={debouncedState.feedbackType === 'error'}
isDisabled={isDisabled}
isRequired={isRequired}
setError={setError}
setSuccess={setSuccess}
setWarning={setWarning}
setInfo={setInfo}
setHasPassedComplexity={setHasPassedComplexity}
clearFeedback={clearFeedback}
sx={sx}
>
{props.children}
</FormControlPrim>
</FormFieldContextProvider>
);
};

const FieldAction = (
props: PropsWithChildren<{ localizationKey?: LocalizationKey | string; onClick?: React.MouseEventHandler }>,
) => {
const { fieldId, isDisabled } = useFormField();

if (!props.localizationKey && !props.children) {
return null;
}

return (
<Link
localizationKey={props.localizationKey}
elementDescriptor={descriptors.formFieldAction}
elementId={descriptors.formFieldLabel.setId(fieldId)}
isDisabled={isDisabled}
colorScheme='primary'
onClick={e => {
e.preventDefault();
props.onClick?.(e);
}}
>
{props.children}
</Link>
);
};

const FieldOptionalLabel = () => {
const { fieldId, isDisabled } = useFormField();
return (
<Text
localizationKey={localizationKeys('formFieldHintText__optional')}
elementDescriptor={descriptors.formFieldHintText}
elementId={descriptors.formFieldHintText.setId(fieldId)}
as='span'
colorScheme='neutral'
variant='smallRegular'
isDisabled={isDisabled}
/>
);
};

const FieldLabelIcon = (props: { icon?: React.ComponentType }) => {
const { t } = useLocalizations();
if (!props.icon) {
return null;
}

return (
<Flex
as={'span'}
title={t(localizationKeys('formFieldHintText__slug'))}
sx={{
marginRight: 'auto',
}}
>
<IconCustomizable
icon={props.icon}
sx={theme => ({
marginLeft: theme.space.$0x5,
color: theme.colors.$blackAlpha400,
width: theme.sizes.$4,
height: theme.sizes.$4,
})}
/>
</Flex>
);
};

const FieldLabel = (props: PropsWithChildren<{ localizationKey?: LocalizationKey | string }>) => {
const { isRequired, id, label, isDisabled, hasError } = useFormField();

if (!(props.localizationKey || label) && !props.children) {
return null;
}

return (
<FormLabel
localizationKey={props.localizationKey || label}
elementDescriptor={descriptors.formFieldLabel}
elementId={descriptors.formFieldLabel.setId(id as FieldId)}
hasError={!!hasError}
isDisabled={isDisabled}
isRequired={isRequired}
sx={{
display: 'flex',
alignItems: 'center',
}}
>
{props.children}
</FormLabel>
);
};

const FieldLabelRow = (props: PropsWithChildren) => {
const { fieldId } = useFormField();
return (
<Flex
justify={'between'}
align='center'
elementDescriptor={descriptors.formFieldLabelRow}
elementId={descriptors.formFieldLabelRow.setId(fieldId)}
sx={theme => ({
marginBottom: theme.space.$1,
marginLeft: 0,
})}
>
{props.children}
</Flex>
);
};

const FieldFeedback = (props: Pick<FormFeedbackProps, 'elementDescriptors'>) => {
const { feedback, feedbackType, isFocused, fieldId } = useFormField();
const { debounced } = useFormControlFeedback({ feedback, feedbackType, isFocused });

return (
<FormFeedback
{...{
...debounced,
elementDescriptors: props.elementDescriptors,
id: fieldId,
}}
/>
);
};

const InputElement = forwardRef<HTMLInputElement>((_, ref) => {
const { t } = useLocalizations();
const formField = useFormField();
const { placeholder, ...inputProps } = sanitizeInputProps(formField);
return (
<Input
ref={ref}
elementDescriptor={descriptors.formFieldInput}
elementId={descriptors.formFieldInput.setId(formField.fieldId)}
{...inputProps}
placeholder={t(placeholder)}
/>
);
});

export const Field = {
Root: Root,
Label: FieldLabel,
LabelRow: FieldLabelRow,
Input: InputElement,
Action: FieldAction,
AsOptional: FieldOptionalLabel,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename this to OptionalLabel ?

LabelIcon: FieldLabelIcon,
Feedback: FieldFeedback,
};
46 changes: 46 additions & 0 deletions packages/clerk-js/src/ui/elements/Form.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { createContextAndHook } from '@clerk/shared/react';
import type { FieldId } from '@clerk/types';
import type { PropsWithChildren } from 'react';
import React, { useState } from 'react';

import type { LocalizationKey } from '../customizables';
import { Button, descriptors, Flex, Form as FormPrim, localizationKeys } from '../customizables';
import { useLoadingStatus } from '../hooks';
import type { PropsOfComponent } from '../styledSystem';
import { useCardState } from './contexts';
import { Field } from './FieldControl';
import { FormControl } from './FormControl';

const [FormState, useFormState] = createContextAndHook<{
Expand Down Expand Up @@ -118,10 +121,53 @@ const FormControlRow = (props: Omit<PropsOfComponent<typeof Flex>, 'elementId'>
);
};

type CommonFieldRootProps = Omit<PropsOfComponent<typeof Field.Root>, 'children'>;

type CommonInputProps = CommonFieldRootProps & {
isOptional?: boolean;
actionLabel?: string | LocalizationKey;
onActionClicked?: React.MouseEventHandler;
icon?: React.ComponentType;
};

const CommonInputWrapper = (props: PropsWithChildren<CommonInputProps>) => {
const { isOptional, icon, actionLabel, children, onActionClicked, ...fieldProps } = props;
return (
<Field.Root {...fieldProps}>
<Field.LabelRow>
<Field.Label />
<Field.LabelIcon icon={icon} />
{!actionLabel && isOptional && <Field.AsOptional />}
{actionLabel && (
<Field.Action
localizationKey={actionLabel}
onClick={onActionClicked}
/>
)}
<Field.Action />
</Field.LabelRow>
{children}
<Field.Feedback />
</Field.Root>
);
};

const PlainInput = (props: CommonInputProps) => {
return (
<CommonInputWrapper {...props}>
<Field.Input />
</CommonInputWrapper>
);
};

export const Form = {
Root: FormRoot,
ControlRow: FormControlRow,
/**
* @deprecated Use Form.PlainInput
*/
Control: FormControl,
PlainInput,
SubmitButton: FormSubmit,
ResetButton: FormReset,
};
Expand Down
Loading