watchField()
function watchField<TForm, TField>(
field,
callback,
options?): void;
Defined in: core/behavior/behaviors/watch-field.ts:92
Выполняет callback при изменении поля
Type Parameters
TForm
TForm
TField
TField
Parameters
field
FieldPathNode<TForm, TField>
Поле для отслеживания
callback
(value, ctx) => void | Promise<void>
Функция обратного вызова
options?
Опции (debounce, immediate)
Returns
void
Examples
import { watchField, type BehaviorSchemaFn } from '@reformer/core/behaviors';
interface AddressForm { region: string; city: string }
export const addressBehavior: BehaviorSchemaFn<AddressForm> = (path) => {
watchField(
path.region,
async (region, ctx) => {
if (!region) {
ctx.form.city.updateComponentProps({ options: [] });
return; // guard: пустое значение не триггерит fetch
}
try {
const { data: cities } = await fetchCities(region);
ctx.form.city.updateComponentProps({ options: cities });
} catch (error) {
console.error('[addressBehavior] failed to load cities:', error);
ctx.form.city.updateComponentProps({ options: [] });
}
},
{ immediate: false, debounce: 300 }, // обязательные опции для async
);
};
import { watchField, type BehaviorSchemaFn } from '@reformer/core/behaviors';
interface InsuranceForm {
insuranceType: 'casco' | 'osago' | 'property' | '';
vehicleVin: string;
propertyType: string;
}
export const insuranceBehavior: BehaviorSchemaFn<InsuranceForm> = (path) => {
// ОДИН watchField на trigger-поле — несколько watcher'ов на одно поле = "Cycle detected"
watchField(
path.insuranceType,
(type, ctx) => {
const isVehicle = type === 'casco' || type === 'osago';
const isProperty = type === 'property';
// Guard — ставим только если состояние реально меняется
if (isVehicle) {
if (ctx.form.vehicleVin.disabled.value) ctx.form.vehicleVin.enable();
} else {
if (!ctx.form.vehicleVin.disabled.value) ctx.form.vehicleVin.disable();
if (ctx.form.vehicleVin.getValue() !== '') ctx.form.vehicleVin.setValue('');
}
if (isProperty) {
if (ctx.form.propertyType.disabled.value) ctx.form.propertyType.enable();
} else {
if (!ctx.form.propertyType.disabled.value) ctx.form.propertyType.disable();
if (ctx.form.propertyType.getValue() !== '') ctx.form.propertyType.setValue('');
}
},
{ immediate: false }, // CRITICAL: предотвращает запуск во время инициализации
);
};