Skip to content

Commit

Permalink
Merge branch 'main' into stefanos/core-2312-add-console-warning-insid…
Browse files Browse the repository at this point in the history
…e-the-clerktesting-package
  • Loading branch information
anagstef committed May 17, 2024
2 parents b55804d + 2f4ce12 commit 3a21c15
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/elements/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @clerk/elements

## 0.4.5

### Patch Changes

- Update `<FieldError />` to enable `asChild` prop for custom markup in render function usage. ([#3396](https://github.com/clerk/javascript/pull/3396)) by [@alexcarpenter](https://github.com/alexcarpenter)

## 0.4.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/elements/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@clerk/elements",
"version": "0.4.4",
"version": "0.4.5",
"description": "Clerk Elements",
"keywords": [
"clerk",
Expand Down
18 changes: 15 additions & 3 deletions packages/elements/src/react/common/form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type { SetRequired } from 'type-fest';
import type { BaseActorRef } from 'xstate';

import type { ClerkElementsError } from '~/internals/errors';
import { ClerkElementsFieldError } from '~/internals/errors';
import { ClerkElementsFieldError, ClerkElementsRuntimeError } from '~/internals/errors';
import type { FieldDetails } from '~/internals/machines/form';
import {
fieldFeedbackSelector,
Expand All @@ -36,6 +36,7 @@ import {
} from '~/internals/machines/form/form.context';
import { usePassword } from '~/react/hooks/use-password.hook';
import type { ErrorMessagesKey } from '~/react/utils/generate-password-error-text';
import { isReactFragment } from '~/react/utils/is-react-fragment';

import type { OTPInputProps } from './otp';
import { OTP_LENGTH_DEFAULT, OTPInput } from './otp';
Expand Down Expand Up @@ -669,6 +670,10 @@ const GlobalError = React.forwardRef<FormGlobalErrorElement, FormGlobalErrorProp
const Comp = asChild ? Slot : 'div';
const child = typeof children === 'function' ? children(error) : children;

if (isReactFragment(child)) {
throw new ClerkElementsRuntimeError('<GlobalError /> cannot render a Fragment as a child.');
}

return (
<Comp
role='alert'
Expand Down Expand Up @@ -702,7 +707,7 @@ const GlobalError = React.forwardRef<FormGlobalErrorElement, FormGlobalErrorProp
* </Clerk.Field>
*/
const FieldError = React.forwardRef<FormFieldErrorElement, FormFieldErrorProps>(
({ children, code, name, ...rest }, forwardedRef) => {
({ asChild = false, children, code, name, ...rest }, forwardedRef) => {
const fieldContext = useFieldContext();
const fieldName = fieldContext?.name || name;
const { feedback } = useFieldFeedback({ name: fieldName });
Expand All @@ -717,17 +722,24 @@ const FieldError = React.forwardRef<FormFieldErrorElement, FormFieldErrorProps>(
return null;
}

const Comp = asChild ? Slot : 'span';
const child = typeof children === 'function' ? children(error) : children;

// const forceMatch = code ? error.code === code : undefined; // TODO: Re-add when Radix Form is updated

if (isReactFragment(child)) {
throw new ClerkElementsRuntimeError('<FieldError /> cannot render a Fragment as a child.');
}

return (
<RadixFormMessage
data-error-code={error.code}
// forceMatch={forceMatch}
{...rest}
ref={forwardedRef}
asChild
>
{child || error.message}
<Comp>{child || error.message}</Comp>
</RadixFormMessage>
);
},
Expand Down
5 changes: 5 additions & 0 deletions packages/elements/src/react/utils/is-react-fragment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as React from 'react';

export function isReactFragment(node: React.ReactNode) {
return React.isValidElement(node) && node.type === React.Fragment;
}

0 comments on commit 3a21c15

Please sign in to comment.