Skip to main content

ReactForms

The ReactForms component controls all of the state for the form and provides several functions that manipulate this state or perform imperative actions.

Props

NameTypeDefaultDescription
idstringRandom UUIDUnique identifier for the form
namesstring[][]List of field names that will be included in the form state. Names and default states are populated when Field components mount, however you can use the names array to tell the form about fields that aren't rendered yet, so you can manipulate their state before the Field has mounted for the first time.
initialValuesPartial<Values>{}Values that should be populated when the form mounts. Used to determine each fields isDirty state
touchOnChangebooleanfalseWhether each field will update it's touched state when the onChange function is called
touchOnBlurbooleantrueWhether each field will update it's touched state when the onBlur function is called
validateOnChangebooleanfalseWhether each field will run the validate function when the onChange function is called
validateOnBlurbooleantrueWhether each field will run the validate function when the onBlur function is called
validateValidation<T, R> \| { [key: string]: SingleValidation<T, R> } \| CreateValidationRulesArgsundefinedConfiguration describing how to validate each field within the form OR a function that returns an object of field errors
handleSubmitSubmission<Values, Helpers>undefinedFunction that runs when the submitForm function is called and the form has passed validation
render / children(props: FormPassthroughProps<T, R>) => JSX.ElementundefinedFunction that takes the current form state and actions and returns the form components to render

Render prop args

The render prop is passed most of the form context and contains all of the data and actions that a user will need to create their forms.

State

NameTypeDescription
idstringUnique identifier of the form
namesstring[]List of all of the field names that are within this form
fieldArrayNamesstring[]List of all of the field array names that are within this form
initialValuesPartial<Values>The initial values passed into the form used to populate the values of the fields on mount
valuesPartial<Values>The values for all of the fields that are managed by this form
touchedBooleanObjectThe touched state of all of the fields within this form - whether or not the user has interacted with any given field
errorsFormErrorObjectThe error state of all of the fields within this form
nonBlockingErrorFormErrorAn error that doesn't block form submission, but should still be visible to the user
metaanyArbitrary state that is passed to some of the callbacks as part of the Helpers
submitCountnumberHow many times this form has been submitted (successfully or not)
isDirtybooleanWhether any of the fields within the form are considered dirty (field value is different from the initialValue)
isValidatingbooleanWhether any of the fields are currently validating their input
isValidbooleanWhether all of the fields are considered valid (if the form has been submitted, any field with an error is considered invalid, otherwise any field with an error that is also touched and isDirty is considered invalid)
isSubmittingbooleanWhether or not this form is currently submitting
fieldIsDirtyBooleanObjectThe isDirty state for each individual field
fieldIsValidatingBooleanObjectThe isValidating state for each individual field
fieldIsValidBooleanObjectThe isValid state for each individual field

Actions

NameTypeDescription
setFieldValue(name: string, value: any) => voidImperatively set a value for a specific field
setFieldTouched(name: string, touched: boolean) => voidImperatively set the touched state for a specific field
setFieldError(name: string, error: string \| FormError) => voidImperatively set an error on a specific field
setServerError(error: string \| FormError) => voidImperatively set a nonBlockingError
setMeta(meta: any) => voidSet any arbitrary meta information about the form
setValues(values: Partial<T>) => voidImperatively set values for multiple fields at once
setTouched(touched: BooleanObject) => voidImperatively set the touched state for multiple fields at once
setErrors(errors: FormErrorObject) => voidImperatively set errors for multiple fields at once
resetField(name: string, value?: any) => voidReset a specific field to it's initial state, optionally pass a value to change the initial value
resetForm(values?: Partial<T>) => voidReset all fields to their initial state, optionally pass values to change the initial values
validateField(name: string, value: any) => voidRun validation for a specific field
validateForm() => voidRun validations for all fields
submitForm(args?: { shouldReset?: boolean; onValidationFail?: ValidationFailArg; }) => Promise<R> \| R \| boolean \| voidAttempts to submit the form; runs validation and the handleSubmit if it passes