Skip to content

React RuleBuilder UI (@qarar/react-builder)

@qarar/react-builder provides a visual rule authoring component (<RuleBuilder />) designed for embedding into internal admin portals, SaaS dashboards, and workflow editors.


1. Basic Usage

The <RuleBuilder /> is a fully controlled React component:

import React, { useState } from "react";
import { RuleBuilder } from "@qarar/react-builder";
import type { RuleNode } from "@qarar/spec";
export function AdminRuleEditor() {
const [ruleRoot, setRuleRoot] = useState<RuleNode>({
type: "and",
conditions: [
{
type: "gte",
left: { type: "field", path: "customer.age" },
right: { type: "number", value: 18 },
},
],
});
return (
<div className="p-6 max-w-4xl mx-auto bg-white dark:bg-slate-900 rounded-lg shadow">
<h2 className="text-xl font-bold mb-4">Edit Promotional Rule</h2>
<RuleBuilder
value={ruleRoot}
onChange={(updatedNode) => {
setRuleRoot(updatedNode);
console.log("Updated JSON AST:", updatedNode);
}}
/>
</div>
);
}

Users can type any dot-notation field path directly into the field text input (e.g., customer.age, cart.total). You can also optionally pass fields (FieldDefinition[]) if you want to provide type metadata or enum hints.


2. Component Props

PropTypeRequiredDescription
valueRule | RuleNodeYesThe current rule or root node to render and edit.
onChange(node: RuleNode) => voidYesCallback fired whenever a condition or gate is modified.
fieldsFieldDefinition[]NoOptional field definitions for type inference and enum value hints.
themeQararThemeNoOptional CSS variable tokens for custom styling.

3. Visual Decision Table Builder (<DecisionTableBuilder />)

Visual authoring for multi-case switch matrices (DecisionTable):

import { DecisionTableBuilder } from "@qarar/react-builder";
import type { DecisionTable } from "@qarar/spec";
export function AdminMatrixEditor() {
const [table, setTable] = useState<DecisionTable>(myDecisionTable);
return (
<DecisionTableBuilder
value={table}
onChange={(updatedTable) => setTable(updatedTable)}
matchedBranchId="vip-tier" // Optional: highlight active matched branch in real-time
/>
);
}

Component Props

PropTypeDescription
valueDecisionTable<T>The decision table AST to render and edit.
onChange(table: DecisionTable<T>) => voidFired when branches, conditions, or outputs change.
matchedBranchIdstring | nullOptional branch ID to highlight with green MATCHED badge.
themeQararThemeOptional CSS variable tokens for styling.