Skip to main content

Class: FormState<VS, SD, ES, WS>

A class that implements the core form state machine behavior.

Type parameters#

NameTypeDescription
VSVSType of form value state.
SDunknownType of submit handler result.
ESundefinedType of form error state.
WSundefinedType of form warning state.

Hierarchy#

  • StateManager<FormData<VS, SD, ES, WS>>

    ↳ FormState

Constructors#

constructor#

• new FormState<VS, SD, ES, WS>(initialValues, options)

Create a new FormState instance with initial values and config options.

const form = new FormState(   // Initial form values   {     name: '',     email: '',   },
   // Form options   {     // Submit handler     async onSubmit(values) {       return {ok: true};     },
     // Error validation callback     validate(values) {       const errors = {};
       if (!values.name) {         errors.name = 'required;       }
       if (!values.email) {         errors.email = 'required;       }
       return errors;     },
     // Warning validation callback     warn(values) {       const warnings = {};
       if (!/.+\@.+\..+/.test(values.email)) {         warnings.email = 'invalid email';       }
       return warnings;     },   },);

Type parameters#

NameType
VSVS
SDunknown
ESundefined
WSundefined

Parameters#

NameTypeDescription
initialValuesVSInitial form values.
optionsFormOptions<VS, SD, ES, WS>Form options object.

Overrides#

StateManager<FormData<VS, SD, ES, WS\>\>.constructor

Methods#

acceptPendingFieldValue#

â–¸ acceptPendingFieldValue(path, resolve?): void

Set the pending value as the current value for a field.

(See also: rejectPendingFieldValue)

const form = new FormState({foo: 'bar'}, {onSubmit() {...}});
formState.setValues({foo, 'updated'});
formState.getFieldValue('foo'); // 'bar'
formState.acceptPendingFieldValue('foo');
formState.getFieldValue('foo'); // 'updated'

Parameters#

NameTypeDescription
pathFieldPathThe path to a field within the form state.
resolve(value: any, pendingValue: any) => any

Returns#

void


blurField#

â–¸ blurField(path): void

Process a blur event for a given path. This clears the current focused path state and sets the touched flag for that path. In the case that the current focused path is not the provided path, this action applies the effects of an implicit focus event before applying the blur effects. This serves as backup in case a field has failed to fire a focus event, keeping the derived state consistent.

(See also: focusField)

const form = new FormState({foo: 'bar'}, ...);
formState.blurField('foo');

Parameters#

NameTypeDescription
pathFieldPathThe path to a field within the form state.

Returns#

void


focusField#

â–¸ focusField(path): void

Process a focus event for a given path. This sets the current focus path to the provided path and sets the visited flag for that path. In the case that the current focused path is not undefined, this action applies the effects of an implicit blur event before applying the focus effects. This serves as backup in case a field has failed to fire a blur event, keeping the derived state consistent.

(See also: blurField)

const form = new FormState({foo: 'bar'}, ...);
formState.focusField('foo');

Parameters#

NameTypeDescription
pathFieldPathThe path to a field within the form state.

Returns#

void


getErrors#

â–¸ getErrors(): undefined | ES

Get the current error validation state. This method returns the most recent value returned from FormOptions.validate.

Returns#

undefined | ES


getFieldError#

â–¸ getFieldError(path): any

Parameters#

NameType
pathFieldPath

Returns#

any


getFieldValue#

â–¸ getFieldValue(path): any

Parameters#

NameType
pathFieldPath

Returns#

any


getFieldWarning#

â–¸ getFieldWarning(path): any

Parameters#

NameType
pathFieldPath

Returns#

any


getInitialFieldValue#

â–¸ getInitialFieldValue(path): any

Parameters#

NameType
pathFieldPath

Returns#

any


getPendingFieldValue#

â–¸ getPendingFieldValue(path): any

Parameters#

NameType
pathFieldPath

Returns#

any


getResult#

â–¸ getResult(): undefined | SD

Returns#

undefined | SD


getState#

â–¸ getState(): FormData<VS, SD, ES, WS>

Returns#

FormData<VS, SD, ES, WS>

Inherited from#

StateManager.getState


getSubmitErrors#

â–¸ getSubmitErrors(): undefined | ES

Returns#

undefined | ES


getSubmitStatus#

â–¸ getSubmitStatus(): FormSubmitStatus

Returns#

FormSubmitStatus


getWarnings#

â–¸ getWarnings(): undefined | WS

Get the current warning validation state. This method returns the most recent value returned from FormOptions.warn.

Returns#

undefined | WS


hasErrors#

â–¸ hasErrors(): boolean

Returns#

boolean


hasSubmitErrors#

â–¸ hasSubmitErrors(): boolean

Returns#

boolean


hasWarnings#

â–¸ hasWarnings(): boolean

Returns#

boolean


isFieldDetached#

â–¸ isFieldDetached(path): boolean

Parameters#

NameType
pathFieldPath

Returns#

boolean


isFieldDirty#

â–¸ isFieldDirty(path): boolean

Parameters#

NameType
pathFieldPath

Returns#

boolean


isFieldFocused#

â–¸ isFieldFocused(path): boolean

Parameters#

NameType
pathFieldPath

Returns#

boolean


isFieldTouched#

â–¸ isFieldTouched(path): boolean

Parameters#

NameType
pathFieldPath

Returns#

boolean


isFieldVisited#

â–¸ isFieldVisited(path): boolean

Parameters#

NameType
pathFieldPath

Returns#

boolean


rejectPendingFieldValue#

â–¸ rejectPendingFieldValue(path): void

Set the current value as the pending value for a field.

(See also: acceptPendingFieldValue)

const form = new FormState({foo: 'bar'}, {onSubmit() {}});
formState.setValues({foo, 'updated'});
formState.getPendingFieldValue('foo'); // 'updated'
formState.rejectPendingFieldValue('foo');
formState.getPendingFieldValue('foo'); // 'foo'

Parameters#

NameTypeDescription
pathFieldPathThe path to a field within the form state.

Returns#

void


runValidate#

â–¸ runValidate(): void

Update the error state with the result of the provided validate callback.

Returns#

void


runWarn#

â–¸ runWarn(): void

Update the warning state with the result of the provided warn callback.

Returns#

void


setFieldValue#

â–¸ setFieldValue(path, setValueAction): void

Set the current value at a given path and run the provided validate and warn callbacks to update the error and warning state if the value change is not idempotent.

Parameters#

NameTypeDescription
pathFieldPathThe path to a field within the form state.
setValueActionany

Returns#

void


setValues#

â–¸ setValues(values): void

Process an incoming values prop. This action update the cached initial values and pending values in the state - which will in turn update the derived detached and dirty flags returned from useField.

Parameters#

NameType
valuesVS

Returns#

void


submit#

â–¸ submit(): Promise<void>

Submit the current form values by calling the configured submit callback.

Returns#

Promise<void>

A promise that resolves when the submit sequence is complete.


subscribe#

â–¸ subscribe(subscriber): Object

Parameters#

NameType
subscriber() => void

Returns#

Object

NameType
unsubscribe() => void

Inherited from#

StateManager.subscribe