@publicodes/forms

API / @publicodes/forms

This library export 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.
// Initialize form state
const formState = initFormState({ engine }, 'target . rule')

// Get current page UI elements
const pageElements = currentPage({ formState, engine })

// Handle user input
const newState = handleInputChange({
    formState,
    engine,
    id: 'rule . name',
    value: 'new value',
})

// Navigate pages
const nextState = goToNextPage(formState)
const prevState = goToPreviousPage(formState)

Getting started

npm install publicodes publicodes-form

Then follow the guide to create a simple page.

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.
currentPageCreates a UI representation of the current form page.
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-
goToNextPageAdvances the form to the next page in the sequence.
goToPreviousPageNavigates to the previous page in the form.
groupByNamespaceGroups fields into pages based on their namespace hierarchy, ensuring related fields stay together. Useful when organizing form fields into logical sections or steps.
handleInputChangeUpdates the form state when a user changes an input value.
initFormStateInitializes a form state based on the provided engine and targets.
paginationRetrieves pagination information for the current form state.
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.
FormState-
Option-
PageBuilderFunction to group fields into pages Takes an array of field names and returns arrays of fields
RuleWithFormMetaA Publicodes Rule with additional metadata for form input