Skip to main content
Kaleidoscope

Form

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.

Editable example

Form Compatible Input Elements

Regular componentForm compatible equivalent
<TextInput/><FormTextInput />
<Checkbox/><FormCheckbox />
<Select/><FormSelect />
<SegmentedControl/><FormSegmentedControl />
<Button/><FormSubmitButton />

Best practices

  • 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.

Editable example

Handling form submission errors

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

Reacting to a submit error

The <OnSubmitFailedSpy> component can trigger a function when a form submit error occurs. It must be rendered inside the <FormComponent>.

This approach can be used in addition to the FormContext approach above to show error messaging and react to an error at the same time.

Editable example

Detecting field interaction

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.

Editable example

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.

Editable example