Перейти к основному содержимому

enableWhen()

function enableWhen<TForm>(
field,
condition,
options?): void;

Defined in: core/behavior/behaviors/enable-when.ts:86

Условное включение поля на основе значений других полей

Type Parameters

TForm

TForm

Parameters

field

FieldPathNode<TForm, any>

Поле для включения/выключения

condition

(form) => boolean

Функция условия (true = enable, false = disable)

options?

EnableWhenOptions

Опции (resetOnDisable, debounce)

Returns

void

Examples

import { enableWhen, type BehaviorSchemaFn } from '@reformer/core/behaviors';

interface LoanForm {
loanType: 'mortgage' | 'consumer' | 'car';
propertyValue: number;
initialPayment: number;
}

export const loanBehavior: BehaviorSchemaFn<LoanForm> = (path) => {
// Поля ипотеки активны только для loanType === 'mortgage'.
// resetOnDisable: true гарантирует чистые initial values при переключении.
enableWhen(path.propertyValue, (form) => form.loanType === 'mortgage', {
resetOnDisable: true,
});
enableWhen(path.initialPayment, (form) => form.loanType === 'mortgage', {
resetOnDisable: true,
});
};
import { enableWhen, type BehaviorSchemaFn } from '@reformer/core/behaviors';

interface ProfileForm {
sameAsRegistration: boolean;
employmentStatus: 'employed' | 'selfEmployed' | 'unemployed';
residenceAddress: { city: string; street: string };
companyName: string;
companyInn: string;
businessType: string;
}

export const profileBehavior: BehaviorSchemaFn<ProfileForm> = (path) => {
// Адрес проживания: enabled, когда НЕ совпадает с регистрационным
enableWhen(path.residenceAddress, (form) => form.sameAsRegistration === false, {
resetOnDisable: true,
});

// Поля работодателя: только для employed
enableWhen(path.companyName, (form) => form.employmentStatus === 'employed', {
resetOnDisable: true,
});
enableWhen(path.companyInn, (form) => form.employmentStatus === 'employed', {
resetOnDisable: true,
});

// ИП-поля: только для selfEmployed
enableWhen(path.businessType, (form) => form.employmentStatus === 'selfEmployed', {
resetOnDisable: true,
});

// ВАЖНО: condition не должен читать значение САМОГО поля — иначе цикл.
// condition зависит ТОЛЬКО от независимых триггеров (loanType, employmentStatus, ...).
};

See

docs/llms/22-cycle-detection.md