Skip to content
On this page

Field

A field is the primitive for every input that you'd like to display to the user. This is what an example Field looks like:

jsx
<Field<string> name="username" initialValue={""}>
    {({value, setValue}) => (
        <input value={value} onChange={e => setValue(e.target.value)} placeholder={"Username"}/>
    )}
</Field>

Field Props

PropertyTypeDescription
namestringThe name of the field in the form.
initialValueTThe initial value of the form field.
resetWithValueTThe value to which a form field should be reset upon calling the reset() method.
preserveValuebooleanPreserve the field's values when unmount.
listenTostring[]A list of form field names to listen to. When a listened field updates it's value, it will trigger the relevant onChangeValidation change detection. Useful when making one field depend on the validation of another.
children(props: FieldInstance<T>) => JSX.ElementPassed FieldInstance, expected to return a JSX element.
onChangeValidate() => Promise<boolean> or ZodTypeThe validation logic for when the user has changed the field value. Either a Zod type or Promise. If resolved, no error is passed. If rejected, rejection string is set as an error.
onBlurValidate() => Promise<boolean> or ZodTypeThe validation logic for when the user has blurred the field. Either a Zod type or Promise. If resolved, no error is passed. If rejected, rejection string is set as an error.
onMountValidate() => Promise<boolean> or ZodTypeThe validation logic for when the component is mounted. Either a Zod type or Promise. If resolved, no error is passed. If rejected, rejection string is set as an error.
onSubmitValidate() => Promise<boolean> or ZodTypeThe validation logic for when the user has submitted the form. Either a Zod type or Promise. If resolved, no error is passed. If rejected, rejection string is set as an error.
onChangeHint() => Promise<boolean> or ZodTypeThe soft validation logic for when the user has changed the field value. Either a Zod type or Promise. If resolved, no hint is passed. If rejected, rejection string is set as a hint.
onBlurHint() => Promise<boolean> or ZodTypeThe validation logic for when the user has blurred the field. Either a Zod type or Promise. If resolved, no hint is passed. If rejected, rejection string is set as a hint.
onMountHint() => Promise<boolean> or ZodTypeThe soft validation logic for when the component is mounted. Either a Zod type or Promise. If resolved, no hint is passed. If rejected, rejection string is set as a hint.
onSubmitHint() => Promise<boolean> or ZodTypeThe soft validation logic for when the user has submitted the form. Either a Zod type or Promise. If resolved, no hint is passed. If rejected, rejection string is set as a hint.
onSubmitTransform(value: T) => any or ZodTypeThe transform function for when the user has submitted the form. Either a Zod transform or Function. The form will receive the transformed value instead if set.
memoChildany[]An array of items passed to the inner useMemo which helps prevent re-renders on the field.

Interface FieldInstance

PropertyTypeDescription
valueTT is the type of the Field that's passed to the <Field<T>> component.
setValue(val: T) => voidA function useful to change the value of a field
onBlur() => voidA function expected to be passed to the onBlur element property.
errorsstring[]The list of errors currently applied to the field.
setErrors(errors: string[]) => voidA way to set the errors present on the field.
hintsstring[]The list of hints currently applied to the field.
setHints(hints: string[]) => voidA way to set the hints present on the field.
isValidbooleanA helper property to check if errors is an empty array.
isValidatingbooleanA helper property to check if the field is running a validation.
isTouchedbooleanA boolean to say if the field has been focused and blurred, regardless of user input.
setIsTouched(val: boolean) => void
isDirtybooleanA boolean to say if the field has had any kind of user input.
setIsDirty(val: boolean) => void
propsFieldPropsThe properties originally passed to a field from the component.
validate(rule: 'onChangeValidate' | 'onSubmitValidate' | 'onMountValidate' | 'onBlurValidate') => voidA function used to manually run change detection on a field instance.