@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
Class | Description |
---|---|
FormBuilder | Creates and manages multi-page forms based on Publicodes rules. |
Functions
Function | Description |
---|---|
buildFormPage | Builds an interactive form page with dynamic field behavior based on user progression. |
computeNextFields | Computes the next fields that need to be asked next in a form. |
convertInputValueToPublicodes | Convert input value from DOM change event to publicodes expression that can be used to update the engine situation. |
getEvaluatedFormElement | Get the form element for a publicodes rules enriched with runtime information from the engine. |
getFormElement | Generates a UI form element representation based on a Publicodes rule. |
getOptionList | - |
groupByNamespace | Groups fields into pages based on their namespace hierarchy, ensuring related fields stay together. Useful when organizing form fields into logical sections or steps. |
updateSituationWithFormValue | Update the engine situation with the value of an associated form input element. |
Interfaces
Interface | Description |
---|---|
EvaluatedCheckbox | - |
EvaluatedNumberInput | - |
EvaluatedRadioGroup | - |
EvaluatedSelect | - |
EvaluatedStringInput | - |
EvaluatedTextarea | - |
InputElement | - |
RadioGroupElement | - |
SelectElement | - |
TextareaElement | - |
Type Aliases
Type alias | Description |
---|---|
EvaluatedFormElement | Represents the different types of form elements that can be generated from Publicodes rules. |
FormElement | Represents the different types of form elements that can be generated from Publicodes rules. This union type combines all possible form control representations. |
FormPageElementProp | Properties that control how a form element should be displayed and behave in the UI. |
FormState | Represents the complete state of a multi-page form. |
Option | - |
PageBuilder | A function that organizes form fields into logical pages or groups. |
RuleWithFormMeta | A Publicodes Rule with additional metadata for form input. |