Convert Figma logo to code with AI

hasanharman logoform-builder

A dynamic form-building tool that allows users to create, customize, and validate forms seamlessly within web applications.

2,050
206
2,050
20

Top Related Projects

34,187

Build forms in React, without the tears 😭

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

4,457

Performance-focused API for React forms 🚀

🏁 High performance subscription-based form state management for React

Quick Overview

Form Builder is a React-based library that allows users to create dynamic forms with various input types. It provides a user-friendly interface for building forms without writing extensive code, making it suitable for both developers and non-technical users.

Pros

  • Easy-to-use interface for creating forms without extensive coding
  • Supports multiple input types, including text, number, dropdown, and more
  • Customizable styling options for form elements
  • Ability to save and load form configurations

Cons

  • Limited documentation and examples available
  • May not support more complex form validation scenarios
  • Dependency on React framework limits its use in other environments
  • Lacks advanced features like conditional form fields or multi-step forms

Code Examples

Creating a basic form:

import FormBuilder from 'form-builder';

const MyForm = () => {
  const formConfig = [
    { type: 'text', label: 'Name', name: 'name' },
    { type: 'email', label: 'Email', name: 'email' },
    { type: 'submit', label: 'Submit' }
  ];

  return <FormBuilder config={formConfig} onSubmit={handleSubmit} />;
};

Adding a custom input type:

import FormBuilder, { registerInputType } from 'form-builder';

registerInputType('phone', PhoneInput);

const formConfig = [
  { type: 'phone', label: 'Phone Number', name: 'phone' }
];

const CustomForm = () => <FormBuilder config={formConfig} />;

Applying custom styling:

import FormBuilder from 'form-builder';

const styles = {
  form: { backgroundColor: '#f0f0f0' },
  input: { border: '1px solid #ccc' },
  button: { backgroundColor: 'blue', color: 'white' }
};

const StyledForm = () => (
  <FormBuilder config={formConfig} styles={styles} />
);

Getting Started

To use Form Builder in your React project:

  1. Install the package:

    npm install @hasanharman/form-builder
    
  2. Import and use in your component:

    import FormBuilder from '@hasanharman/form-builder';
    
    const MyComponent = () => {
      const formConfig = [
        { type: 'text', label: 'Username', name: 'username' },
        { type: 'password', label: 'Password', name: 'password' },
        { type: 'submit', label: 'Login' }
      ];
    
      const handleSubmit = (formData) => {
        console.log('Form submitted:', formData);
      };
    
      return <FormBuilder config={formConfig} onSubmit={handleSubmit} />;
    };
    
    export default MyComponent;
    

This setup creates a simple login form with username and password fields, and a submit button. The handleSubmit function will be called with the form data when the form is submitted.

Competitor Comparisons

34,187

Build forms in React, without the tears 😭

Pros of Formik

  • More mature and widely adopted project with extensive documentation
  • Offers a complete solution for form state management, validation, and submission
  • Integrates well with popular UI libraries like Material-UI and React Bootstrap

Cons of Formik

  • Steeper learning curve for beginners due to its comprehensive feature set
  • Can be overkill for simple form implementations
  • Requires additional setup for custom form layouts and complex validation scenarios

Code Comparison

Form-builder:

<FormBuilder
  fields={[
    { name: 'email', type: 'email', label: 'Email' },
    { name: 'password', type: 'password', label: 'Password' }
  ]}
  onSubmit={handleSubmit}
/>

Formik:

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

Form-builder provides a more declarative approach with a single component, while Formik offers more granular control over form elements. Form-builder is simpler for basic forms, but Formik provides greater flexibility for complex scenarios.

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

Pros of react-hook-form

  • More comprehensive and feature-rich form handling solution
  • Larger community support and regular updates
  • Better performance optimization for complex forms

Cons of react-hook-form

  • Steeper learning curve for beginners
  • May be overkill for simple form implementations
  • Requires additional setup and configuration

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 type="submit" />
</form>

form-builder:

import { FormBuilder } from "form-builder";

const formConfig = {
  fields: [{ name: "firstName", type: "text" }]
};

<FormBuilder
  config={formConfig}
  onSubmit={data => console.log(data)}
/>

react-hook-form offers more granular control over form fields and validation, while form-builder provides a simpler, configuration-based approach. react-hook-form is better suited for complex forms with custom logic, whereas form-builder may be more appropriate for quickly creating basic forms with less code.

4,457

Performance-focused API for React forms 🚀

Pros of Unform

  • More comprehensive form management solution with advanced features like schema validation and custom inputs
  • Better TypeScript support and type safety
  • Larger community and more frequent updates

Cons of Unform

  • Steeper learning curve due to its more complex API
  • Potentially overkill for simple form implementations
  • Requires additional setup and configuration compared to Form-builder

Code Comparison

Form-builder:

<FormBuilder
  fields={[
    { name: 'username', type: 'text', label: 'Username' },
    { name: 'password', type: 'password', label: 'Password' }
  ]}
  onSubmit={handleSubmit}
/>

Unform:

<Form onSubmit={handleSubmit}>
  <Input name="username" label="Username" />
  <Input name="password" type="password" label="Password" />
  <button type="submit">Submit</button>
</Form>

Form-builder provides a more declarative approach with a single component, while Unform offers more flexibility and control over individual form elements. Unform's approach allows for easier customization of form layout and styling, but requires more boilerplate code for basic forms.

🏁 High performance subscription-based form state management for React

Pros of react-final-form

  • More mature and widely adopted project with extensive documentation
  • Offers performance optimizations for large forms
  • Provides a rich ecosystem of add-ons and integrations

Cons of react-final-form

  • Steeper learning curve due to its advanced features
  • May be overkill for simple form implementations
  • Requires additional setup for form validation

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>
  )}
/>

form-builder:

import { FormBuilder } from 'form-builder'

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

Summary

react-final-form is a more robust solution for complex form management, offering advanced features and optimizations. However, it may be more complex to set up and use for simple forms. form-builder appears to be a simpler, more straightforward solution for basic form creation, but may lack some of the advanced features and ecosystem support of react-final-form. The choice between the two depends on the specific requirements of your project and the complexity of the forms you need to build.

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

Form Builder

A dynamic form-building tool that allows users to create, customize, and validate forms seamlessly within web applications. Built with React, Next.js, and various other technologies, Form Builder provides an intuitive interface for developers and users alike.

Table of Contents

Features

  • Dynamic Form Creation: Easily create forms with various input types including text, checkbox, radio buttons, and more.
  • Real-Time Validation: Validate user inputs using the Zod library, ensuring data integrity and user-friendly feedback.
  • Responsive Design: Built with Tailwind CSS, ensuring forms look great on all devices.
  • Customizable Components: Leverage ShadCN components for a consistent and modern UI experience.
  • Server-Side Rendering: Utilize Next.js for optimized performance and SEO.

Live Demo

Check out the live demo of the Form Builder here.

YouTube Demo

Watch the demo

Installation

To get started with Form Builder, follow these steps:

  1. Clone the repository:

    git clone https://github.com/hasanharman/form-builder.git
    
  2. Navigate into the project directory:

    cd form-builder
    
  3. Install the necessary dependencies:

    npm install
    

Usage

To start the development server, run:

npm run dev

Open your browser and navigate to http://localhost:3000 to see the application in action.

Creating a Form

  1. Access the Builder: Once the app is running, navigate to the form builder interface.
  2. Add Inputs: Use the toolbar to add different types of inputs.
  3. Customize Inputs: Click on any input field to configure properties such as label, placeholder, required validation, etc.
  4. Save & Preview: Once your form is built, save it and preview the output.

Components

Form Builder consists of various reusable components:

  • FormContainer: The main container for the form elements.
  • InputField: A customizable input component.
  • SelectField: Dropdown selection component.
  • CheckboxField: A checkbox input component.
  • Button: A styled button component for form submission.

Validation

Form Builder utilizes Zod for input validation. You can define schemas for your forms as follows:

import { z } from 'zod';

const formSchema = z.object({
  name: z.string().min(1, "Name is required"),
  email: z.string().email("Invalid email address"),
  age: z.number().min(18, "You must be at least 18 years old"),
});

This schema can be applied to your form to enforce validation rules.

API

Form Submission

Once your form is ready, you can handle submissions with a simple API call. Here’s an example of how to submit the form data:

const handleSubmit = async (data) => {
  try {
    const response = await fetch('/api/form-submit', {
      method: 'POST',
      body: JSON.stringify(data),
      headers: {
        'Content-Type': 'application/json',
      },
    });
    const result = await response.json();
    console.log('Form submitted successfully:', result);
  } catch (error) {
    console.error('Error submitting form:', error);
  }
};

Contributing

Contributions are welcome! If you would like to contribute to Form Builder, please follow these steps:

  1. Fork the Repository: Click on the “Fork” button at the top right corner of the repository page.
  2. Create a Branch:
    git checkout -b feature/YourFeatureName
    
  3. Make Changes: Implement your feature or fix.
  4. Commit Changes:
    git commit -m "Add a feature"
    
  5. Push Changes:
    git push origin feature/YourFeatureName
    
  6. Create a Pull Request: Go to the original repository and create a pull request.

License

This project is licensed under the MIT License.

Acknowledgements

  • React - A JavaScript library for building user interfaces.
  • Next.js - The React framework for production.
  • Tailwind CSS - A utility-first CSS framework for creating custom designs.
  • Zod - TypeScript-first schema declaration and validation.