Create or change a field: its name, kind, choices and colors, number format, link or formula.
- StatusSelect
- PrioritySelect
- AssigneePerson
- LabelsMulti-select
- EstimateNumber
- DueDate
- CustomerRelation
- ImpactRating
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { FieldEditor } from "./ui/uicontext/components/kit/FieldEditor.ts"import { kindGlyph, kindLabel, Property } from "./ui/uicontext/components/kit/Property.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"import { issueFields } from "./issues.ts"// The issue fields with an Edit button each, and New field, beside the editor drawn in place// (`inline`); without it the editor opens in a dialog. The editor asks for the whole field on Save// (RequestedSaveField); the app saves it into its fields and answers through `outcome`.const styles = stylex.create({ layout: { display: "grid", gridTemplateColumns: { default: "minmax(0, 1fr)", "@container (min-width: 720px)": "minmax(0, 280px) minmax(0, 1fr)" }, alignItems: "start", gap: space.xl, inlineSize: "100%", }, frame: { containerType: "inline-size", inlineSize: "min(100%, 860px)" }, stack: { display: "grid", gap: space.lg, justifyItems: "start", minInlineSize: 0 }, selected: { backgroundColor: color.hover }, idle: { display: "grid", placeItems: "center", minBlockSize: 160, margin: 0, borderWidth: 1, borderStyle: "dashed", borderColor: color.line, borderRadius: radius.lg, color: color.muted, fontSize: font.sm, }, list: { display: "flex", flexDirection: "column", margin: 0, padding: space.xs, listStyle: "none", inlineSize: "100%", boxSizing: "border-box", borderRadius: radius.lg, borderWidth: 1, borderStyle: "solid", borderColor: color.line, backgroundColor: color.panel, }, item: { display: "flex", alignItems: "center", gap: space.md, blockSize: 36, paddingInline: space.md, borderRadius: radius.md, backgroundColor: { default: "transparent", ":hover": color.hover }, }, glyph: { inlineSize: 18, flexShrink: 0, color: color.faint, fontSize: font.xs, textAlign: "center" }, name: { flexGrow: 1, minInlineSize: 0, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", fontSize: font.sm }, kind: { color: color.faint, fontSize: font.xs },})const Message = UiSchema.TaggedEnum({ ClickedEditField: { key: Schema.NullOr(Schema.String) }, RequestedSaveIssueField: { request: Schema.String, field: Property }, RequestedDeleteIssueField: { request: Schema.String, key: Schema.String }, RequestedCloseFieldEditor: {},})export const IssueFields = Html.component({ name: "IssueFields", model: Schema.Struct({ fields: Schema.Array(Property), /** the field being edited: its key, "" for a new one, null when closed */ editing: Schema.NullOr(Schema.String), outcome: Schema.NullOr(Outcome), }), message: Message,}).make({ init: () => ({ fields: issueFields, editing: "status", outcome: null }), update: { ClickedEditField: ($, { key }) => $.evolve({ editing: key ?? "", outcome: null }), // a real app saves first; this one takes every change at once RequestedSaveIssueField: ($, { request, field }) => $.evolve({ fields: (fields) => (fields.some((f) => f.key === field.key) ? fields.map((f) => (f.key === field.key ? field : f)) : [...fields, field]), outcome: { _tag: "Accepted", request }, }), RequestedDeleteIssueField: ($, { request, key }) => $.evolve({ fields: (fields) => fields.filter((f) => f.key !== key), outcome: { _tag: "Accepted", request } }), RequestedCloseFieldEditor: ($) => $.evolve({ editing: null }), }, view: (h, { fields, editing, outcome }) => h.div.attrs(sx(styles.frame)).children(h.div.attrs(sx(styles.layout)).children(h.div.attrs(sx(styles.stack)).children( Button(h, { tone: "primary" }).on("click", Message.ClickedEditField({ key: null })).children("New field"), h.ul.attrs({ "aria-label": "Issue fields", ...sx(styles.list) }).children( ...fields.map((f) => h.li.key(f.key).attrs(sx(styles.item, editing === f.key && styles.selected)).children( h.span.attrs({ "aria-hidden": "true", ...sx(styles.glyph) }).children(kindGlyph[f.kind]), h.span.attrs(sx(styles.name)).children(f.label), h.span.attrs(sx(styles.kind)).children(kindLabel[f.kind]), Button(h, { size: "sm", emphasis: "ghost" }) .attrs({ "aria-label": `Edit ${f.label}` }) .on("click", Message.ClickedEditField({ key: f.key })) .children("Edit"), ), ), ), ), editing === null && h.p.attrs(sx(styles.idle)).children("Choose a field to edit, or add one."), // keyed by the field, so choosing another field starts a fresh draft FieldEditor(h, { key: editing ?? "closed", props: { inline: true, open: editing !== null, field: fields.find((f) => f.key === editing) ?? null, fields, targets: [{ value: "Companies", label: "Companies" }], outcome, can: ["delete"], }, onEmit: { RequestedSaveField: (e) => Message.RequestedSaveIssueField(e), RequestedDeleteField: (e) => Message.RequestedDeleteIssueField(e), RequestedClose: () => Message.RequestedCloseFieldEditor(), }, }), )),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/field-editorUsage
import type { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { Property } from "./ui/uicontext/components/domain/Record.ts"import { FieldEditor } from "./ui/uicontext/components/kit/FieldEditor.ts"const Message = UiSchema.TaggedEnum({ RequestedSaveField: { request: Schema.String, field: Property }, ClosedFieldEditor: {},})// `editing` is the field being changed, or null for a new one; `open` shows the dialog.export const EditField = ( h: Html.H<typeof Message.Type>, open: boolean, editing: Property | null, fields: ReadonlyArray<Property>, outcome: Outcome | null,) => FieldEditor(h, { props: { open, field: editing, fields, outcome }, onEmit: { // save the field, then answer with { _tag: "Accepted", request } as `outcome` RequestedSaveField: (save) => Message.RequestedSaveField(save), RequestedDeleteField: () => undefined, RequestedClose: () => Message.ClosedFieldEditor(), }, })Examples
A refused change
Open Status and press Delete field: the app refuses, and the editor says why.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { FieldEditor } from "./ui/uicontext/components/kit/FieldEditor.ts"import { Property } from "./ui/uicontext/components/kit/Property.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { issueFields } from "./issues.ts"// The app decides. Here it accepts changes to Status but refuses to delete it, because the team's// board is grouped by it; the editor stays open and shows the reason.const styles = stylex.create({ row: { display: "flex", flexWrap: "wrap", gap: 8, justifyContent: "center" },})const Message = UiSchema.TaggedEnum({ ClickedEdit: { key: Schema.String }, RequestedSave: { request: Schema.String, field: Property }, RequestedDelete: { request: Schema.String, key: Schema.String }, Closed: {},})export const ProtectedField = Html.component({ name: "ProtectedField", model: Schema.Struct({ fields: Schema.Array(Property), editing: Schema.NullOr(Schema.String), outcome: Schema.NullOr(Outcome) }), message: Message,}).make({ init: () => ({ fields: issueFields, editing: null, outcome: null }), update: { ClickedEdit: ($, { key }) => $.evolve({ editing: key, outcome: null }), RequestedSave: ($, { request, field }) => $.evolve({ fields: (fields) => fields.map((f) => (f.key === field.key ? field : f)), outcome: { _tag: "Accepted", request }, }), RequestedDelete: ($, { request, key }) => key === "status" ? $.evolve({ outcome: { _tag: "Refused", request, reason: "The team board is grouped by Status, so it can't be deleted." } }) : $.evolve({ fields: (fields) => fields.filter((f) => f.key !== key), outcome: { _tag: "Accepted", request }, editing: null }), Closed: ($) => $.evolve({ editing: null }), }, view: (h, { fields, editing, outcome }) => h.div.attrs(sx(styles.row)).children( Button(h, { emphasis: "outline" }).on("click", Message.ClickedEdit({ key: "status" })).children("Edit Status"), Button(h, { emphasis: "outline" }).on("click", Message.ClickedEdit({ key: "estimate" })).children("Edit Estimate"), FieldEditor(h, { props: { agentId: "field-editor:protected", open: editing !== null, field: fields.find((f) => f.key === editing) ?? null, fields, outcome, can: ["delete"] }, onEmit: { RequestedSaveField: (e) => Message.RequestedSave(e), RequestedDeleteField: (e) => Message.RequestedDelete(e), RequestedClose: () => Message.Closed(), }, }), ),})Choices and their colors
Reorder, recolor, add and remove a select's choices. Removing one, or changing the kind, warns what happens to existing values.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import { Schema } from "effect"import { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { Property } from "./ui/uicontext/components/domain/Record.ts"import { FieldEditor } from "./ui/uicontext/components/kit/FieldEditor.ts"// A pipeline's Stage field, edited in place. Type a choice and press Enter for the next one; drag a// choice by its handle, or press Alt+Up or Alt+Down, to move it; click its dot for a color. Remove a// choice or change the kind, and the editor says what happens to deals that use it before you save.const stage: Property = { key: "stage", label: "Stage", kind: "select", description: "Where the deal is in the pipeline", options: [ { value: "lead", label: "Lead", tone: "gray" }, { value: "qualified", label: "Qualified", tone: "blue" }, { value: "proposal", label: "Proposal", tone: "purple" }, { value: "won", label: "Won", tone: "green" }, { value: "lost", label: "Lost", tone: "red" }, ],}const others: ReadonlyArray<Property> = [ { key: "owner", label: "Owner", kind: "person" }, { key: "amount", label: "Amount", kind: "number", format: { style: "currency", currency: "USD" } },]const Message = UiSchema.TaggedEnum({ RequestedSaveStage: { request: Schema.String, field: Property }, RequestedCloseStage: {},})export const StageChoices = Html.component({ name: "StageChoices", model: Schema.Struct({ field: Property, outcome: Schema.NullOr(Outcome) }), message: Message,}).make({ init: () => ({ field: stage, outcome: null }), update: { // a real app saves the field and converts its values; this one accepts at once RequestedSaveStage: ($, { request, field }) => $.evolve({ field, outcome: { _tag: "Accepted", request } }), // the editor is always shown here: closing starts it over on the saved field RequestedCloseStage: ($) => $.evolve({ outcome: null }), }, view: (h, { field, outcome }) => FieldEditor(h, { props: { agentId: "field-editor:stage", open: true, inline: true, field, fields: [field, ...others], outcome, locale: "en-US", }, onEmit: { RequestedSaveField: ({ request, field }) => Message.RequestedSaveStage({ request, field }), RequestedDeleteField: () => undefined, RequestedClose: () => Message.RequestedCloseStage(), }, }),})API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
openrequired | boolean | — | Whether the editor is showing; closing it comes back as RequestedClose |
fieldrequired | Property | null | — | The field to change; null makes a new one |
fieldsrequired | Property[] | — | Every field of the collection: names must differ, and rollups and formulas refer to them |
targets | FieldTarget[] | — | What a relation can link to |
outcome | Outcome | null | — | The app's answer to the latest save or delete request |
can | Can(["delete"]) | — | "delete" offers Delete field when editing |
locale | string | — | The locale numbers and dates are formatted in; the browser's by default |
agentId | string | field-editor | This editor's id for agents, unique on the page; field-editor by default |
inline | boolean | — | Draw the editor in place, as a panel in the page, instead of in a dialog: for a settings page or a side panel. open still decides whether it shows. |
Events
| Event | Payload | Description |
|---|---|---|
RequestedSaveField | { request: string; field: Property } | Save this field (a new one when its key is new); answer through outcome with the same request |
RequestedDeleteField | { request: string; key: string } | Delete the field; answer through outcome with the same request |
RequestedClose | {} | Close the editor: cancelled, dismissed, or the app accepted the change |
Agent actions
| Action | Description |
|---|---|
ProposedField | Fill in the whole draft: { label, kind, description?, options?: [{ value, label, tone? }], format?: { style, currency?, decimals? }, max?, relation?: { target, multiple? }, rollup?: { relation, field, aggregate }, formula?, result? } |
UpdatedName | Rename the field |
PickedKind | Change the field's kind |
SubmittedField | Save the field as drafted |
ClickedConfirmDeleteasks first | Delete the field and every record's value in it |
ClickedCancel | Close without saving |
Accessibility
- A modal dialog titled for the field; its name field takes focus when it opens, and Escape closes it without saving.
- Each choice has a labelled text field, Move up, Move down and Remove buttons named for it, and a color picker whose swatches say their color.
- Problems and refusals are alerts, and the preview is announced as it changes. Deleting a field asks first, inline.
- Agents rename, change the kind, save or delete; deleting is marked as consequential.