Quick Start
Prerequisites
- An existing project using RJSF (
@rjsf/core≥ 5 and@rjsf/utils≥ 5). - An RJSF validator (e.g.
@rjsf/validator-ajv8) installed separately. - React ≥ 17.
Installation
sh
npm install @a-ludi/rjsf-formulasDefine a schema with computed fields
Add the x-formula key to any field you want to compute automatically:
json
{
"type": "object",
"properties": {
"price": { "type": "number", "title": "Price" },
"quantity": { "type": "number", "title": "Quantity" },
"total": { "type": "number", "title": "Total", "x-formula": "price * quantity" }
}
}The total field will be kept read-only and updated automatically whenever price or quantity changes.
Render the form
Replace RJSF's <Form> with <FormulaForm> and supply an evaluator:
tsx
import { FormulaForm } from '@a-ludi/rjsf-formulas'
import validator from '@rjsf/validator-ajv8'
import { Parser } from 'expr-eval'
const parser = new Parser()
const schema = {
type: 'object' as const,
properties: {
price: { type: 'number' as const, title: 'Price' },
quantity: { type: 'number' as const, title: 'Quantity' },
total: { type: 'number' as const, title: 'Total', 'x-formula': 'price * quantity' },
},
}
export function MyForm() {
return (
<FormulaForm
schema={schema}
validator={validator}
evaluator={(formula, ctx) => parser.evaluate(formula, ctx)}
/>
)
}FormulaForm accepts all the same props as RJSF's <Form>, so you can add formData, onChange, onSubmit, custom widgets, and everything else you would normally use.
What happens
- On mount,
totalis computed from the initialpriceandquantityvalues. - When the user changes
priceorquantity, the formula is re-evaluated (debounced by 300 ms by default). - The
totalfield is rendered as read-only — the user cannot edit it directly.
Next steps
- See Customization for all available props.
- Try the live demo to explore different schema configurations interactively.