Form / foldkit-mixins-form

mixins-form

Writing form markup by hand for every schema field—wiring <label for>, aria-invalid, aria-describedby, role="alert" error summaries, nested array controls, and pending submit states—is repetitive and easy to get subtly wrong.

foldkit-mixins-form renders any foldkit-form declaration into accessible HTML with every element published as a foldkit-mixins Slot. The form controls drafts, validation, and Messages; this package renders the markup and accessibility attributes; your Style and Behavior attachments customize how fields look and behave. See forms running live in the Chirp demo.

Install

sh
pnpm add effect foldkit foldkit-bundle foldkit-form foldkit-mixins foldkit-mixins-form

A first form view

Define an Entity-backed form and attach a view to its Bundle with FormView.submodel and FormView.define:

ts
import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Entity } from 'foldkit-entity'
import { Form as FormKit } from 'foldkit-form'
import { FormView } from 'foldkit-mixins-form'

const Post = Entity.define(
  'Post',
  Schema.Struct({
    id: Schema.String,
    title: Schema.String.check(Schema.isMinLength(1)).annotate({ title: 'Title' }),
  }),
)
const Edit = FormKit.make('Edit', Entity.input(Post, Schema.Struct({ title: Post.fields.title.schema })))

const Drawn = Edit.bundle.pipe(
  Bundle.withView(FormView.submodel(Edit, FormView.define(Edit))),
)

Drawn is a complete Bundle you place in a parent Model with your onOut handler. Rendering it draws a <form> containing a labelled <input id="Edit-title" type="text"> and an accessible submit button (aria-disabled while invalid); typing and submitting route Messages (Changed, Blurred, Submitted, and siblings) back to the form's reducer, and submitting a valid form emits the decoded value to onOut.

Two Slot contracts separate form-level and field-level styling:

  • FormSlots wraps the whole form (root, errors, failure, summary, submit, and nested row controls).
  • FieldSlots wraps each individual field (root, label, description, checking, error, and control slots) and receives that field's FieldInput (such as input.invalid) so Styles can react to field validity.

All view state stays in foldkit-form and your parent Model. For styling through foldkit-mixins-ui components instead of plain HTML controls, use the optional foldkit-mixins-form/ui subpath.