Skip to content

Include & Exclude

Use include to render a widget only when one or more named states are active; use exclude to render it only when they are not active.

Both shapes accept either a list of state names ({ in: [...] }) or a one-off reactive expression ({ when: '...' }).

A boolean toggle reveals a discount-code input only when the user opts in. The state expression reads $form.hasDiscountCode and the discount input declares include: { in: ['hasDiscount'] }.

The state lives in formConfig.states; the include rule sits next to the widget’s other props.

import { gui } from '@golemui/gui-shared';
import { GuiForm } from '@golemui/gui-react';
const config = {
formDef: [
gui.inputs.booleanInput('hasDiscountCode', { label: 'I have a discount code' }),
gui.inputs.textInput('discountCode', {
label: 'Discount code',
include: { in: ['hasDiscount'] },
}),
],
formConfig: {
states: { hasDiscount: '$form.hasDiscountCode === true' },
},
};
export function MyForm() {
return <GuiForm config={config} />;
}

The state lives at the form root under "states"; the widget carries "include" inline.

{
"states": {
"hasDiscount": "$form.hasDiscountCode === true"
},
"form": [
{
"kind": "input",
"type": "checkbox",
"path": "hasDiscountCode",
"label": "I have a discount code"
},
{
"kind": "input",
"type": "textinput",
"path": "discountCode",
"label": "Discount code",
"include": { "in": ["hasDiscount"] }
}
]
}

Wire the parsed JSON into your form component the same way as any other JSON formDef:

import { GuiForm } from '@golemui/gui-react';
import formDef from './my-form.json';
export function MyForm() {
return <GuiForm config={{ formDef }} />;
}

When $form.hasDiscountCode flips to true, the discount-code input mounts; flipping it back unmounts the widget and clears its data.

Both include and exclude accept either form. Pick the one that reads best at the call site.

include: { in: ['hasDiscount'] } // active-state list — name defined in formConfig.states
include: { when: '$form.age >= 18' } // ad-hoc expression — no name needed
"include": { "in": ["hasDiscount"] }
"include": { "when": "$form.age >= 18" }

Use a named state when the same condition appears in multiple places. Use when for one-shot conditions (see Inline when).

exclude is the mirror of include — render the widget unless the state(s) match.

gui.displays.alert('signin-hint', {
text: 'Already have an account? Sign in.',
exclude: { from: ['hasAccount'] },
});
{
"kind": "display",
"type": "alert",
"props": { "text": "Already have an account? Sign in." },
"exclude": { "from": ["hasAccount"] }
}