Blocks / Field editor

Field editor

One field: name, type, the options of its type, required, unique, its default value.

import { FieldEditor } from "@forgeron/ui";
/01 → Default
Type
Rules
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>
  );
}
/02 → Select choices
Type
Choices
Rules
Default value
// 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>
  );
}
/03 → Relation
Type
Points at
Rules
// 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
PropTypeDefaultDescription
value / defaultValueDataField—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.
relationsRecord<table, { id, label, hint? }[]>—The records relation fields can point at, by table: their labels and the picker.
takenNamesstring[]—The other fields' names: a name must not repeat one.
typesFieldType[]allThe types offered.
namePatternRegExp | nullsnake_caseThe shape of a name; null for any.
submitLabelstring"Save field"The words of the save button.
classNamestring—Extra classes.