Use import { AutoForm } from "@/components/ui/autoform" to import the component. By default, the registry will use the zod integration. If you want to use another schema library, simply update the /components/ui/autoform/utils.ts file.
more to come...
If you want to integrate into your own UI components or another UI library, check out the custom integration guide.
In the docs, we will use Material UI as an example but simply replace the imports with the ones from your UI library.
AutoForm can only be used inside a client-side React component due to serialization of the schema and values to your event listeners. If you want to use it in a Next.js app, simply mark your component with "use client":
// MyPage.tsxexport default function MyPage() { return ( <div> <MyForm /> </div> );}// MyForm.tsx"use client";import { AutoForm } from "@autoform/mui";export default function MyForm() { return <AutoForm ... />;}
The preferred way is to use the onSubmit prop. This will be called when the form is submitted and the data is valid.
<AutoForm onSubmit={(data, form) => { // Do something with the data // Data is validated and coerced with zod automatically // You can use the "form" prop to access the underlying "react-hook-form" instance // for further information and control over the form }}/>
If you need more control over the form, you can use the onFormInit and values props to get access to the react-hook-form instance and control data directly:
const [values, setValues] = useState({});<AutoForm onFormInit={(form) => { // You can use the "form" prop to access the underlying "react-hook-form" instance // https://www.react-hook-form.com/api/useform/ form.watch((data) => { setValues(data); }); // You can freely save the form instance to a state or context and use it later to access the form state form.formState.isDirty; // => true }} values={values}/>;
This allows you to access form methods and state from the parent component. You can use the onFormInit prop independent of controlled forms to access the form instance.
All children passed to the AutoForm component will be rendered below the form.
<AutoForm> <p className="text-gray-500 text-sm"> By submitting this form, you agree to our{" "} <a href="#" className="text-primary underline"> terms and conditions </a> . </p></AutoForm>