A set of components to handle capturing input and submitting data
The <FormComponent> uses the React Final Form library under the hood.
Default
A basic form is made up of a parent <FormComponent> and form-specific child input components, see . The <FormComponent> requires an onSubmit prop to handle the form submission action.
Keep the number of fields as low as possible to avoid overwhelming users. Consider splitting forms into multiple steps if the number of fields grows too long.
Avoid asking for unnecessary details. Where appropriate explain why details are being requested e.g. "We'll use this information to tailor your experience".
Consider other options to break up multi step forms, for example a grid of visual cards can be more interesting than a series of plain dropdown inputs.
Input validation
Provide a validate function to the child input components, that returns an error message when the field value is invalid. Validation runs before form submission therefore invalid data will block the <FormComponent>'s onSubmit function.
The onSubmit function can return a FormSubmitError object when something goes wrong during form submission. Use the helper function createFormSubmitErrorObject() to create the error object format with a required general error message and optional field-specific error messages.
Showing an error message
Consume FormContext to receive the submitError, which can then be used to render error messaging to the user.
Editable example
<FormComponent onSubmit={(data)=>{
try{
thrownewError();
}catch(error){
returncreateFormSubmitErrorObject("Something went wrong!",{firstName:"Is that your real name?"});
Use the <OnFieldValueChangeSpy> component to react to value changes. The component must be rendered inside the <FormComponent>. The onChange handler will trigger when a field is blurred AND the field's value has been changed since last blur.
<OnFieldValueChangeSpyonChange={(field)=> window.alert(`The ${field} field was changed`)}/>
<FormSubmitButton>
Submit
</FormSubmitButton>
</div>
</FormComponent>
Component sizing
Providing a fieldSize prop to the <FormComponent> parent will set a consistent size on the child elements. Supported values are "small", "medium", "large".
The <FormSubmitButton> component also accepts a width prop to set the button to the width of the form.