FormulaBuilder

Basic Usage

Drag variables and operators into the canvas, or click them to append. Reorder tokens by dragging within the canvas. Remove tokens with the X button.

Variables
Operators
Formula
Drag variables and operators here to build a formula

With Validation

Enable showValidation to display errors for malformed expressions.

Variables
Operators
Formula
Drag variables and operators here to build a formula

Pre-filled Formula

Pass an initial value to pre-populate the canvas.

Variables
Operators
Formula
Drag variables and operators here to build a formula

Disabled

All interaction is blocked when disabled is set.

Variables
Operators
Formula
Drag variables and operators here to build a formula

FormulaBuilder

A drag-and-drop formula builder that lets users compose expressions from variable blocks and math operators.

Usage

import { FormulaBuilder } from "@/components/ui/formula-builder"

const variables = {
  height: "Person height",
  weight: "Person weight",
  age: "Person age",
}

<FormulaBuilder
  variables={variables}
  value={formula}
  onChange={setFormula}
/>

Props

PropTypeDefaultDescription
variables*Record<string, string>Dictionary of variable keys to display labels (e.g. { height: "Person height" }).
valuestringControlled formula string, space-separated (e.g. "height * age / weight").
onChange(formula: string) => voidCallback fired when the formula changes.
operatorsstring[]["+", "-", "*", "/", "(", ")"]Operators available in the palette.
placeholderstring"Drag variables and operators here…"Placeholder text shown when the formula canvas is empty.
disabledbooleanfalseDisables all interaction (drag, click, remove).
showValidationbooleanfalseShow validation errors for consecutive operators, unbalanced parentheses, etc.
classNamestringAdditional className applied to the root container.