Install react-geek-form and zod, run:
react-geek-form
zod
npm install react-geek-form zod
pnpm add react-geek-form zod
yarn add react-geek-form zod
Register your custom fields using createInstance:
createInstance
import { createInstance } from "react-geek-form"; // Example Custom Field Componentsconst CustomInput = ({ value, onChange, error }) => ( <input value={value} onChange={(e) => onChange(e.target.value)} />); const CustomTextarea = ({ value, onChange, error }) => ( <textarea value={value} onChange={(e) => onChange(e.target.value)} />); // Create the instance with Custom Field Componentsconst { createForm } = createInstance({ CustomInput, CustomTextarea, // ...[Custom Fields Components]}); // Export createForm for reuse across your appexport { createForm };
Create your first Form component using createForm:
createForm
import { z } from "zod";import { createForm } from "./helpers"; const { forwardFormContext, CustomInput, CustomTextarea } = createForm({ zodSchema: z.object({ first_name: z.string(), middle_name: z.string(), last_name: z.string(), bio: z.string(), }),}); const App = forwardFormContext((_, ctx) => { return ( <form onSubmit={ctx.handleSubmit((values) => console.log(values))}> CustomTextarea component will be like as below. <CustomInput name="first_name" /> {/* Valid */} <CustomInput name="middle_name" /> {/* Valid */} <CustomInput name="last_name" /> {/* Valid */} {/* <CustomInput name="address" /> Invalid; Type error: 'address' is not in the schema */} <CustomTextarea name="bio" /> {/* Valid */} <button type="submit">Submit</button> </form> );}); export default App;