Convert Figma logo to code with AI

vantezzen logoautoform

🌟 Automatically render forms for your existing data schema

2,834
102
2,834
1

Top Related Projects

34,041

Build forms in React, without the tears 😭

📋 React Hooks for form state management and validation (Web + React Native)

🏁 High performance subscription-based form state management for React

4,460

Performance-focused API for React forms 🚀

Quick Overview

Auto Form is a React library that simplifies form creation and management. It automatically generates form fields based on a schema, handles validation, and manages form state, reducing boilerplate code and streamlining the form development process.

Pros

  • Automatic form generation based on schema, saving development time
  • Built-in validation and error handling
  • Seamless integration with React and TypeScript
  • Customizable form fields and layouts

Cons

  • Learning curve for developers new to schema-based form generation
  • May be overkill for simple forms
  • Limited customization options compared to building forms from scratch

Code Examples

Creating a basic form:

import { AutoForm } from 'auto-form';

const schema = {
  name: { type: 'string', required: true },
  email: { type: 'string', format: 'email', required: true },
  age: { type: 'number', minimum: 18 }
};

function MyForm() {
  return (
    <AutoForm
      schema={schema}
      onSubmit={(data) => console.log(data)}
    />
  );
}

Customizing field rendering:

import { AutoForm, useField } from 'auto-form';

function CustomInput({ name }) {
  const { value, onChange, error } = useField(name);
  return (
    <div>
      <input value={value} onChange={(e) => onChange(e.target.value)} />
      {error && <span style={{ color: 'red' }}>{error}</span>}
    </div>
  );
}

function MyForm() {
  return (
    <AutoForm
      schema={schema}
      onSubmit={handleSubmit}
      fields={{
        name: CustomInput,
        email: CustomInput
      }}
    />
  );
}

Using with React Hook Form:

import { AutoForm } from 'auto-form';
import { useForm } from 'react-hook-form';

function MyForm() {
  const form = useForm();

  return (
    <AutoForm
      schema={schema}
      form={form}
      onSubmit={form.handleSubmit(handleSubmit)}
    />
  );
}

Getting Started

  1. Install the package:

    npm install auto-form
    
  2. Import and use in your React component:

    import { AutoForm } from 'auto-form';
    
    const schema = {
      // Define your form schema here
    };
    
    function MyForm() {
      return (
        <AutoForm
          schema={schema}
          onSubmit={(data) => {
            // Handle form submission
          }}
        />
      );
    }
    
  3. Customize as needed using the library's hooks and components.

Competitor Comparisons

34,041

Build forms in React, without the tears 😭

Pros of Formik

  • More mature and widely adopted, with extensive documentation and community support
  • Offers greater flexibility and control over form state management
  • Integrates well with Yup for schema validation

Cons of Formik

  • Steeper learning curve, especially for beginners
  • Requires more boilerplate code for basic form setups
  • Can be overkill for simple form implementations

Code Comparison

Formik:

import { Formik, Form, Field } from 'formik';

<Formik initialValues={{ name: '' }} onSubmit={handleSubmit}>
  <Form>
    <Field name="name" />
    <button type="submit">Submit</button>
  </Form>
</Formik>

Auto-form:

import { AutoForm } from 'auto-form';

<AutoForm
  fields={[{ name: 'name', type: 'text' }]}
  onSubmit={handleSubmit}
/>

Summary

Formik is a powerful and flexible form library with a large ecosystem, suitable for complex form scenarios. Auto-form, on the other hand, aims to simplify form creation with minimal setup, making it ideal for quick prototyping or simpler use cases. The choice between the two depends on the project's complexity and the developer's familiarity with form libraries.

📋 React Hooks for form state management and validation (Web + React Native)

Pros of react-hook-form

  • More mature and widely adopted in the React community
  • Extensive documentation and large ecosystem of extensions
  • Highly performant with minimal re-renders

Cons of react-hook-form

  • Steeper learning curve for beginners
  • Requires more manual setup and configuration
  • Less automated form generation compared to auto-form

Code Comparison

react-hook-form:

import { useForm } from "react-hook-form";

const { register, handleSubmit } = useForm();
const onSubmit = data => console.log(data);

<form onSubmit={handleSubmit(onSubmit)}>
  <input {...register("firstName")} />
  <input {...register("lastName")} />
  <button type="submit">Submit</button>
</form>

auto-form:

import { AutoForm } from "auto-form";

const formSchema = {
  firstName: { type: "text" },
  lastName: { type: "text" }
};

<AutoForm
  schema={formSchema}
  onSubmit={data => console.log(data)}
/>

react-hook-form offers more granular control over form elements and validation, while auto-form provides a more streamlined approach with automatic form generation based on a schema. react-hook-form is better suited for complex forms with custom logic, whereas auto-form excels in quickly creating simple forms with minimal setup.

🏁 High performance subscription-based form state management for React

Pros of react-final-form

  • More mature and widely adopted in the React ecosystem
  • Extensive documentation and community support
  • Highly flexible and customizable for complex form scenarios

Cons of react-final-form

  • Steeper learning curve for beginners
  • Requires more boilerplate code for simple forms
  • Larger bundle size due to additional features

Code Comparison

react-final-form:

import { Form, Field } from 'react-final-form'

<Form
  onSubmit={onSubmit}
  render={({ handleSubmit }) => (
    <form onSubmit={handleSubmit}>
      <Field name="firstName" component="input" placeholder="First Name" />
      <button type="submit">Submit</button>
    </form>
  )}
/>

auto-form:

import { AutoForm } from 'auto-form'

<AutoForm
  onSubmit={onSubmit}
  fields={[
    { name: 'firstName', type: 'text', placeholder: 'First Name' }
  ]}
/>

Summary

react-final-form offers more power and flexibility for complex form scenarios, backed by extensive documentation and community support. However, it comes with a steeper learning curve and requires more code for simple forms. auto-form, on the other hand, provides a simpler API for basic form creation, making it easier to get started quickly. The choice between the two depends on the complexity of your form requirements and your familiarity with React form libraries.

4,460

Performance-focused API for React forms 🚀

Pros of Unform

  • More comprehensive form management solution with advanced features like schema validation and custom inputs
  • Better suited for complex, large-scale applications with intricate form requirements
  • Extensive documentation and community support

Cons of Unform

  • Steeper learning curve due to its more complex API and concepts
  • Potentially overkill for simple form implementations or smaller projects

Code Comparison

Unform:

import React from 'react';
import { Form } from '@unform/web';
import Input from './components/Input';

function MyForm() {
  function handleSubmit(data) {
    console.log(data);
  }

  return (
    <Form onSubmit={handleSubmit}>
      <Input name="email" />
      <Input name="password" type="password" />
      <button type="submit">Submit</button>
    </Form>
  );
}

Auto-form:

import React from 'react';
import { AutoForm } from 'auto-form';

function MyForm() {
  return (
    <AutoForm
      onSubmit={(data) => console.log(data)}
      fields={[
        { name: 'email', type: 'email' },
        { name: 'password', type: 'password' },
      ]}
    />
  );
}

The code comparison shows that Auto-form provides a more concise and declarative approach, while Unform offers more flexibility and control over form rendering and behavior.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

AutoForm

Automatically render forms for your existing data schema.


[!NOTE]

Work in Progress AutoForm as a standalone package is still work in progress. If you want to help out, please check out the GitHub repository and add your own integrations!

Find the full documentation at autoform.vantezzen.io.


AutoForm is now a full library!

AutoForm quickly grew from a small component into a codebase larger than any shadcn component should be. To let AutoForm grow without bloating your shadcn/ui components, AutoForm is now a full library!

Don't worry, you can still use AutoForm with your shadcn components and expand it with your own components - but it now also supports integration into other UI libraries like MUI and Mantine and we plan on adding support for other schema libraries than zod too.

Check out the new AutoForm documentation for more information.

The new AutoForm does not have full feature-parity with the old AutoForm as we look into what features actually make sense and which once just bloat the experience. If you're missing a feature or have problems with the new library, feel free to write your feedback in the welcome post!

If you want to continue using the pure shadcn/ui component, you can find the old codebase at https://github.com/vantezzen/auto-form/tree/pure-shadcn - but write us what keeps you from migrating to the new library!


What is AutoForm? Let's say you have a zod schema that you already use for your backend:

import { z } from "zod";

const userSchema = z.object({
  name: z.string(),
  birthday: z.coerce.date(),
  email: z.string().email(),
});

With AutoForm, you can automatically render a form for this schema:

import { AutoForm } from "@autoform/mui";
import { ZodProvider } from "@autoform/zod";

function MyForm() {
  return (
    <AutoForm
      schema={schemaProvider}
      onSubmit={(data) => {
        console.log(data);
      }}
      withSubmit
    />
  );
}

AutoForm itself is agnostic to the schema library, rendering library and UI library you use, but it comes with a set of official packages that make it easy to use with popular libraries like Zod, React, Material UI, etc.

When to use AutoForm?

AutoForm is mostly meant as a drop-in form builder for your internal and low-priority forms with existing schemas. For example, if you already have schemas for your API and want to create a simple admin panel to edit user profiles, simply pass the schema to AutoForm and you're done.

As forms almost always grow more complex, AutoForm gives you options to customize how forms are rendered (e.g. using the fieldConfig options) and gives you escape hatches to customize the form even further.

However, AutoForm does not aim to be a full-featured form builder. It does not aim to support every edge case in your schema or allow building complex, multi-page forms. If you need more customization, feel free to customize AutoForm's renderer in your project or use more powerful form builders like Formik - though those require more specialized configuration instead of simple drop-in support for your zod schema. For an example on how AutoForm can be extended for more powerful, YAML-based, multi-page forms, see AutoForm YAML.

shadcn/ui component

AutoForm started out as a shadcn/ui component but grew so large I decided it's best to split it into a package instead.

@autoform/react does currently not have full feature-parity with the shadcn/ui component, but it's getting there. If you want to use the shadcn/ui component, you can still use it as a standalone package.

Development

AutoForm uses a TurboRepo monorepo setup. To get started, run:

npm install
npm run dev

This will start the development server for the documentation website and the AutoForm package itself.

For releases, AutoForm uses changesets. To create a new release, run:

npm run build
npx changeset

This will guide you through creating a new changeset. To publish the changeset, run:

npx changeset version
npx changeset publish

License

All packages in the AutoForm monorepo are licensed under the MIT license.

NPM DownloadsLast 30 Days