Conditional Logic
Show, hide, enable, or disable fields based on conditions.
enableWhenβ
Enable field only when condition is true.
import { enableWhen } from '@reformer/core/behaviors';
behaviors: (path, ctx) => [
enableWhen(path.submitButton, () => form.controls.agreeToTerms.value === true),
];
Example: Step Formβ
const form = new GroupNode({
form: {
step1Complete: { value: false },
step2Data: { value: '' },
},
behaviors: (path, ctx) => [
enableWhen(path.step2Data, () => form.controls.step1Complete.value === true),
],
});
resetWhenβ
Reset field to initial value when condition becomes true.
import { resetWhen } from '@reformer/core/behaviors';
behaviors: (path, ctx) => [
// Reset details when type changes
resetWhen(path.typeDetails, () => form.controls.type.value, { watchValue: true }),
];
Example: Dependent Dropdownsβ
const form = new GroupNode({
form: {
country: { value: '' },
city: { value: '' },
},
behaviors: (path, ctx) => [
// Reset city when country changes
resetWhen(path.city, () => form.controls.country.value, { watchValue: true }),
],
});
form.controls.country.setValue('US');
form.controls.city.setValue('NYC');
form.controls.country.setValue('UK'); // city resets to ''
Combining Conditionsβ
Use multiple behaviors together:
behaviors: (path, ctx) => [
// Show business fields only for business accounts
enableWhen(path.companyName, () => form.controls.accountType.value === 'business'),
enableWhen(path.taxId, () => form.controls.accountType.value === 'business'),
// Reset business fields when switching to personal
resetWhen(path.companyName, () => form.controls.accountType.value === 'personal'),
resetWhen(path.taxId, () => form.controls.accountType.value === 'personal'),
];
Complex Conditionsβ
behaviors: (path, ctx) => [
enableWhen(path.spouseInfo, () => {
const status = form.controls.maritalStatus.value;
const includeSpouse = form.controls.includeSpouse.value;
return status === 'married' && includeSpouse === true;
}),
];
Next Stepsβ
- Field Sync β Copy and synchronize fields
- Watch Behaviors β React to changes