HTML Adapter

useForm() Hook

Sets up the form hook for use with HTML form elements.

Signature:
function useForm<Values extends FormValues>(
initialValues: Values,
onSubmit: (values: Values) => void | Promise<void>,
options?: FormOptions<Values>
): FormiteHtmlForm<Values>

Parameters

ParameterTypeDescription
initialValuesValuesInitial values of the form
onSubmit(values: Values) => void ǀ Promise<void>The function that is called when submitting the form.
optionsFormOptions<Values>Optional Form options
Returns:

FormiteHtmlForm<Values>

The forms state and API, a Form component and a handleSubmit callback.

interface FormiteHtmlForm<Values extends FormValues> extends FormiteForm<Values> {
Form: (props: React.DetailedHTMLProps<React.FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>) => JSX.Element;
handleSubmit: (ev: React.FormEvent<HTMLFormElement>) => void;
}

Remarks

All initial field values should be set even if they are undefined.

useInput() Hook

Sets up a form's field hook for use with an HTML input element.

Signature:
function useInput(field: Field, onValidate?: ValidateFieldHandler, metadata?: any): {
value: string | undefined;
onBlur: () => void;
onChange: (ev: React.ChangeEvent<HTMLInputElement>) => void;
};

Parameters

ParameterTypeDescription
fieldFieldThe Field to connect to an input component.
onValidateValidateFieldHandlerAn optional callback function to validate the field.
metadataanyAn optional data value that is stored with the .
Returns:
{
value: string | undefined;
onBlur: () => void;
onChange: (ev: React.ChangeEvent<HTMLInputElement>) => void;
}

The properties to pass to the HTML input component.

Example

<input type="text" {...useInput(fields.stringField)} />

useCheckbox() Hook

Sets up a form's field hook for use with an HTML checkbox element.

Signature:
function useCheckbox(field: Field<boolean>, onValidate?: ValidateFieldHandler, metadata?: any): {
checked: boolean | undefined;
type: string;
onBlur: () => void;
onChange: (ev: React.ChangeEvent<HTMLInputElement>) => void;
};

Parameters

ParameterTypeDescription
fieldField<boolean>The Field to connect to an input component.
onValidateValidateFieldHandlerAn optional callback function to validate the field.
metadataanyAn optional data value that is stored with the .
Returns:
{
checked: boolean | undefined;
type: string;
onBlur: () => void;
onChange: (ev: React.ChangeEvent<HTMLInputElement>) => void;
}

The properties to pass to the HTML input component.

Example

<input {...useCheckbox(fields.aBooleanField)} />

useRadioButton() Hook

Sets up a form's field hook for use with an HTML radio button element.

Signature:
function useRadioButton(field: Field, value: string | number, onValidate?: ValidateFieldHandler, metadata?: any): {
checked: boolean;
type: string;
value: string | number;
onBlur: () => void;
onChange: (ev: React.ChangeEvent<HTMLInputElement>) => void;
};

Parameters

ParameterTypeDescription
fieldFieldThe Field to connect to an input component.
valuestring ǀ numberThe value that should be assigned to the field when the radio button in the group is selected.
onValidateValidateFieldHandlerAn optional callback function to validate the field.
metadataanyAn optional data value that is stored with the .
Returns:
{
checked: boolean;
type: string;
value: string | number;
onBlur: () => void;
onChange: (ev: React.ChangeEvent<HTMLInputElement>) => void;
}

The properties to pass to the HTML input component.

Example

<input {...useRadioButton(fields.someField, "A_VALUE")} />

useSelect() Hook

Sets up a form's field hook for use with an HTML select element.

Signature:
function useSelect(field: Field, onValidate?: ValidateFieldHandler, metadata?: any): {
value: string | string[] | undefined;
onBlur: () => void;
onChange: (ev: React.ChangeEvent<HTMLSelectElement>) => void;
};

Parameters

ParameterTypeDescription
fieldFieldThe Field to connect to a select component.
onValidateValidateFieldHandlerAn optional callback function to validate the field.
metadataanyAn optional data value that is stored with the .
Returns:
{
value: string | string[] | undefined;
onBlur: () => void;
onChange: (ev: React.ChangeEvent<HTMLSelectElement>) => void;
}

The properties to pass to the HTML select component.

Example

<select {...useSelect(fields.someField)}>
<option value="VALUE_1">Option A</option>
<option value="VALUE_2">Option B</option>
</select>