![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@joyfill/conditional-logic
Advanced tools
Joyfill is a forms platform with APIs and SDKs built to take the burden of building form and pdf solutions off of developers and product teams. Our Embeddable Form SDKs for JS, Angular, Vue, React, React-Native, etc. utilize this library to handle the visibility evaluation logic for fields and pages. We have open sourced this library to enable other developers to utilize the same conditional logic we use. Enjoy!
The goal of conditional logic evaluation is to determine if an item needs to be displayed or hidden based on a set of conditions.
Item’s passed to the library will either have hidden: true by default (representing hidden items) or hidden: false|undefined by default (representing shown items). The library will evaluate the item’s logic (if present) to determine if the value of the hidden property should be changed or left the same.
yarn add @joyfill/conditional-logic
or npm install --save @joyfill/conditional-logic
import { applyLogic } from "@joyfill/conditional-logic";
const fields = [
{ _id: "field_one", type: "text", value: "hello world" },
{ _id: "field_two", type: "number", value: 100 },
{ _id: "field_three", type: "multiSelect", value: ["option_one"] }
];
const items = [
{
_id: "conditional_one",
hidden: true,
logic: {
action: "show",
eval: "and",
conditions: [
{
field: "field_one",
condition: "?=",
value: "hello"
}
]
}
}
]
const evaluatedItems = applyLogic(items, fields, "_id");
console.log(evaluatedItems[0].hidden);
/**
* Logs false.
*
* Why? The logic associated with that item has an action to show
* the item if "field_one" contains "hello" in the value property.
* Since "field_one" value matches that condition the hidden
* property is changed to "hidden: false".
*/
In most cases when using this library the fields argument and items argument will be same array of objects. Why is this? This is because when you’re talking about conditional logic for forms the conditions to show or hide fields will be determined by the other field values on the form. For instance, show field B when field A has the proper value entered into it. In the example below I split the arrays into two for readability.
Evaluates the logic on each item and sets the hidden
property to either true or false depending on if they should be shown or hidden.
items
- Object_Arrayfields
- Object_ArrayfieldLookupKey
- Stringaction
show
- Item should be shown (hidden: false
) when conditions are met.hide
- Item should be hidden (hidden: true
) when conditions a met.eval
and
- All conditions must be met.or
- Only one condition must be met.conditions
field
- The that will be used to retrieve the associated field you want to evaluate the value of.value
- The value you will evaluate against the associated field. Supports string and number types.condition
- What type of evaluation check to perform against the logic condition value and field value. Only certain conditions cab be used with certain field value data types (see field value types below the condition option for specifics).
*=
null=
=
!=
?=
>
<
{ //Item
...,
logic: {
action: 'show',
eval: 'and',
conditions: [
{
field: 'field_id',
condition: '=',
value: 'hello'
}
]
}
}
logic.condition[x].field
is referencing. This must be unique per-field.type
- Will be used in the future to allow custom condition value evaluations.value
- The value that will be used in the associated item condition evaluation.
[ //Fields Array
{ //Field Object
"id": "unique_id",
"type": "custom_type",
"value": "hello world"
},
...
]
The goal of conditional logic evaluation is to determine if an item needs to be displayed or hidden based on a set of conditions.
Item’s passed to the library will either have hidden: true
by default (representing hidden items) or hidden: false|undefined
by default (representing shown items). The library will evaluate the item’s logic (if present) to determine if the value of the hidden property should be changed or left the same.
Below are the steps used to determine if we should show or hide items after evaluating their logic. The show or hide action is determined by the item.logic.action
property.
hidden: true
)Below are the steps used to determine if we should change the hidden property to show the item (hidden: false
) or leave an item hidden (hidden: true
).
hidden: false
). Skips all other steps.hidden: true
). Skip all other steps.logic.action === 'show'
)
hidden: true
). Skip all other steps.hidden: false|undefined
)Below are the steps used to determine if we should change the hidden property to hide the item (hidden: true
) or leave an item shown (hidden: false
).
hidden: false
). Skips all other steps.hidden: false
). Skip all other steps.logic.action === 'hide'
)
hidden: false
). Skip all other steps.FAQs
Conditional logic for forms
The npm package @joyfill/conditional-logic receives a total of 7,497 weekly downloads. As such, @joyfill/conditional-logic popularity was classified as popular.
We found that @joyfill/conditional-logic demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.