+
+
+ Spike react-hook-form
+
+
+
+
+
+
+
+Currently, Heartbeat lacks a form management tool, making form state management difficult.
+
+Introduce a new form management tool to address the existing form state issues, enhancing the maintainability and robustness of the form section.
+
+Integrate react-hook-form into the form for declaration/validation/submission.
+
+Declare a form instance:
+
+Wrap the form structure with FormProvider. The child DOM nodes contained within FormProvider constitute the scope of the form. All subsequent form operations including validation and submission will be carried out within this scope.
+
+Associate the useForm instance with FormProvider:
+
+With the above code, we quickly declare an empty form that doesn’t contain any fields or validation conditions.
+
+Declare form fields and initial values using JSON structure:
+
+
+Validate form values using yup:
+
+yup comes with rich validation rules to meet basic validation requirements. For complex validation needs, yup’s test method and when method can be used for more sophisticated operations.
+
+
+
+There are two ways to bind form controls:
+
+- Use the register method, which is used for generic components and can simply bind a form component to the form.
+
+
+
+- Use the Controller tag to wrap a custom component.
+
+
+The use of register and controller each has its pros and cons. For systems with complex business scenarios, variable scenes, and higher compatibility and functionality requirements for components, it is recommended to use the controller approach to customize component behavior.
+
+Within a form scope, default → schema → component binding is performed by defining a unique name.
+
+Similarly, the name can be an object access path or even an array index.
+
+
+In business scenarios, forms are usually complex and large. When splitting components, different submodules are usually split into different files.
+index.tsx
+
+In such scenarios, how do we easily access the parent component scope within ChildA and ChildB components?
+react-hook-form provides the useFormContext syntax for accessing the nearest parent form scope.
+
+Through this access method, we can encapsulate some custom form components around react-hook-form, and even combine a series of components into a complex business component, triggering validation and form behavior through manual onChange event.
+
+
+After integrating react-hook-form, for a component containing a complete FormProvider scope within a file, normal testing can be done using userEvent.
+
+Verify if form validation is functioning correctly.
+
+For validating child components within the form, an additional step is required: when initializing the test instance, manually declare FormProvider and validation rules.
+
+
+Through integrating react-hook-form, we’ve decoupled some of the manually written form validation logic. We also aim to unify global form behavior by using shared components, allowing developers to focus more on writing business logic itself. Please note that we are not trying to completely decouple existing redux components. On the contrary, we want to focus more clearly on how to differentiate between form/component internal states and cross-component data that needs to be persisted, making the system architecture and standards clearer.
+
+
+
+
+