Skip to content

createField

Creates field, which contains primitive value (like string, number, boolean, Date, bigint, Blob, ArrayBuffer, File, FileList, null)

Type Signature:

ts
interface CreatePrimitiveFieldOptions<Meta extends object = any> {
  error?: FieldError;
  meta?: Meta;
  copyOnCreateForm?: boolean;
}

function createField<
  T extends PrimitiveValue,
  Meta extends object = any,
>(
  defaultValue: T,
  overrides?: CreatePrimitiveFieldOptions<Meta>,
): PrimitiveField<T, Meta>;

Example Usage:

ts
import { createField } from "@effector-reform/core";
import { sample, createEffect } from "effector";

const numberField = createField(0);
const stringField = createField('');
const typedStringField = createField<'inner' | 'outer'>('inner');

const logFx = createEffect(console.log);

sample({
  clock: [numberField.changed, stringField.changed],
  target: logFx,
});

numberField.change(10); // logFx -> 10
stringField.change('test'); // logFx -> 'test'

base primitive field example

ts
import { createField } from "@effector-reform/core";
import { createEvent, sample } from "effector";

const field = createField<number, { onlyPositive: boolean }>(0, {
  meta: { onlyPositive: false }
});

const ruleToggled = createEvent();

sample({
  clock: ruleToggled,
  source: field.$meta,
  fn: (meta) => { onlyPositive: !meta.onlyPositive },
  target: field.changeMeta,
});

field.$meta.getState(); // { onlyPositive: false } 

ruleToggled();

field.$meta.getState(); // { onlyPositive: true }

primitive field meta example

API

nametypedescription
$valueStore<T>value of field
$metaStore<Meta>field meta
$errorStore<FieldError>error of field
$isValidStore<boolean>is field valid
$isFocusedStore<boolean>is user focused on field
blurredEvent<void>triggered when user blurred input (you must set focus/blur handlers in input)
focusedEvent<void>triggered when user focused input (you must set focus/blur handlers in input)
changeEventCallable<T>change field value
changedEvent<T>triggered when field value changed
resetEventCallable<void>reset field value
changeErrorEventCallable<FieldError>change outer field error
changeMetaEventCallable<Meta>change field meta
metaChangedEvent<Meta>field meta changed
errorChangedEvent<FieldError>triggered when error changed (inner or outer)

Released under the MIT License.