Easy manage form fields, ensuring type-safety and strong reusability built with react-hook-form
npm install hookform-field


- Type-safe
- Strongly reusable
- Easy to use
- Extends all the awesome features from react-hook-form.
This documentation will guide you through the usage of the custom form components built using React Hook Form. This package help you manage easily form fields such as text input, number input, file input, and select dropdown, etc.. Below, you will find detailed instructions on how to set up and use these components in your React application.
First, install the package via npm/yarn/pnpm:
``bashnpm
npm install hookform-field react-hook-form
Usage
$3
You can create custom form fields by using the
createField function. For example, to create text, number, file, and select fields:`javascript// Example: //src/components/form/field.tsx
import React from "react";
import { createField } from "hookform-field";
import Foo from 'your_component_path'
interface InputProps {}
interface NumberProps {}
interface FileProps {}
interface SelectProps {
options: any[];
}
const Field = createField({
text: (props: InputProps) => ,
number: (props: NumberProps) => ,
file: (props: FileProps) => ,
select: (props: SelectProps) => (
),
foo: Foo,
});
export default Field;
`$3
Next, create a form using the
Form component and include your custom fields:`javascript
import React from "react";
import { Form } from "hookform-field";
import Field from "@/components/form/field"; // Import the Field component you createdimport { z } from "zod"
import { zodResolver } from "@hookform/resolvers/zod" // Import your resolver from hookform
const schema = z.object({
name: z.string(),
amount: z.number(),
avatar: z.any(),
age: z.string(),
})
type SchemaInferred = z.infer
const resolver = zodResolver(schema)
const MyForm = () => {
return (
//
useForm wrapped by of hookform-field help you save time define it
);
};export default MyForm;
`The
component will be type-safe based on your component values, suggesting the correct props based on the component props.$3
Finally, render your form in your application:
`javascript
import React from "react"
import ReactDOM from "react-dom"
import MyForm from "./MyForm" // Import the MyForm component you createdconst App = () => {
return (
My Custom Form
)
}
``TBD
This documentation provides a comprehensive guide to using the custom form components in your React application. By following these steps, you can create and render various form fields and ensure they function correctly through testing.