Skip to content

Form components

Process Focus provides default input components for Effect TS Schema fields, but allows selecting a more appropriate match.

Let’s start with the simplest form, a pure Effect TS Schema:

import { Schema } from "effect"
const formSchema = Schema.Struct({
name: Schema.String,
quantity: Schema.Number,
})

It looks like this:

Basic Form Demo dialog with a text input for name and a number input for quantity.

Use TextField and NumberField from @pf/form-schema to set the labels and add instructions below each input. The submitted field names stay name and quantity.

import { NumberField, TextField } from "@pf/form-schema"
import { Schema } from "effect"
const formSchema = Schema.Struct({
name: TextField({
label: "Item name",
description: "Enter the name of the item you want to order.",
}),
quantity: NumberField({
label: "Quantity",
description: "Enter how many units you need.",
}),
})

Basic Form Demo dialog with Item name and Quantity labels and instructions below both inputs.

A TextField is just a wrapper around an Schema.String and makes it easier to specify properties like a label and a description. It makes presentation easier, but does not change how the schema is constructed and validated.

Besides making it easier to add labels and descriptions, input fields can also add specific validation. For example use the EmailField to validate that a string only contains valid email addresses.

Process Focus supports the following input components:

Component Use it to collect
TextField A short, single-line value, such as a SKU.
TextArea Multi-line text, such as delivery instructions.
EmailField An email address.
PhoneField A phone number.
HttpsUrlField An HTTPS URL.
NumberField A number, such as an order quantity.
DateField A date.
LocalDateTimeField A local date and time.
BooleanField A yes-or-no value.
RadioField One choice from a set of options.
CheckboxGroupField Multiple choices from a set of options.
LookupField An existing record selected from search results.
SuggestionField A suggested value or text entered by the user.
CalendarSlotField An available time slot from a calendar.
ProviderUserField An existing Process Focus user.
FileField An uploaded file.

Use LookupField when the person must select an existing record. For example, let them search for a customer by name and select the matching record. The form submits the customer’s ID, not the name they typed. Editing the text after selecting a result clears the selection.

For a small customer list, declare the field in a browser-safe schema:

src/customer-form.ts
import { LookupField } from "@pf/form-schema"
import { Schema } from "effect"
export const customerForm = Schema.Struct({
customerId: LookupField({
label: "Customer",
mode: "local",
maxItems: 10,
}),
})

In your process definition, attach the schema to a form step and supply the choices on the server:

import { Form } from "@pf/process"
import { Effect } from "effect"
import { customerForm } from "./customer-form"
const selectCustomer = new Form(process, "Select customer", {
role: employee,
form: customerForm,
browser: {
entrypoint: "src/customer-form.ts",
exportName: "customerForm",
},
})
selectCustomer.lookups.customerId.setQuery(() =>
Effect.succeed([
{ value: "cust-001", label: "Acme Supplies" },
{ value: "cust-002", label: "Harbour Office" },
]),
)

Selecting Acme Supplies submits "customerId": "cust-001".

Use SuggestionField when the person can enter a value that isn’t in the list. For example, suggest common delivery instructions but let the person write their own. The form submits the selected suggestion’s value or the text they entered.

To add delivery instructions to the same customer form, import SuggestionField from @pf/form-schema and add this field to customerForm:

deliveryInstructions: SuggestionField({ label: "Delivery instructions" }),

Keep the same Form and browser configuration. In the process definition, import searchLookupItems from @pf/form-schema and add a query for the new field:

const deliverySuggestions = [
{ value: "Leave at reception", label: "Leave at reception" },
{ value: "Use the loading dock", label: "Use the loading dock" },
]
selectCustomer.lookups.deliveryInstructions.setQuery(({ filter }, limit) =>
Effect.succeed(searchLookupItems(deliverySuggestions, filter, limit)),
)

Selecting Leave at reception submits "Leave at reception". Typing Call on arrival without selecting a suggestion submits "Call on arrival".

  • Local lookup: Use mode: "local" with maxItems for a small set of eligible records. The browser loads the full set and filters it as the person types. If the set exceeds maxItems, the lookup fails to load rather than showing a partial list.
  • Server lookup: Use server search for larger sets. The server searches as the person types. If there are too many matches to show, the person can narrow the search.

In customerForm, replace the local lookup with a server lookup:

customerId: LookupField({ label: "Customer", mode: "server" }),

Replace the earlier setQuery call in the process definition with the following code. The example uses two eligible customers; in a real form, query your data source for customers who are currently eligible.

import { searchLookupItems } from "@pf/form-schema"
const eligibleCustomers = [
{ value: "cust-001", label: "Acme Supplies" },
{ value: "cust-002", label: "Harbour Office" },
]
const findEligibleCustomer = (id: string) =>
Effect.succeed(
eligibleCustomers.find((customer) => customer.value === id) ?? null,
)
selectCustomer.lookups.customerId.setQuery(({ filter }, limit) =>
Effect.succeed(searchLookupItems(eligibleCustomers, filter, limit)),
)
selectCustomer.lookups.customerId.setResolver(findEligibleCustomer)

setQuery returns matches for what the person types, up to limit. setResolver looks up an existing selection by its exact ID. For example, it can return Acme Supplies for a saved cust-001 even when the current search only matches Harbour Office.

To reject a customer who is no longer eligible, add this server-side validation to the new Form(...) options. It checks the submitted ID again:

validate: ({ customerId }) =>
findEligibleCustomer(customerId).pipe(
Effect.map((customer) =>
customer === null ? "Select an eligible customer." : undefined,
),
),

To organise fields into groups, nested objects, or lists, see Form layout.

Process Focus uses Effect Schema annotations under the hood. Using a TextField just makes access to annotations a bit easier.

You can define a field using Effect Schema alone. The Dashboard uses the title annotation as the field label and description as help text. Use check to validate the value:

import { Schema } from "effect"
const formSchema = Schema.Struct({
email: Schema.String
.check(
Schema.isPattern(/^[^\s@]+@[^\s@]+\.[^\s@]+$/, {
message: "Enter a valid email address.",
}),
)
.annotate({
title: "Work email",
description: "Enter the email address you use at work.",
}),
})

This pattern checks for a basic email shape. Use EmailField for more complete email validation and an email-specific input.