An operator's add and edit form for a list of settings, with the editor state that opens it.
<div id="cf-editor" data-mount="editor"></div>
Installation
// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/config-form.css";
import "capsomer/tokens.css";
import "capsomer/config-form.css";
import { ConfigForm } from "capsomer/react/config-form";
Usage
import { ConfigForm, useConfigForm, useEditor } from "capsomer/react/config-form";
const ed = useEditor<SiteRow>(() => read());
<button className="cap-btn" data-variant="primary" onClick={ed.add} disabled={ed.editing?.kind === "add"}>Add site</button>
{ed.editing && <SiteForm key={ed.formKey} editing={ed.editing} onClose={ed.close} />}
// a row: onEdit={() => ed.edit(row, row.namespace)}
function SiteForm({ editing, onClose }) {
const { f, first } = useConfigForm();
return (
<ConfigForm f={f} first={first} title="Add a site" hint={hint} onSubmit={preview} onClose={onClose} danger={...}>
<Field label="Name"><input className="cap-input" id={f("name")} ref={first} ... /></Field>
</ConfigForm>
);
}
API reference
| Prop | Type | Required | Does |
|---|
f | (k: string) => string | Yes | |
|---|
first | RefObject<HTMLInputElement | null> | Yes | |
|---|
title | string | Yes | |
|---|
src | ReactNode | | Where the setting lives: "Saved in D1". |
|---|
hint | string | null | Yes | Why the draft cannot be sent yet ("A name is needed"), shown above the buttons and announced. |
|---|
onSubmit | () => void | Yes | |
|---|
onClose | () => void | Yes | |
|---|
submitLabel | string | | The submit's words. "Preview" by default: it opens a confirm that shows what will change, and changes nothing itself. |
|---|
danger | ReactNode | | A Danger area under the buttons (Remove this site), apart from the form's own actions. |
|---|
children | ReactNode | Yes | |
|---|
CSS variables
| Variable | Default |
|---|
--fields-gap | var(--gap) var(--space-6) |
|---|
Parts and data attributes
| Class | Is |
|---|
.cap-config-actions | its own class |
|---|
.cap-config-danger | its own class |
|---|
.cap-config-form | the root |
|---|
.cap-config-full | its own class |
|---|
Accessibility
The panel is a named region (its heading). Fields are Capsomer Fields, labeled and described. The hint is announced when it appears.