Skip to main content

Class: FormState<T>

Defined in: state/index.ts:18

FormState manages the state of a form including values, validation, history, and subscriptions. It provides methods to get/set values, validate data, manage history for undo/redo functionality, and subscribe to value changes.

Type Parameters​

T​

T extends object

The type of the form data object

Constructors​

Constructor​

new FormState<T>(initialValues, validationSchema?, historyItems?, historyItemThrottle?, filterHistoryKeys?): FormState<T>

Defined in: state/index.ts:42

Creates a new FormState instance.

Parameters​

initialValues​

T

The initial values for the form

validationSchema?​

ObjectSchema<T, AnyObject, any, "">

Optional Yup validation schema

historyItems?​

number = 0

Number of history items to keep for undo/redo (0 to disable)

historyItemThrottle?​

number = 5

Throttle for saving history items (every Nth change)

filterHistoryKeys?​

string[] = []

Array of field paths to exclude from history tracking

Returns​

FormState<T>

Methods​

getValues()​

getValues(): T

Defined in: state/index.ts:66

Gets a deep copy of all form values.

Returns​

T

A deep copy of the current form values


setErrors()​

setErrors(errors): void

Defined in: state/index.ts:75

Sets validation errors for the form.

Parameters​

errors​

Record<string, ValidationError[]>

Validation errors organized by field path

Returns​

void


getErrors()​

getErrors(): null | Record<string, ValidationError[]>

Defined in: state/index.ts:84

Gets the current validation errors.

Returns​

null | Record<string, ValidationError[]>

Current validation errors or null if no errors


setValues()​

setValues(values, cb?): void

Defined in: state/index.ts:94

Sets all form values and triggers subscriptions.

Parameters​

values​

T

New values to set for the form

cb?​

(success) => void

Call back on success setting value (optional)

Returns​

void


setValue()​

setValue<V>(path, value?): void

Defined in: state/index.ts:114

Sets a value at a specific path and triggers subscriptions.

Type Parameters​

V​

V

The type of value being set

Parameters​

path​

string

Dot notation path to the field (e.g., 'user.name')

value?​

V

The value to set at the specified path

Returns​

void


subscribeOnChange()​

subscribeOnChange<V>(cb, path?): () => boolean

Defined in: state/index.ts:141

Subscribes to form value changes.

Type Parameters​

V​

V

The type of value being subscribed to

Parameters​

cb​

SubscriptionCallBack<V>

Callback function to execute when values change

path?​

string

Optional path to subscribe to a specific field

Returns​

Unsubscribe function to remove the subscription

(): boolean

Returns​

boolean


subscribeOnHistoryChange()​

subscribeOnHistoryChange(cb): () => boolean

Defined in: state/index.ts:158

Subscribes to form history changes (undo/redo operations).

Parameters​

cb​

() => void

Callback function to execute when history changes

Returns​

Unsubscribe function to remove the subscription

(): boolean

Returns​

boolean


getValue()​

getValue<V>(path?): V

Defined in: state/index.ts:173

Gets a value at a specific path or all values if no path is provided.

Type Parameters​

V​

V

The type of value to return

Parameters​

path?​

string

Optional dot notation path to the field

Returns​

V

The value at the specified path or all form values


validate()​

validate(): Promise<void>

Defined in: state/index.ts:182

Validates the form using the provided validation schema.

Returns​

Promise<void>

Throws​

Throws validation errors if validation fails


prev()​

prev(): boolean

Defined in: state/index.ts:227

Moves to the previous state in the form history (undo operation).

Returns​

boolean

True if the operation was successful, false if no previous state exists


next()​

next(): boolean

Defined in: state/index.ts:241

Moves to the next state in the form history (redo operation).

Returns​

boolean

True if the operation was successful, false if no next state exists


canPrev()​

canPrev(): boolean

Defined in: state/index.ts:255

Checks if there is a previous state available for undo operation.

Returns​

boolean

True if undo operation is possible


canNext()​

canNext(): boolean

Defined in: state/index.ts:264

Checks if there is a next state available for redo operation.

Returns​

boolean

True if redo operation is possible