Form Component Documentation
Introduction
The Form
component is a versatile and flexible form builder in React, designed to handle form state management, validation, and submission. It provides an intuitive API to define form fields, their validation rules, and custom rendering logic.
Component Description
The Form
component simplifies the process of creating forms by allowing you to define a validation schema and the route for form submission. This example demonstrates how to use the Form
component to create a registration form that collects a user's name and surname, ensuring both fields are required.
Usage
To use the Form
component, follow these steps:
- Validator and Route Setup:
- Create a validation schema using the validation library (VineJS.) and pass it to the
schema
prop. - Specify the submission route using the
routeName
prop.
- Create a validation schema using the validation library (VineJS.) and pass it to the
Important! You need, in routes, to specify the route name! With the
as
method.
router.post("otp/ask-otp", [OtpController, "askForOtp"])
.as("otp.ask-otp")
-
Form Fields Definition:
- Inside the
Form
component, use theFormField
sub-component to define each field. - The
FormField
requires acontrol
prop (provided by theForm
component) and thename
of the field. - Use the
render
prop to define how the field should be rendered, including the label, input, and any additional styling or requirements.
- Inside the
-
Submit Button:
- Add a
Button
component for form submission.
- Add a
Example
Below is an example showcasing the usage of the Form
component to create a registration form:
import { Form, FormField, FormItem, FormLabel, FormControl, FormMessage } from 'your-form-library';
import { Input } from 'your-input-component';
import { Button } from 'your-button-component';
// Assuming completeRegistrationValidator is already defined
import { completeRegistrationValidator } from 'your-validator-schema';
const RegistrationForm = () => (
<Form schema={completeRegistrationValidator} routeName={"otp.complete"}>
{({ control }) => (
<div className="space-y-4 text-black">
<FormField
control={control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input
placeholder=""
{...field}
required={true}
type="text"
className="bg-darkgray"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={control}
name="surname"
render={({ field }) => (
<FormItem>
<FormLabel>Surname</FormLabel>
<FormControl>
<Input
placeholder=""
{...field}
required={true}
type="text"
className="bg-darkgray"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button className="w-full" type="submit">Submit</Button>
</div>
)}
</Form>
);
export default RegistrationForm;
Detailed Explanation
-
Form Component:
schema={completeRegistrationValidator}
: Specifies the validation schema.routeName={"otp.complete"}
: Specifies the route for form submission.
-
FormField Component:
control={control}
: Prop provided by theForm
component to manage field state.name="name"
andname="surname"
: Field names corresponding to the validation schema.
-
Field Rendering:
FormItem
,FormLabel
,FormControl
, andFormMessage
are used to structure the field's layout.Input
component is used for the actual input field with specific props likerequired
,type
, and custom classes.
-
Button Component:
- A
Button
component is used to submit the form, styled with a full-width class and asubmit
type.
- A
This example demonstrates how to create a simple registration form using the Form
component, showcasing how to integrate validation and handle form submission seamlessly.