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

[react/form] Fix Form.Message not being able to be used outside a Form.Field #3044

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

DGiannaris
Copy link

@DGiannaris DGiannaris commented Aug 1, 2024

Description

Closes: #2279

This PR regards an issue where users receive an error when using Form.Message outside of a Form.Field, even when following the documentation and specifying the name prop.

The Problem

The root cause is in the useContext hook within createContext.tsx, which throws an error immediately upon detecting a missing context.

The Changes

This PR introduces the following changes to try and solve that:

  • Enhances the useContext hook in createContext.tsx:

    • Adds an options object parameter with an isOptional prop
    • Checks if the context is optional and returns an empty object if so, preventing unintended errors
  • Improves FormMessage implementation:

    • Prioritizes using the name prop to link Field.Message to a Form.Field when available
    • Falls back to using the form context if name is not provided
    • Throws an appropriate error when both Form.Field context and name prop are absent.
  • Updates Form.stories.tsx:

    • Modifies a Form.Message usage to be out of context, enabling Cypress tests to also check for this scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FormMessage can't be used outside of FormField
1 participant