# Angular FormBuilder Component

FormBuilder is a visual editor of FormRenderer schemas: a palette of field types, a canvas with a live preview, drag and drop with a full keyboard alternative, a properties panel for validation, options and conditions, sections, steps, undo and redo, and JSON import and export.

## Basic

FormBuilder edits the schema of a FormRenderer, bound with [(schema)] . Add a field from the palette with a click, or drag it to a place on the canvas; the canvas shows each field as FormRenderer draws it. Select a field to edit its label, key, hint, validation, options and conditions in the properties panel, and drag it or use the move buttons to reorder it. The toolbar has undo and redo, a live preview of the whole form and a JSON view to copy, download or import the schema. On narrow screens, the palette, the canvas and the properties become tabs.

```typescript
import { Component, signal } from '@angular/core';
import { FormBuilderModule } from '@exhaustive-ui/core/formbuilder';
import { FormSchema } from '@exhaustive-ui/core/formrenderer';

@Component({
    template: `
        <div class="card flex flex-col gap-4">
            <p-form-builder [(schema)]="schema" />
            <details>
                <summary class="cursor-pointer">Schema</summary>
                <pre class="text-sm font-mono m-0 mt-2 p-3 rounded-border bg-surface-100 dark:bg-surface-800 overflow-auto">{{ json(schema()) }}</pre>
            </details>
        </div>
    `,
    standalone: true,
    imports: [FormBuilderModule]
})
export class FormbuilderBasicDemo {
    schema = signal<FormSchema>({
        version: 1,
        title: 'Event registration',
        columns: 2,
        fields: [
            { key: 'name', type: 'text', label: 'Name', required: true },
            { key: 'email', type: 'email', label: 'Email', required: true },
            { key: 'ticket', type: 'radio', label: 'Ticket', options: ['Standard', 'VIP'], default: 'Standard', colSpan: 2 },
            { key: 'dinner', type: 'checkbox', label: 'I join the dinner' }
        ]
    });

    json(value: unknown) {
        return JSON.stringify(value, null, 2);
    }
}
```

## Builder Customization

types limits the palette to the allowed field types, in their order. palette replaces it with your own items: a type , a label and defaults merged into the new field, so a preset such as a country list or a consent checkbox is one click away. validatorNames lists the custom validators the properties panel offers, and validators and loaders give their functions to the previews. showPreview and showJson hide the views of the toolbar, breakpoint sets the width under which the panels become tabs, and every text is replaced or translated with labels or the formBuilder key of the locale texts.

```typescript
import { Component, signal } from '@angular/core';
import { FormBuilderModule, FormBuilderPaletteItem } from '@exhaustive-ui/core/formbuilder';
import { FormRendererValidatorFn, FormSchema } from '@exhaustive-ui/core/formrenderer';

@Component({
    template: `
        <div class="card">
            <p-form-builder [(schema)]="schema" [palette]="palette" [validatorNames]="['even']" [validators]="validators" [showJson]="false" ariaLabel="Survey builder" />
        </div>
    `,
    standalone: true,
    imports: [FormBuilderModule]
})
export class FormbuilderCustomizationDemo {
    schema = signal<FormSchema>({ version: 1, fields: [{ key: 'satisfaction', type: 'rating', label: 'How satisfied are you?', props: { stars: 5 } }] });

    palette: FormBuilderPaletteItem[] = [
        { type: 'text', label: 'Short answer' },
        { type: 'textarea', label: 'Long answer' },
        { type: 'radio', label: 'Single choice' },
        { type: 'multiselect', label: 'Multiple choice' },
        { type: 'rating', label: 'Rating', defaults: { props: { stars: 5 } } },
        { type: 'select', label: 'Country', defaults: { key: 'country', label: 'Country', options: ['France', 'Germany', 'Italy', 'Spain'] } },
        { type: 'checkbox', label: 'Consent', defaults: { key: 'consent', label: 'I agree to be contacted', required: true } }
    ];

    validators: Record<string, FormRendererValidatorFn> = {
        even: (value) => (value == null || value % 2 === 0 ? null : 'Enter an even number.')
    };
}
```

## Steps and Rendering

The schema is plain JSON: save it to a server, then render it anywhere with FormRenderer. Steps and sections are managed from the canvas header with Add section and Add step ; select a section or a step to edit its title, description, columns and visibility. Here the form below follows the builder as you edit it.

```typescript
import { Component, signal } from '@angular/core';
import { FormBuilderModule } from '@exhaustive-ui/core/formbuilder';
import { FormRendererModule, FormSchema } from '@exhaustive-ui/core/formrenderer';

@Component({
    template: `
        <div class="card flex flex-col gap-6">
            <p-form-builder [(schema)]="schema" [showPreview]="false" ariaLabel="Steps builder" />
            <p-form-renderer [schema]="schema()" [headingLevel]="3" />
        </div>
    `,
    standalone: true,
    imports: [FormBuilderModule, FormRendererModule]
})
export class FormbuilderRenderDemo {
    schema = signal<FormSchema>({
        version: 1,
        title: 'Job application',
        steps: [
            {
                id: 'about',
                title: 'About you',
                sections: [
                    {
                        id: 'identity',
                        columns: 2,
                        fields: [
                            { key: 'name', type: 'text', label: 'Name', required: true },
                            { key: 'email', type: 'email', label: 'Email', required: true }
                        ]
                    }
                ]
            },
            {
                id: 'experience',
                title: 'Experience',
                sections: [
                    {
                        id: 'skills',
                        fields: [
                            { key: 'years', type: 'slider', label: 'Years of experience', default: 3, maximum: 30 },
                            { key: 'cv', type: 'file', label: 'Resume', required: true }
                        ]
                    }
                ]
            }
        ]
    });
}
```

## Accessibility

Screen Reader FormBuilder is a region named by ariaLabel . The palette, the canvas and the properties are titled sections, and every action of drag and drop has a button: the palette items add a field after the selection, and each field has buttons to move it up or down, duplicate it and remove it, named after the field, such as Move Email, Email down . A field of the canvas is a toggle button with aria-pressed when it is selected, and its preview is inert , so it is neither focused nor read twice. Adding, moving, removing a field, undo and redo are announced by a polite live region, such as Email, Email moved to position 2 , and the focus follows the field or goes to the next one after a removal. The key of a field is checked as you type, with an error linked to its input when another field uses it.

## Pass Through Options

| Name | Type | Description |
|------|------|-------------|
| host | PassThroughOption<HTMLElement, I> | Used to pass attributes to the host's DOM element. |
| root | PassThroughOption<HTMLElement, I> | Used to pass attributes to the root's DOM element. |
| toolbar | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the toolbar. |
| paneSwitch | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the switch between the panels on narrow screens. |
| palette | PassThroughOption<HTMLElement, I> | Used to pass attributes to the palette. |
| paletteItem | PassThroughOption<HTMLButtonElement, I> | Used to pass attributes to each item of the palette. |
| canvas | PassThroughOption<HTMLElement, I> | Used to pass attributes to the canvas. |
| containerHeader | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the header of each step and section of the canvas. |
| fieldList | PassThroughOption<HTMLUListElement, I> | Used to pass attributes to each list of fields of the canvas. |
| fieldItem | PassThroughOption<HTMLLIElement, I> | Used to pass attributes to each field of the canvas. |
| fieldPreview | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the preview of each field of the canvas. |
| properties | PassThroughOption<HTMLElement, I> | Used to pass attributes to the properties panel. |
| panelTitle | PassThroughOption<HTMLHeadingElement, I> | Used to pass attributes to the title of each panel. |
| pcFormRenderer | FormRendererPassThrough | Used to pass attributes to the FormRenderer of the previews. |
| pcFilterBuilder | FilterBuilderPassThrough | Used to pass attributes to the FilterBuilder of the conditions. |
| pcJsonViewer | JsonViewerPassThrough | Used to pass attributes to the JsonViewer of the JSON view. |
| actionButton | ButtonPassThrough | Used to pass attributes to the buttons of the builder. |
| emptyMessage | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the empty message of a list of fields. |
| liveRegion | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to the live region. |

## Theming

### CSS Classes

| Class | Description |
|-------|-------------|
| p-formbuilder | Class name of the root element |
| p-formbuilder-toolbar | Class name of the toolbar |
| p-formbuilder-palette | Class name of the palette |
| p-formbuilder-palette-item | Class name of an item of the palette |
| p-formbuilder-canvas | Class name of the canvas |
| p-formbuilder-field-list | Class name of a list of fields of the canvas |
| p-formbuilder-field-item | Class name of a field of the canvas |
| p-formbuilder-field-preview | Class name of the preview of a field of the canvas |
| p-formbuilder-properties | Class name of the properties panel |
| p-formbuilder-empty-message | Class name of the empty message of a list of fields |

### Design Tokens

| Token | CSS Variable | Description |
|-------|--------------|-------------|
| formbuilder.root.color | --p-formbuilder-root-color | Text color of root |
| formbuilder.root.gap | --p-formbuilder-root-gap | Gap between the toolbar and the panels |
| formbuilder.toolbar.gap | --p-formbuilder-toolbar-gap | Gap between the buttons of the toolbar |
| formbuilder.panel.background | --p-formbuilder-panel-background | Background of the palette, the canvas and the properties |
| formbuilder.panel.border.color | --p-formbuilder-panel-border-color | Border color of a panel |
| formbuilder.panel.border.radius | --p-formbuilder-panel-border-radius | Border radius of a panel |
| formbuilder.panel.padding | --p-formbuilder-panel-padding | Padding of a panel |
| formbuilder.panel.gap | --p-formbuilder-panel-gap | Gap between the items of a panel |
| formbuilder.panel.title.font.size | --p-formbuilder-panel-title-font-size | Font size of the title of a panel |
| formbuilder.panel.title.font.weight | --p-formbuilder-panel-title-font-weight | Font weight of the title of a panel |
| formbuilder.panel.title.color | --p-formbuilder-panel-title-color | Color of the title of a panel |
| formbuilder.palette.item.background | --p-formbuilder-palette-item-background | Background of an item of the palette |
| formbuilder.palette.item.hover.background | --p-formbuilder-palette-item-hover-background | Background of an item of the palette on hover |
| formbuilder.palette.item.border.color | --p-formbuilder-palette-item-border-color | Border color of an item of the palette |
| formbuilder.palette.item.border.radius | --p-formbuilder-palette-item-border-radius | Border radius of an item of the palette |
| formbuilder.palette.item.padding | --p-formbuilder-palette-item-padding | Padding of an item of the palette |
| formbuilder.palette.item.color | --p-formbuilder-palette-item-color | Text color of an item of the palette |
| formbuilder.canvas.background | --p-formbuilder-canvas-background | Background of the canvas |
| formbuilder.canvas.min.height | --p-formbuilder-canvas-min-height | Minimum height of the canvas |
| formbuilder.canvas.field.background | --p-formbuilder-canvas-field-background | Background of a field of the canvas |
| formbuilder.canvas.field.border.color | --p-formbuilder-canvas-field-border-color | Border color of a field of the canvas |
| formbuilder.canvas.field.hover.border.color | --p-formbuilder-canvas-field-hover-border-color | Border color of a field of the canvas on hover |
| formbuilder.canvas.field.selected.border.color | --p-formbuilder-canvas-field-selected-border-color | Border color of the selected field |
| formbuilder.canvas.field.border.radius | --p-formbuilder-canvas-field-border-radius | Border radius of a field of the canvas |
| formbuilder.canvas.field.padding | --p-formbuilder-canvas-field-padding | Padding of a field of the canvas |
| formbuilder.canvas.field.gap | --p-formbuilder-canvas-field-gap | Gap between the parts of a field of the canvas |
| formbuilder.placeholder.border.color | --p-formbuilder-placeholder-border-color | Border color of the drop placeholder |
| formbuilder.placeholder.background | --p-formbuilder-placeholder-background | Background of the drop placeholder |
| formbuilder.empty.message.color | --p-formbuilder-empty-message-color | Color of the empty message |
| formbuilder.empty.message.padding | --p-formbuilder-empty-message-padding | Padding of the empty message |

