buildFormPage

API / @publicodes/forms / buildFormPage

function buildFormPage<Name>(
   page, 
   engine, 
   targets, 
   lastAnswered): FormPageElementProp & EvaluatedFormElement[]

Defined in: buildFormPage.ts:59

Builds an interactive form page with dynamic field behavior based on user progression.

This function enhances form elements with UI-specific behaviors needed for a step-by-step form experience:

  • Progressive disclosure: Fields are shown/hidden based on their relevance to target computations
  • Focus management: Auto-focuses the first unanswered field when entering a new page
  • Field dependencies: Disables fields that are no longer relevant for target calculations
  • Visual cleanup: Automatically collapses irrelevant fields that appear after the last answered question

Use this when building multi-step forms where you want to guide users through a sequence of questions while maintaining a clean interface by hiding unnecessary fields.

Type Parameters

Type Parameter
Name extends string

Parameters

ParameterTypeDescription
pageName[]Array of rule names representing form fields to display
engineEngine<Name>Publicodes engine instance containing rules and current situation
targetsName[]Target rules that determine which fields are useful
lastAnswerednull | NameThe most recently answered field, used for progressive disclosure

Returns

FormPageElementProp & EvaluatedFormElement[]

Array of form elements enhanced with UI interaction states (hidden, disabled, autofocus)

Example

// In a form wizard component
const pageElements = buildFormPage(
  ['person . age', 'person . income'],
  engine,
  ['taxes . eligible'],
  'person . age'
)
// Returns elements with proper focus/visibility for a form wizard interface