@publicodes/forms

API / @publicodes/forms

This library exports utilities to create interactive forms and simulators from publicodes rules.

This library is framework-agnostic and can be used with any state management system:

  • React useState
  • Redux
  • Svelte stores
  • Vue reactive system
  • etc.

Usage

// Create a form builder
const engine = new Engine(rules)
const formBuilder = new FormBuilder({ engine })

// Initialize form state
let state = FormBuilder.newState()
state = formBuilder.start(state, 'target . rule')

// Get current page UI elements
const formElements = formBuilder.currentPage(state)

// Handle user input
state = formBuilder.handleInputChange(state, 'rule . name', 'new value')

// Navigate pages
state = formBuilder.goToNextPage(state)
state = formBuilder.goToPreviousPage(state)

// Get pagination information
const { current, pageCount, hasNextPage, hasPreviousPage } =
    formBuilder.pagination(state)

Getting started

npm install publicodes publicodes-form

Then follow the guide to create a simple page.

Classes

ClassDescription
FormBuilderCreates and manages multi-page forms based on Publicodes rules.

Functions

FunctionDescription
buildFormPageBuilds an interactive form page with dynamic field behavior based on user progression.
computeNextFieldsComputes the next fields that need to be asked next in a form.
convertInputValueToPublicodesConvert input value from DOM change event to publicodes expression that can be used to update the engine situation.
getEvaluatedFormElementGet the form element for a publicodes rules enriched with runtime information from the engine.
getFormElementGenerates a UI form element representation based on a Publicodes rule.
getOptionList-
groupByNamespaceGroups fields into pages based on their namespace hierarchy, ensuring related fields stay together. Useful when organizing form fields into logical sections or steps.
updateSituationWithFormValueUpdate the engine situation with the value of an associated form input element.

Interfaces

InterfaceDescription
EvaluatedCheckbox-
EvaluatedNumberInput-
EvaluatedRadioGroup-
EvaluatedSelect-
EvaluatedStringInput-
EvaluatedTextarea-
InputElement-
RadioGroupElement-
SelectElement-
TextareaElement-

Type Aliases

Type aliasDescription
EvaluatedFormElementRepresents the different types of form elements that can be generated from Publicodes rules.
FormElementRepresents the different types of form elements that can be generated from Publicodes rules. This union type combines all possible form control representations.
FormPageElementPropProperties that control how a form element should be displayed and behave in the UI.
FormStateRepresents the complete state of a multi-page form.
Option-
PageBuilderA function that organizes form fields into logical pages or groups.
RuleWithFormMetaA Publicodes Rule with additional metadata for form input.