computeType: "function" and providing the calculation logic in fnSource.
In this guide, you’ll learn:
- How computed properties differ from static values
- When to use
computeTypeandfnSource - Practical patterns for dynamic labels, helper text, and validation messages
- How to avoid infinite loops and performance pitfalls
Overview
A computed property is a ComponentProperty whose value is determined by a JavaScript function that runs inside the form’s execution context. The function receives the current IFormData (as theform parameter) and can read form.data,
form.errors, form.state, and other form metadata.
Computed properties are useful for:
- Dynamic labels and hints that change based on other field values
- Conditional formatting (colors, visibility hints)
- Real‑time validation feedback beyond the built‑in schema validation
- Derived values that you want to display but not store as form data
How computed properties work
Inside a component’sprops, any property can be either a static value or a computed one. The structure is the same as
for conditional rendering and localization, but with computeType "function".
Property value shapes
Static property (fixed value):DynamicHelperText.json
fnSource field contains the body of a JavaScript function that receives form (type IFormData) and returns the
property value. Do not wrap the body in function(form) { ... }, write the code that would appear inside the function.
Execution context
InsidefnSource you have access to:
| Variable | Type | Purpose |
|---|---|---|
form | IFormData | The current form state, errors, and metadata. |
form.data | Record<string, unknown> | Values of all fields keyed by their dataKey / key. |
form.errors | Record<string, unknown> | Validation errors keyed by dataKey / key. |
form.state | Record<string, unknown> | Custom state you can set via actions or event handlers. |
form.index | number | undefined | Index of the current item inside repeaters. |
Examples
Dynamic helper text
Show a hint that adapts to the user’s input.PasswordStrength.json
Conditional label
Change a label based on a checkbox selection.BillingLabel.json
Real‑time validation feedback
Provide immediate feedback beyond schema validation.EmailFeedback.json
Live examples
Dynamic character counter
Show a remaining‑character count below a text field.Live example
live
Price calculator
Compute a total price based on quantity and unit price, and display it as helper text.Live example
live
Conditional styling via computed property
Makes the button accessible if the checkbox is checked.Live example
live
Pitfalls and precautions
Infinite update loops
The most dangerous risk with computed properties is creating a circular dependency: a computed property writes toform.data (directly or
via an action), which triggers recomputation, which writes again, and so on.
DangerousExample.json
Performance impact
Computed properties run on every form change. If you have many computed properties or complex logic inside them, the form may feel sluggish. Do:- Keep the logic inside
fnSourceshort and fast. - Use computed properties only for properties that truly need to be dynamic.
- Perform network requests, large array operations, or synchronous heavy computations inside
fnSource. - Create chains where property A depends on B, B depends on C, and C depends on A.
Error handling
If the function throws an exception, FormEngine Core catches it and logs a warning to the console. The property will keep its previous value (or fall back to the component’s default). Always test your computed functions with edge cases:null, undefined, empty strings,
unexpected types.
Best practices
- Keep functions pure: A computed property should be a pure function of
form. No side effects, no modifications toform. - Use for display, not storage: Prefer computed properties for labels, helper text, colors, and read‑only displays. For values that should be saved as form data, use regular data‑bound fields.
- Leverage
form.hasErrors: When you want to reflect validation state, checkform.hasErrorsandform.errorsrather than re‑implementing validation logic. - Test the dependency chain: Before deploying, verify that changing any field doesn’t cause unexpected recomputation loops.
Summary
- Computed properties let you define component settings that recalculate automatically when the form state changes.
- Set
computeType: "function"and provide the JavaScript function body infnSource. - The function receives
form(type IFormData) and must return the property value. - Use with caution: Avoid modifying
form.data, keep functions lightweight, and watch for circular dependencies. - Ideal for dynamic labels, real‑time feedback, conditional formatting, and derived display values.
For more information:
- Form JSON structure
- Conditional rendering
- Localization
- Handling form data
- Actions and events
- ComponentProperty reference
- IFormData reference
Next steps
- Conditional Logic — Show or hide components based on form conditions
- Actions & Events — Respond to user interactions and form changes