Field editor
One field: name, type, the options of its type, required, unique, its default value.
import { FieldEditor } from "@forgeron/ui";import { FieldEditor } from "@forgeron/ui";
import { useState } from "react";
export default function Default() {
const [saved, setSaved] = useState("");
return (
<div style={{ width: 380, display: "grid", gap: 8 }}>
<FieldEditor
defaultValue={{ id: "f1", name: "due_date", type: "date", required: true }}
takenNames={["id", "name"]}
onSubmit={(field) => setSaved(`${field.name}: ${field.type}`)}
/>
{saved ? <code style={{ font: "400 11px/16px var(--font-mono)" }}>{saved}</code> : null}
</div>
);
}// The choices of a select: Enter adds the next one, Backspace in an empty one removes it, the
// square cycles its colour.
import { FieldEditor, type DataField } from "@forgeron/ui";
import { useState } from "react";
export default function SelectChoices() {
const [field, setField] = useState<DataField>({
id: "stage",
name: "stage",
type: "select",
description: "Where the deal stands.",
choices: [
{ value: "lead", label: "Lead", color: "var(--ink-dim)" },
{ value: "quote", label: "Quote", color: "var(--warning)" },
{ value: "won", label: "Won", color: "var(--success)" },
],
defaultValue: "lead",
});
return (
<div style={{ width: 400 }}>
<FieldEditor
value={field}
onChange={setField}
takenNames={["id", "name", "amount"]}
onSubmit={setField}
onCancel={() => {}}
onDelete={() => {}}
/>
</div>
);
}// A relation: the table it points at, one or several records, a default record.
import { FieldEditor } from "@forgeron/ui";
export default function Relation() {
return (
<div style={{ width: 400 }}>
<FieldEditor
defaultValue={{
id: "company",
name: "company_id",
type: "relation",
relation: { table: "companies" },
}}
tables={[
{ value: "companies", label: "Companies" },
{ value: "contacts", label: "Contacts" },
{ value: "invoices", label: "Invoices" },
]}
submitLabel="Add field"
onSubmit={() => {}}
onCancel={() => {}}
/>
</div>
);
}API → Props
| Prop | Type | Default | Description |
|---|---|---|---|
value / defaultValue | DataField | — | The field. Controlled or not. |
onChange | (field) => void | — | Every change. |
onSubmit | (field) => void | — | Once the name, the choices and the default are valid. |
onCancel / onDelete | () => void | — | Buttons in the foot. |
tables | { value, label }[] | — | The tables a relation can point at. |
relations | Record<table, { id, label, hint? }[]> | — | The records relation fields can point at, by table: their labels and the picker. |
takenNames | string[] | — | The other fields' names: a name must not repeat one. |
types | FieldType[] | all | The types offered. |
namePattern | RegExp | null | snake_case | The shape of a name; null for any. |
submitLabel | string | "Save field" | The words of the save button. |
className | string | — | Extra classes. |