DataForm with Text Editing ✨

Powerful form component with YAML editor and optional JSON Schema validation. Switch between form and text editing modes seamlessly.

Key Features

  • Dual Mode: Switch between form UI and YAML text editor
  • Real-time Sync: Changes in form automatically update YAML and vice versa
  • JSON Schema Validation: Optional schema validation for YAML content
  • Flexible Fields: Add custom fields in YAML that aren't in the form
  • Error Handling: Clear error messages for invalid YAML or schema violations

Quick Start

Basic Usage:

With JSON Schema Validation:

Live Example

ℹ️ How to Test:

  1. Fill out the form below with some data
  2. Click the "YAML Editor" tab to see your data as YAML
  3. Edit the YAML (try changing values or adding new fields)
  4. Switch back to "Form" tab to see changes reflected
  5. Click "Save" to submit (uses mock API endpoint)

This example uses a mock API endpoint (/api/mock/user) that returns 200 OK for updates and 201 Created for new entries. Schema validation is enabled - try violating the schema to see error messages.

JSON Schema

The form uses the following JSON Schema for validation:

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "minLength": 2,
      "maxLength": 50
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "age": {
      "type": "number",
      "minimum": 18,
      "maximum": 120
    },
    "bio": {
      "type": "string",
      "maxLength": 500
    },
    "subscribe": {
      "type": "boolean"
    },
    "role": {
      "type": "string",
      "enum": [
        "user",
        "admin",
        "moderator"
      ]
    }
  },
  "required": [
    "name",
    "email",
    "age",
    "role"
  ],
  "additionalProperties": true
}

Mock API Endpoint

This demo uses a Next.js API route at /api/mock/user that simulates a real backend:

MethodStatusDescription
GET200 OKReturns current user data
POST201 CreatedCreates new user (simulated)
PUT200 OKUpdates existing user
DELETE200 OKResets to default data

💡 The API stores data in memory, so it resets when the server restarts.

Advanced Usage

Adding Custom Fields in YAML

You can add fields that aren't defined in the form by switching to the YAML Editor:

Schema Validation Rules

  • Name must be 2-50 characters long
  • Email must be a valid email format
  • Age must be between 18 and 120
  • Bio can be up to 500 characters
  • Role must be one of: user, admin, moderator
  • Additional properties are allowed (additionalProperties: true)

Props Reference

PropTypeDefaultDescription
allowTextEditingbooleanfalseEnable dual mode with form and YAML editor tabs
formJSONSchemaobjectundefinedOptional JSON Schema for validating YAML content
fieldsFormField<T>requiredForm field definitions
apiUrlstringundefinedAPI endpoint for fetching data
submitApiUrlstringundefinedAPI endpoint for submitting (defaults to apiUrl)
responsePropstringundefinedProperty to extract from API response (e.g., "data" for response format like {success: true, data: {...}}). Defaults to "data" if not specified.
onSuccessfunctionundefinedCallback function called on successful submission