Skip to content

createArrayField

Creates field, which contains an array of primitive values (check createField api docs) or an array of objects with subfields

Type Signature:

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

function createArrayField<
  T extends PrimitiveValue | AnySchema,
  Meta extends object = any,
  Value = UserFormSchema<T>,
>(
  values: T[],
  overrides?: CreateArrayFieldOptions<Meta>,
): ArrayField<T, Meta, Value>

Example Usage:

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

const arrayFieldPrimary = createArrayField<number>([]);
const addNumber = createEvent<number>();

const logFx = createEffect(console.log);

sample({
  clock: addNumber,
  target: arrayFieldPrimary.push,
});

sample({
  clock: arrayFieldPrimary.changed,
  target: logFx,
});

addNumber(10); // logFx -> [10]
addNumber(20); // logFx -> [10, 20]

array field with primitive values

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

const field = createArrayField<number, { onlyPositive: boolean }>([], {
  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 }

array field meta example

WARNING

By the some reasons, you can't use dynamic array field subfields in effector logic, cause the sample/split/etc target must be static.

TIP

you can use any variant of schema values in array field api

ts
field.change([10])
field.change([{ name: 'John' }])
field.change([{ name: createField('John') }])
field.change([{ names: createArrayField<string>(['John']) }])

API

nametypedescription
changeErrorEventCallable<FieldError>change outer array field error
changeEventCallable<T[]>change array field values, like:field.change([10, 20])
pushEventCallable<PushPayload<T>>push item at the and of array, like:field.push(10)
changeMetaEventCallable<Meta>change field meta, like:field.changeMeta({ onlyPositive: true })
swapEventCallable<SwapPayload>swap items by indexes, like:field.swap({ indexA: 2, indexB: 10 })
moveEventCallable<MovePayload>move item by indexes, like:field.move({ from: 2, to: 10 })
insertEventCallable<InsertOrReplacePayload<T>>insert item at the index, like:field.insert({ index: 2, value: 10 })
unshiftEventCallable<UnshiftPayload<T>>unshift value (place at the start), like:field.unshift(2)```orfield.unshift([2, 4])`
removeEventCallable<RemovePayload>remove item by index, like:field.remove({ index: 10 })
popEventCallable<void>pop item (remove last element), like:field.pop()
replaceEventCallable<InsertOrReplacePayload<T>>replace item by index, like:field.replace({ index: 2, value: 10 })
resetEventCallable<{ values: U; error: FieldError }>reset field values
changedEvent<U[]>triggered when values changed
errorChangedEvent<FieldError>triggered when outer or inner error changed
pushedEvent<{ params: PushPayload<T>; result: U[] }>triggered when pushed
metaChangedEvent<Meta>field meta changed
swappedEvent<{ params: SwapPayload; result: U[] }>triggered when swapped
movedEvent<{ params: MovePayload; result: U[] }>triggered when moved
insertedEvent<{ params: InsertOrReplacePayload<T>; result: U[]; }>triggered when inserted
unshiftedEvent<{ params: UnshiftPayload<T>; result: U[]; }>triggered when unshifted
removedEvent<{ params: RemovePayload; result: U[] }>triggered when removed
poppedEvent<U[]>triggered when popped
replacedEvent<{ params: InsertOrReplacePayload<T>; result: U[]; }>triggered when replaced
$valuesStore<U[]>primitive values (or objects with subfields)
$errorStore<FieldError>error of array field
$metaStore<Meta>field meta
$isValidStore<boolean>is field valid

Released under the MIT License.