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(elements): Consider ValidityState in FieldState #3594

Conversation

LekoArts
Copy link
Member

@LekoArts LekoArts commented Jun 19, 2024

Description

This PR adds the ValidityState (https://www.radix-ui.com/primitives/docs/components/form#validitystate) to our FieldState logic. So e.g. if someone enters an invalid email address into a <input type="email" /> and focus away, our <FieldState> will now report an error. Previously it didn't do anything.

In this PR also some data attributes were removed/renamed, see changeset + inline comments for more details.

Docs PR: clerk/clerk-docs#1179

Fixes SDK-1638

Checklist

  • npm test runs as expected.
  • npm run build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Copy link

changeset-bot bot commented Jun 19, 2024

🦋 Changeset detected

Latest commit: 76391bc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@clerk/elements Minor
@clerk/ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

* @see https://developer.mozilla.org/en-US/docs/Web/API/ValidityState
*/
const enrichFieldState = (validity: ValidityState | undefined, fieldState: FieldStates) => {
return validity?.valid === false ? FIELD_STATES.error : fieldState;
Copy link
Member Author

Choose a reason for hiding this comment

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

valid

A boolean value that is true if the element meets all its validation constraints, and is therefore considered to be valid, or false if it fails any constraint.

Copy link
Member Author

Choose a reason for hiding this comment

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

I also thought about doing this instead:

return fieldState === FIELD_STATES.idle ? validity?.valid : fieldState

I guess it's a question of what should take precedence. The validity or our own fieldState

@@ -149,7 +166,7 @@ const useForm = ({ flowActor }: { flowActor?: BaseActorRef<{ type: 'SUBMIT' }> }

return {
props: {
[`data-${validity}`]: true,
...(errors.length > 0 ? { 'data-global-error': true } : {}),
Copy link
Member Author

Choose a reason for hiding this comment

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

It didn't make much sense to me to have data-valid/data-invalid on the <form> if that's already placed further below in the DOM tree. And in those instances we refer to the <FieldError> state, not <GlobalError>.

So renaming this to make it more clear that this is about global errors.


return {
hasValue,
props: {
[`data-${validity}`]: true,
Copy link
Member Author

@LekoArts LekoArts Jun 19, 2024

Choose a reason for hiding this comment

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

Radix already adds data-valid/data-invalid correctly to the field, so this is removed to not end up in a situation where both data-valid/data-invalid can exist at the same time.

Comment on lines +462 to +472
<RadixValidityState>
{validity => {
const enrichedFieldState = enrichFieldState(validity, fieldState);

return (
<ValidityStateContext.Provider value={validity}>
{typeof children === 'function' ? children(enrichedFieldState) : children}
</ValidityStateContext.Provider>
);
}}
</RadixValidityState>
Copy link
Member Author

Choose a reason for hiding this comment

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

Main gist of this PR. Using Context here to not have to pass around values through props 👍

@LekoArts LekoArts marked this pull request as ready for review June 19, 2024 08:38
@LekoArts LekoArts enabled auto-merge (squash) June 21, 2024 05:24
@LekoArts LekoArts merged commit 5aedc29 into main Jun 21, 2024
15 checks passed
@LekoArts LekoArts deleted the lekoarts/sdk-1638-bug-client-side-field-validation-not-considered-in-field branch June 21, 2024 05:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants