Calm motion: animations on this site only fade, with no movement
Formula
A calculation typed by hand, checked as they type
An expression field for the places where a person writes a small calculation: a price rule, a derived column, a score. It shows the result as they type, says exactly what is wrong when it is not, and offers the values they can use. For a plain number use InputNumber. For free text, Input.
Price rule
= 52
Result for the form: —
- Supports
+ − × ÷ ^, brackets, numbers, named values and functions (sum min max avg abs sqrt floor ceil round, plus your own infunctions). It is parsed, neverevaled: the formula can only ever produce a number, andconstructorortoString(1)are just unknown names. variablesare{ name, label?, value }. The chips insert the name at the caret and keep focus in the field. A chip reads as pressed when the formula uses it, and showsname = valueon hover.- The line under the field is a polite live region:
= 52when it works, otherwise what is wrong and where (Unexpected “$” at position 7,A bracket is not closed,Division by zero). A mistyped name gets "did you mean…?" and a button that fixes it in place. The input isaria-invalidwhile it is wrong. resultemits the number, ornullwhile it cannot be calculated: disable your Save button on that.labels.error(e)receives{ code, pos, length, name, suggestion }to write the messages in your language.evaluateFormula()is exported fromblessing-uifor server-side checks with the same rules.
Usage
ts
import { BlessFormula, evaluateFormula } from "blessing-ui";API
Props
| Name | Type | Default | Description |
|---|---|---|---|
variables | BlessFormulaVariable[] | [] | |
functions | FormulaFns | — | extra functions: `{ vat: (x) => x * 0.11 }` |
label | string | "Formula" | |
placeholder | string | "price * qty" | |
disabled | boolean | — | |
locale | string | — | |
labels | Partial<{ insert: string; result: (value: string) => string; error: (e: FormulaError) => string; use: (name: string) => string; }> | — | |
modelValue | string | "" |
Events
| Name | Payload | Description |
|---|---|---|
update:modelValue | [value: string] | |
result | [value: number | null] |