Blocks / Condition builder

Condition builder

Field, operator and value rows joined by and / or, with groups; said in one line.

import { ConditionBuilder, describeCondition, evaluateCondition } from "@forgeron/ui";
/01 → Default
Where
Field
Operator for Amount
Field
Operator for Stage
Value of Stage
import { ConditionBuilder, type ConditionField } from "@forgeron/ui";

const fields: ConditionField[] = [
  { id: "deal.amount", label: "Amount", type: "number" },
  {
    id: "deal.stage",
    label: "Stage",
    type: "enum",
    options: [
      { value: "won", label: "Won" },
      { value: "lost", label: "Lost" },
    ],
  },
  { id: "deal.company", label: "Company", type: "text" },
];

export default function Default() {
  return (
    <div style={{ width: 520 }}>
      <ConditionBuilder
        fields={fields}
        maxDepth={1}
        defaultValue={{
          id: "root",
          combinator: "and",
          rules: [
            { id: "a", field: "deal.amount", operator: "gt", value: "5000" },
            { id: "b", field: "deal.stage", operator: "eq", value: "won" },
          ],
        }}
      />
    </div>
  );
}
/02 → Groups and a live test
Where
Field
Operator for Stage
Value of Stage
Where
Field
Operator for Amount
Field
Operator for VIP client

Stage is Won and (Amount is at least 5000 or VIP client is true)
Transalp, €9,600: goes to true

import { TrophyIcon } from "@forgeron/icons";
import {
  ConditionBuilder,
  describeCondition,
  evaluateCondition,
  variablesFromSample,
  type ConditionField,
  type ConditionGroup,
} from "@forgeron/ui";
import { useState } from "react";

const fields: ConditionField[] = [
  { id: "deal.amount", label: "Amount", type: "number" },
  {
    id: "deal.stage",
    label: "Stage",
    type: "enum",
    options: ["Lead", "Proposal", "Won", "Lost"].map((s) => ({ value: s, label: s })),
  },
  { id: "deal.company", label: "Company", type: "text" },
  { id: "deal.closedOn", label: "Closed on", type: "date" },
  { id: "deal.vip", label: "VIP client", type: "boolean" },
];
const deal = {
  amount: 9600,
  stage: "Won",
  company: "Transalp",
  closedOn: "2026-11-17",
  vip: false,
};

// The sentence and the result on the sample follow every change.
export default function GroupsAndALiveTest() {
  const [condition, setCondition] = useState<ConditionGroup>({
    id: "root",
    combinator: "and",
    rules: [
      { id: "a", field: "deal.stage", operator: "eq", value: "Won" },
      {
        id: "g",
        combinator: "or",
        rules: [
          { id: "b", field: "deal.amount", operator: "gte", value: "5000" },
          { id: "c", field: "deal.vip", operator: "is_true" },
        ],
      },
    ],
  });
  const passes = evaluateCondition(condition, { deal });
  return (
    <div style={{ display: "grid", gap: 16, width: "100%", maxWidth: 620 }}>
      <ConditionBuilder
        fields={fields}
        value={condition}
        onChange={setCondition}
        variables={[variablesFromSample("deal", "Deal moved to Won", deal, TrophyIcon)]}
      />
      <p className="t-small" style={{ margin: 0 }}>
        {describeCondition(condition, fields)}
        <br />
        <span className="t-mono dim">
          Transalp, €9,600: {passes ? "goes to true" : "goes to false"}
        </span>
      </p>
    </div>
  );
}
API → Props
PropTypeDefaultDescription
value / defaultValue / onChange{ id, combinator, rules: (Rule | Group)[] }—A rule is { id, field, operator, value? }.
fields{ id, label, type, options?, icon? }[]—type: text, number, date, boolean, enum. The operators and the value follow it.
variables{ id, label, icon?, fields: { path, hint? }[] }[]—Given, text and number values take {{ references }}.
maxDepthnumber2How deep groups go; 1: no group.
readOnlyboolean—Shows without editing: a run, a published version.
classNamestring—Extra classes.