primitives
primitives copied to clipboard
[react/form] Fix Form.Message not being able to be used outside a Form.Field
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
useContexthook increateContext.tsx:- Adds an
optionsobject parameter with anisOptionalprop - Checks if the context is optional and returns an empty object if so, preventing unintended errors
- Adds an
-
Improves
FormMessageimplementation:- Prioritizes using the name prop to link
Field.Messageto aForm.Fieldwhen available - Falls back to using the form context if name is not provided
- Throws an appropriate error when both
Form.Fieldcontext and name prop are absent.
- Prioritizes using the name prop to link
-
Updates Form.stories.tsx:
- Modifies a
Form.Messageusage to be out of context, enabling Cypress tests to also check for this scenario.
- Modifies a