@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
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. |
currentPage | Creates a UI representation of the current form page. |
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 | - |
goToNextPage | Advances the form to the next page in the sequence. |
goToPreviousPage | Navigates to the previous page in the form. |
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. |
handleInputChange | Updates the form state when a user changes an input value. |
initFormState | Initializes a form state based on the provided engine and targets. |
pagination | Retrieves pagination information for the current form state. |
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 | - |
Option | - |
PageBuilder | Function to group fields into pages Takes an array of field names and returns arrays of fields |
RuleWithFormMeta | A Publicodes Rule with additional metadata for form input |