Quick Start

Learn how to build your first form with FormMorf in just a few minutes.

Basic Usage

Here's a simple example of using the FormBuilder component:

tsx
import { FormBuilder } from '@formmorf/builder';
import '@formmorf/builder/dist/style.css';

function App() {
  const handleSave = (schema) => {
    console.log('Form schema:', schema);
    // Save to your backend or state management
  };

  return (
    <FormBuilder
      onSave={handleSave}
      initialSchema={{
        title: 'Contact Form',
        description: 'Get in touch with us',
        fields: []
      }}
    />
  );
}

FormViewer for Runtime

Once you have a form schema, use FormViewer to render it for users:

tsx
import { FormViewer } from '@formmorf/builder';
import '@formmorf/builder/dist/style.css';

function ContactPage({ formSchema }) {
  const handleSubmit = (data) => {
    console.log('Form submitted:', data);
    // Process form submission
  };

  return (
    <FormViewer
      schema={formSchema}
      onSubmit={handleSubmit}
    />
  );
}

Complete Example

Here's a full example showing both builder and viewer:

tsx
import { useState } from 'react';
import { FormBuilder, FormViewer, FormSchema } from '@formmorf/builder';
import '@formmorf/builder/dist/style.css';

function FormManager() {
  const [schema, setSchema] = useState<FormSchema>({
    title: 'My Form',
    description: '',
    fields: []
  });
  const [mode, setMode] = useState<'build' | 'preview'>('build');

  const handleSave = (newSchema: FormSchema) => {
    setSchema(newSchema);
    console.log('Schema saved:', newSchema);
  };

  const handleSubmit = (data: Record<string, any>) => {
    console.log('Form data:', data);
    alert('Form submitted! Check console for data.');
  };

  return (
    <div>
      <div className="mb-4">
        <button
          onClick={() => setMode('build')}
          className={mode === 'build' ? 'font-bold' : ''}
        >
          Build Mode
        </button>
        <button
          onClick={() => setMode('preview')}
          className={mode === 'preview' ? 'font-bold' : ''}
        >
          Preview Mode
        </button>
      </div>

      {mode === 'build' ? (
        <FormBuilder
          initialSchema={schema}
          onSave={handleSave}
        />
      ) : (
        <FormViewer
          schema={schema}
          onSubmit={handleSubmit}
        />
      )}
    </div>
  );
}

export default FormManager;

With Initial Fields

You can provide a pre-configured schema with fields:

tsx
const initialSchema = {
  title: 'Contact Form',
  description: 'We would love to hear from you',
  fields: [
    {
      id: '1',
      type: 'text',
      label: 'Full Name',
      name: 'fullName',
      required: true,
      placeholder: 'Enter your name'
    },
    {
      id: '2',
      type: 'email',
      label: 'Email Address',
      name: 'email',
      required: true,
      placeholder: 'you@example.com'
    },
    {
      id: '3',
      type: 'textarea',
      label: 'Message',
      name: 'message',
      required: true,
      placeholder: 'Your message here...',
      rows: 5
    }
  ]
};

<FormBuilder initialSchema={initialSchema} onSave={handleSave} />

Next.js Integration

For Next.js projects, make sure to use client components:

tsx
'use client';

import { FormBuilder } from '@formmorf/builder';
import '@formmorf/builder/dist/style.css';

export default function BuilderPage() {
  return <FormBuilder {...props} />;
}

💡 Tips

  • • The FormBuilder component is fully controlled - save the schema to your state or backend
  • • FormViewer is lightweight and can render any schema created by FormBuilder
  • • All components work with Material-UI themes out of the box
  • • Use TypeScript for better autocomplete and type safety

Explore Components

Learn more about the FormBuilder and FormViewer components

Component Documentation