Data Driven Forms

repository·master·Indexed 43 days ago

https://github.com/data-driven-forms/react-forms

A React library for rendering and managing forms using a schema-driven approach. It decouples form logic from UI components via mappers, supporting libraries such as Material-UI, Ant Design, PatternFly 4, BlueprintJS, and Carbon. The ecosystem includes @data-driven-forms/react-form-renderer for core rendering and @data-driven-forms/parsers for transforming external schemas into compatible formats.

Tokens
111.4K
Snippets
427
Records
555
Agent score
55%

What's inside Data Driven Forms

  1. Introduction to Data Driven Forms

    master

    Data Driven Forms is a React library designed to render forms and manage their state using a schema-driven approach. Instead of writing manual JSX for every form, you define a form's structure, validation, and logic using a JSON schema.

    Key Features

    • Centralized Implementation: All components and functionality are defined in one place, ensuring consistency across applications.
    • JSON Schemas: Forms are described by JSON objects that can be stored in databases or loaded dynamically from an API.
    • Extensibility: Easily customize forms by changing component types via strings in the schema.
    • Advanced Logic: Built-in support for conditions (hiding/showing fields), actions, custom validators, Wizards, and asynchronous selects.
    • State Management: Uses React Final Form under the hood for robust state and validation management.
    • Component Mappers: Provides ready-to-use mappers for popular UI libraries like MUI, Ant Design, PatternFly, Blueprint, Carbon, and Semantic UI.
  2. Understand the purpose of @data-driven-forms/common

    master

    @data-driven-forms/common is a utility package containing shared code used across multiple @data-driven-forms/* packages. It provides a centralized location for:

    • Shared interfaces and propTypes
    • Common mapper logic, including:
      • wizard
      • select
      • multiplechoice list
      • form template
      • dual list select
    • Demo schemas used for testing and demonstration purposes.
  3. What is a component mapper and how to use it

    master

    A component mapper is an object of React components used by FormRenderer to map schema componentType identifiers to actual React components.

    Each key in the mapper object must be a unique identifier that matches the componentType defined in your form schema. The value for each key can be either:

    1. A React component.
    2. An object containing a component and globally defined props.

    To use a mapper, pass it to the componentMapper prop of the FormRenderer component.

    import { FormRenderer } from '@data-driven-forms/react-form-renderer';
    
    const componentMapper = {
      'new-component': NewComponent
    };
    
    const Form = (props) => (
      <FormRenderer
        componentMapper={componentMapper}
        {...props}
      />
    );
  4. Implement record-level validation

    master

    Record-level validation allows you to define a validation function for the entire form rather than individual fields. This is particularly useful for cross-field validation, where the validity of one field depends on the value of another (e.g., ensuring a 'confirm password' field matches a 'password' field).

    A record-level validator is a function that receives the entire form values object and returns either an object containing error messages or a Promise that resolves to an error object. If the form is valid, it should return undefined or an empty object.

    /*
    Signature:
    (values) => object | Promise<object>
    */
  5. Customize Semantic UI react components

    master

    All components mapped by the Semantic UI react mapper accept all props defined in the original Semantic UI react documentation. You can pass these props directly through your form schema to customize the appearance or behavior of the components.

    To apply common customizations across multiple fields without repeating them in every schema entry, refer to the global component props documentation.

  6. Resolve props at runtime with resolveProps

    master

    The resolveProps utility allows you to dynamically change component props at runtime based on the current state of the form. This is useful when a field's configuration (like disabled, required, or placeholder) depends on the value of another field in the form.

    To ensure the field updates immediately when the dependency changes, you must trigger a re-render of the field being modified. There are two primary strategies for this: using subscription for simplicity or FormSpy for performance optimization.

    // Conceptual usage of resolveProps in a schema
    {
      "type": "text",
      "name": "myField",
      "resolveProps": (formState) => ({
        "disabled": formState.otherField === 'some-value'
      })
    }
  7. Use the experimental MUI Field Array

    master
    The @data-driven-forms/mui-component-mapper provides an experimental implementation of a field-array. This component allows you to manage a dynamic list of fields, supporting both arrays of simple values and arrays of objects. It also includes a 'revert removal' feature that allows users to undo the last removal action.
  8. Set global component props via component mappers

    master

    If you need to apply the same prop value to every component instance within a form, you can achieve this by configuring your component mapper.

    Note: The specific prop names and values you use depend entirely on the component mapper being employed (e.g., Material-UI, Ant Design, etc.), as they must match the props expected by the underlying UI library components.