useField()

The useField is the preferred way to get access to the values and metadata of a specific field. Yafl uses a field's location in the Form's component hierarchy to determine the shape of the resulting form value!

useField API

TypeScript

type UseField = <T, F>(name: string | number, config: UseFieldConfig<F, T>) => [InputProps<T>, FieldMeta<F, T>]

Options

name

name: string | number

Name your field! Providing a number usually indicates that this Field appears in an array.

validate

validate?: ((value: T, formValue: F) => string | void) | (Array<(value: T, formValue: F) => string | void)>

A validation function or array of validation functions used to validate a field. Should return a string as an error message if validation fails and undefined if validation passes.

Result

useField<T, F>() returns a tuple that contains InputProps and FieldMeta where T and F correspond to the generic types for the Field and Form respectively.

PropDescription
input: InputProps<T>An object containing the core handlers and props for an input.
Allows for easy use of the spread operator onto an <input />.
meta: FieldMeta<F, T>An object containing any meta state related to the current field as well as some handy helper functions.

InputProps

PropDescription
name: stringForwarded from the name prop of this Field.
value: TThe current value of this Field.
onBlur: (e: React.FocusEvent<any>) => voidThe onBlur handler for your input (DOM only).
Useful if you need to keep track of which Fields have been visited.
onFocus: (e: React.FocusEvent<any>) => voidThe onFocus handler for your input (DOM only).
Useful if you need to keep track of which field is currently active.
onChange: (e: React.ChangeEvent<any>) => voidThe onChange handler for your input (DOM only).
Sets the value of this Field.

FieldMeta

TypeScript: FieldMeta<F, T> where F and T correspond to the generic types for the current Field and Form respectively.

PropDescription
path: stringThe path for this field.
visited: booleanIndicates whether this Field has been visited.
Automatically set to true on when input.onBlur() is called.
touched: booleanIndicates whether this Field has been touched.
Automatically set to true the first time a Field's value is changed.
isDirty: booleanIndicates whether the initialValue for this Field is different from the current formValue.
isActive: booleanIndicates whether this Field is currently in focus.
isValid: booleanIndicates whether this Field is valid based on whether any errors have been returned from this fields validate prop.
errors: string[]An array containing any errors for this Field returned from the validate prop
initialValue: TThe value this Field was initialized with.
setValue: (value: T, touch?: boolean) => voidSets the value for this Field.
Optionally specify if this Field should be touched when this function is called.
If the touch parameter is not provided it defaults to true.
formValue: FThe current value of the Form
submitCount: numberThe number of times the Form has been submitted.
resetForm: () => voidClears all Form state. formValue is reset to its initialValue.
submit: () => voidCalls the onSubmit function supplied to the Form component
forgetState: () => voidResets submitCount, touched and visited to their initial values. The formValue is not reset.
setFormValue: (set: SetFormValueFunc<F>) => voidSets the formValue imperatively.
setFormVisited: (set: SetFormVisitedFunc<F>) => voidSets the Form's visited state imperatively.
Accepts a callback with the Form's previous value.
setFormTouched: (set: SetFormTouchedFunc<F>) => voidSets the Form's touched state imperatively.
Accepts a callback with the Form's previous visited state.